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
模塊來檢查給定單詞的拼寫,並獲得拼寫錯誤單詞的建議。
原創文章,作者:P3N2D,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129791.html