Python 拼写检查


拼写检查是任何文本处理或分析的基本要求。蟒蛇包 拼写检查器 为我们提供此功能以查找可能拼写错误的单词并建议可能的更正。

首先,我们需要在我们的 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'}