如何用Python检查一个单词是否是英语单词?

2024-05-14 05:09:35 发布

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

如果英语词典中有单词,我想签入Python程序。

我相信nltk wordnet接口可能是一种方法,但我不知道如何使用它来完成这样一个简单的任务。

def is_english_word(word):
    pass # how to I implement is_english_word?

is_english_word(token.lower())

将来,我可能要检查字典中是否有单词的单数形式(例如properties->;property->;english word)。我怎样才能做到这一点?


Tags: to方法gt程序englishisdefpass
3条回答

为了获得更多的能力和灵活性,可以使用专门的拼写检查库,比如^{}。有一个tutorial,或者你可以直接潜进去:

>>> 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},fr}),但是如果您想要更多的语言,可以使用任何OpenOffice ones

似乎有一个叫做^{}的多元化图书馆,但我不知道它是否有用。

它不能很好地与WordNet一起工作,因为WordNet不包含所有的英语单词。 基于NLTK而没有enchant的另一种可能性是NLTK的单词语料库

>>> from nltk.corpus import words
>>> "would" in words.words()
True
>>> "could" in words.words()
True
>>> "should" in words.words()
True
>>> "I" in words.words()
True
>>> "you" in words.words()
True

使用NLTK:

from nltk.corpus import wordnet

if not wordnet.synsets(word_to_test):
  #Not an English Word
else:
  #English Word

如果在安装wordnet时遇到问题或想尝试其他方法,应该参考this article

相关问题 更多 >