📜  Python拼写检查

📅  最后修改于: 2020-11-06 06:18:41             🧑  作者: Mango


拼写检查是任何文本处理或分析中的基本要求。 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'}