LoginSignup
2
4

More than 5 years have passed since last update.

英単語のスペルチェックツール(python製)

Posted at

自分が書いたパッチ(C言語)内のコメントに英語のtypoが多いのがずっと問題だったので、patch/diffを入力にしてそのコメント中にある英単語のスペルチェックをするようなツールを作ったのでそれのメモ。
CとかC++の/* ・・・ */形式のコメントなら対応可能。

ソースはgithubで公開していてリポジトリはここ

使い方

  • githubからリポジトリをclone
$ git clone git@github.com:MasahikoSawada/Patch-Spell-Checker.git
  • 最低限の設定
$ export PATH=$PATH:/path/to/Patch-Spell-Checker/PatchSpellChecker.py
$ export WLIST_DIR=/path/to/Patch-Spell-Checker/wlist.d/

WLIST_DIR環境変数に入れたくない場合は、実行時に毎回-dで指定する。

  • スペルチェック実行例(パッチに対して実行)
 $ git diff | PatctSpellChecker.py
"xl_heap_lock" might be wrong at line 13.
        "+               * needed before releasing buffer. we can reuse xl_heap_lock "
"pupose" might be wrong at line 14.
        "+               * for this pupose. it should be fine even if we crash midway "
"combocids" might be wrong at line 45.
        "+                       * for logical decoding we need combocids to properly decode the "

※"pupose"は"purpose"のtypo。"xl_heap_lock"や"combocids"は未定義の単語(詳細は後述)

  • スペルチェック実行例(すでにあるソースファイルに対して実行)
-s(--source-file)オプションをつける
$ PatctSpellChecker.py -f src/backend/postmaster/postmaster.c -s
"subprocess" might be wrong at line 11.
        " *       operations, mind you --- it just forks off a subprocess to do them "
"lock-manager" might be wrong at line 18.
        " *       and so it cannot participate in lock-manager operations.  keeping "

※"subprocess"や"lock-manager"は未定義の単語(詳細は後述)

辞書ファイル(Dictionary File)

環境変数WLIST_DIRにある*.dictファイルに単語が書くことで新しい単語を登録することが可能。専門用語等を登録することでスペルチェックの精度を上げることができる。
(ネット上にフリーの単語リストはあるので、ライセンスに気をつけて自分で登録する。)
辞書ファイルで受け付けられるフォーマットは以下の通り。

  • 文書形式(フリーの英語文書をそのまま張ることができる)
文書形式
$ cat sentence.txt
PostgreSQL is a powerful open source object-relational database system.
It is fully ACID compliant, has full support for foreign keys, joins, views, triggers, and stored procedures.
  • 単語形式(独自の専門用語を定義するときに便利)
単語形式
$ cat words.txt
PostgreSQL
is
a
ACID
database
system

既存ソースから単語を抽出したい場合は、-s -wオプションを使う。sortuniqと組み合わせれば、そのまま単語データができる。

$ PatchSpellChecker.py -f src/backend/postmaster/postmaster.c -s -w | sort | uniq
activity_buffer
addr
am_syslogger
antivirus
archiver
archive_recovery
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4