首页 > 编程知识 正文

如何使用 Python 中的enchant检查给定单词的拼写

时间:2023-11-26 13:13:30 阅读:309448 作者:ITQW

Python 有一个名为enchant的模块,用于检查单词的拼写并给出更正建议。它还为单词的反义词和同义词提供了选项。它还可以在字典中检查一个单词是否存在。

如果给定的单词存在于语言字典中, check() 函数返回“真”;否则,它将返回“假”。我们也可以使用检查()功能的这个功能来检查单词的拼写。

enchant模块中,我们还可以使用建议()功能来纠正拼写错误的单词的拼写。

在本教程中,我们将看到一个例子来了解 Python 中的enchant模块在检查给定单词的拼写时的用法以及正确拼写的建议。

安装:

要安装enchant模块,我们可以使用以下命令:

!pip3 install pyenchant

输出:

Collecting pyenchant
  Downloading pyenchant-3.2.2-py3-none-win_amd64.whl (11.9 MB)
Installing collected packages: pyenchant
Successfully installed pyenchant-3.2.2

示例:检查单词的拼写并获取更正建议


# First, we will import the required module
import enchant as EC

# Now, we will create dictionary for the language in use (en_US here)
dict1 = EC.Dict("en_US")

# Then, we will create the list of words
words1 = ["Sung", "Cer", "BOOK", "Peaple", "Dronk", "Bat", "Beur", "Plut", "Pot"]

# Here, we will be finding those words that may be misspelled
misspelled1 = []
for word in words1:
    if dict1.check(word) == False:
        misspelled1.append(word)
print ("The misspelled words in the list are : " + str(misspelled1))

# Now, we will use suggest() function for the correct spelling of the misspelled words
for word in misspelled1:
    print ("Suggestion for misspelled" + word + " : " + str(dict1.suggest(word)))

输出:

The misspelled words in the list are : ['Cer', 'Peaple', 'Dronk', 'Beur', 'Plut']
Suggestion for misspelledCer : ['Ce', 'Cr', 'Er', 'Cher', 'Cerf', 'Ser', 'Ter', 
'Cor', 'Cdr', 'Der', 'Ger', 'Per', 'Chr', 'Her', 'Yer']
Suggestion for misspelledPeaple : ['Peale', 'People', 'Leaper', 'Plea']
Suggestion for misspelledDronk : ['Cronk', 'Drink', 'Drone', 'Drank', 'Drunk', 'Drongo']
Suggestion for misspelledBeur : ['Bur', 'Beer', 'Bear', 'Blur', 'Eur']
Suggestion for misspelledPlut : ['Pluto', 'Slut', 'Prut', 'Glut', 'Pl ut', 
'Pl-ut', 'Plur', 'Put', 'Plus', 'Plat', 'Plot', 'Pout', 'Plug', 'Plum']

结论

在本教程中,我们讨论了如何安装和使用enchant模块来检查给定单词的拼写,并获得拼写错误单词的建议。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。