如何使用pydictionary检查输入是否是有效的英语单词

2024-05-19 06:46:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用

from PyDictionary import PyDictionary

word = input("enter a word: ")
dictionary = PyDictionary(word)

check = dictionary.getMeanings()

print(check)
input("press enter to exit")

Tags: tofromimportinputdictionarycheckexitword
2条回答

如果使用PyDictionary,则可以检查单词的含义并将返回值包装在bool函数中:

from PyDictionary import PyDictonary
dictionary = PyDictionary()
valid_word = bool(dictionary.meaning("hdaoij")) # False
valid_word = bool(dictionary.meaning("hello")) # True

valid_word = bool(dictionary.meaning(input("enter a word: ")))

否则,我将在@RoadJDK的答案中使用enchant中的check函数

我刚才看到这篇文章: How to check if a word is an English word with Python?

>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
True
>>> d.check("Helo")
False
>>> d.suggest("Helo")
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 
'Hero', 
"He'll"]
>>>

相关问题 更多 >

    热门问题