Python – 拼写检查

Python – 拼写检查

在任何文本处理或分析中,检查拼写是一项基本要求。Python包pyspellchecker为我们提供了这个功能,可以找到可能拼错的单词,并提出可能的纠正建议。

首先,我们需要使用以下命令在Python环境中安装必需的包。

pip install pyspellchecker 
Python

现在我们看下面的内容,了解如何使用该软件包指出错误拼写的单词并提出某些可能正确的单词建议。

from spellchecker import SpellChecker

spell = SpellChecker()

#找到可能拼错的单词
misspelled = spell.unknown(['let', 'us', 'wlak','on','the','groun'])

for word in misspelled:
    # 获取最可能的答案
    print(spell.correction(word))

    # 获取一系列可能的选项
    print(spell.candidates(word))
Python

当我们运行以上程序时,我们得到以下输出:

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

区分大小写

如果我们使用Let替换let,那么它将与字典中最接近匹配的单词进行区分大小写比较,并且结果现在看起来不同。

from spellchecker import SpellChecker

spell = SpellChecker()

#找到可能拼错的单词
misspelled = spell.unknown(['Let', 'us', 'wlak', 'on', 'the', 'groun'])

for word in misspelled:
    # 获取最可能的答案
    print(spell.correction(word))

    # 获取一系列可能的选项
    print(spell.candidates(word))
Python

当我们运行以上程序时,我们得到以下输出:

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'}
Python

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册