Python-text-processing-python-spelling-check

提供:Dev Guides
移動先:案内検索

Python-スペルチェック

スペルチェックは、テキスト処理または分析の基本的な要件です。 pythonパッケージ pyspellchecker は、この機能を提供して、つづりが間違っている可能性のある単語を見つけ、可能な修正を提案します。

まず、Python環境で次のコマンドを使用して必要なパッケージをインストールする必要があります。

 pip install pyspellchecker

次に、パッケージがどのように間違ってつづられた単語を指摘し、可能性のある正しい単語についていくつかの提案をするのにどのように使用されるかを見る。

from spellchecker import SpellChecker

spell = SpellChecker()

# find those words that may be misspelled
misspelled = spell.unknown(['let', 'us', 'wlak','on','the','groun'])

for word in misspelled:
    # Get the one `most likely` answer
    print(spell.correction(word))

    # Get a list of `likely` options
    print(spell.candidates(word))

上記のプログラムを実行すると、次の出力が得られます-

group
{'group', 'ground', 'groan', 'grout', 'grown', 'groin'}
walk
{'flak', 'weak', 'walk'}

大文字と小文字を区別

letの代わりにLetを使用すると、大文字と小文字が区別される単語と辞書内の最も近い一致する単語との比較になり、結果が異なるようになります。

from spellchecker import SpellChecker

spell = SpellChecker()

# find those words that may be misspelled
misspelled = spell.unknown(['Let', 'us', 'wlak','on','the','groun'])

for word in misspelled:
    # Get the one `most likely` answer
    print(spell.correction(word))

    # Get a list of `likely` options
    print(spell.candidates(word))

上記のプログラムを実行すると、次の出力が得られます-

group
{'groin', 'ground', 'groan', 'group', 'grown', 'grout'}
walk
{'walk', 'flak', 'weak'}
get
{'aet', 'ret', 'get', 'cet', 'bet', 'vet', 'pet', 'wet', 'let', 'yet', 'det', 'het', 'set', 'et', 'jet', 'tet', 'met', 'fet', 'net'}