「ひとかじりのりんご」

へっぽこエンジニアの備忘録。こちらは旧ブログ。新しい方はこちら→「http://sbkro.github.io」

Sphinxでスペルチェックをさせてみた。(sphinxcontrib.spelling)

「sphinxcontrib.spelling」というSphinx拡張でreSTファイルのスペルチェックができるようなので調べてみました。

インストール

Sphinxでインストールされている前提で、pip installコマンドを実行します。

$ pip install sphinxcontrib-spelling
$ pip install pyenchant

セットアップ

Sphinxプロジェクトを作成後、conf.pyにextensionの設定とsphinxcontrib.spellingの設定を記述します。

# Extensionsを追加
extensions = []
↓
extensions = ['sphinxcontrib.spelling']

...

# 以下2行を追加
spelling_lang='en_US'
spelling_word_list_filename='spelling_wordlist.txt'

'spelling_lang'は、スペルチェックを行う言語。pyEnchantによると、以下の4種類の言語がサポートされているみたいです。

'spelling_word_list_filename'は、辞書ファイル名。ここに単語を記入しておくと、スペルチェック対象から外れます。

$ cat spelling_wordlist.txt
Indices

スペルチェックをさせてみる。

「Shpinx」でエラーが検出されるのを期待するために、index.rstに以下のような記述を行ってみます。

Correct string.

    Sphinx

Wrong string.

    Shpinx

コマンドの基本的な使い方は、以下のとおり

sphinx-build -b spelling <source_path> <output_path>
パラメータ名 説明
source_path スペルチェックを行うファイルのrootパス
spell_check_result_path スペルチェックの結果を保存するパス

実行してみます。

$ sphinx-build -b spelling -d _build/doctrees . _build/spelling
Running Sphinx v1.2.3
Initializing Spelling Checker
loading pickled environment... done
building [spelling]: all documents
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
preparing documents... done
index.rst:20:Shpinx:00%] index

Spelling checker messages written to ./doc/_build/spelling/output.txt
build finished with problems.

検出されました。

また、<output_path>で指定した場所に、'output.txt'というファイルが作成され、チェック結果が格納されます。

$ cat _build/spelling/output.txt
index.rst:20: (Shpinx)

おまけ

ちなみに、Sphinxプロジェクト作成時に自動生成されるMakefileを以下のように編集することで、ドキュメント生成前に、スペルチェックを行うことができます。もし、スペルミスがある場合は異常終了しドキュメントは生成されません。なので、これを通すことで、一定の品質のドキュメントができるのではないかと思います。

html:
    $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
    @echo
    @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
↓
html:
    $(SPHINXBUILD) -b spelling $(ALLSPHINXOPTS) $(BUILDDIR)/spelling
    $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
    @echo
    @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

サンプルコード

サンプルのプロジェクトをGIthubに上げました。参考にしてみてください。

動作環境

参考