如何使用Python检查单词是否为英语单词?
我想检查Python程序中英语词典中是否有单词.
I want to check in a Python program if a word is in the English dictionary.
我相信可以使用nltk wordnet界面,但是我不知道如何将其用于如此简单的任务.
I believe nltk wordnet interface might be the way to go but I have no clue how to use it for such a simple task.
def is_english_word(word):
pass # how to I implement is_english_word?
is_english_word(token.lower())
将来,我可能想检查单词的单数形式是否在字典中(例如,属性->属性->英文单词).我将如何实现?
In the future, I might want to check if the singular form of a word is in the dictionary (e.g., properties -> property -> english word). How would I achieve that?
要获得(更多)强大的功能和灵活性,请使用专用的拼写检查库,例如 PyEnchant
.有一个教程,或者您可以直接潜入:
For (much) more power and flexibility, use a dedicated spellchecking library like PyEnchant
. There's a tutorial, or you could just dive straight in:
>>> 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"]
>>>
PyEnchant
带有一些词典(en_GB,en_US,de_DE,fr_FR),但可以使用任何
PyEnchant
comes with a few dictionaries (en_GB, en_US, de_DE, fr_FR), but can use any of the OpenOffice ones if you want more languages.
似乎有一个名为 inflect
的多元化图书馆,但我不知道不管有什么好处.
There appears to be a pluralisation library called inflect
, but I've no idea whether it's any good.