如何检查一个单词是否与Python的英文单词?

如果一个单词在英语词典中,我想检查一个Python程序。

我相信nltk wordnet的界面可能是要走的路,但我不知道如何使用它来完成这么简单的任务。

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

在将来,我可能想要检查一个单词的单数forms是否在字典中(例如,属性 – >属性 – >英文单词)。 我将如何实现这一目标?

为了(更多)更多的力量和灵活性,使用像PyEnchant这样的专门的拼写检查库。 有一个教程 ,或者你可以直接跳入:

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

似乎有一个名为inflect的多元化图书馆,但我不知道它是否有好处。

使用NLTK

 from nltk.corpus import wordnet if not wordnet.synsets(word_to_test): #Not an English Word else: #English Word 

如果您在安装wordnet时遇到困难,或者想尝试其他方法,请参阅本文 。

使用一个集合来存储单词列表,因为查找它们会更快:

 with open("english_words.txt") as word_file: english_words = set(word.strip().lower() for word in word_file) def is_english_word(word): return word.lower() in english_words print is_english_word("ham") # should be true if you have a good english_words.txt 

为了回答问题的第二部分,复数已经在一个好的单词列表中,但是如果你想明确地从列表中排除这些单词,你确实可以编写一个函数来处理它。 但英语的复数规则是非常棘手的,我只是在单词列表中包含复数。

至于在哪里可以find英文单词列表,我只是通过谷歌searchfind了几个“英文单词列表”。 这里是一个: http : //www.sil.org/ linguistics /wordlists/english/wordlist/wordsEn.txt你可以谷歌英国或美国英语,如果你想具体的方言之一。

WordNet不能很好地工作,因为WordNet不包含所有的英文单词。 另一种基于NLTK而不附魔的可能性是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的解决scheme,您可以对单词进行散列以避免线性search。

 from nltk.corpus import words as nltk_words def is_english_word(word): # creation of this dictionary would be done outside of # the function because you only need to do it once. dictionary = dict.fromkeys(nltk_words.words(), None) try: x = dictionary[word] return True except KeyError: return False 

对于语义Web方法,您可以对RDF格式的WordNet运行sparql查询 。 基本上只是使用urllib模块发出GET请求并以JSON格式返回结果,使用python'json'模块parsing。 如果不是英文单词,你将得不到结果。

作为另一个想法,您可以查询维基词典的API 。

用pyEnchant.checker SpellChecker:

 from enchant.checker import SpellChecker def is_in_english(quote): d = SpellChecker("en_US") d.set_text(quote) errors = [err.word for err in d] return False if ((len(errors) > 4) or len(quote.split()) < 3) else True print(is_in_english('“办理美国加州州立大学圣贝纳迪诺分校高仿成绩单Q/V2166384296加州州立大学圣贝纳迪诺分校学历学位authentication')) print(is_in_english('“Two things are infinite: the universe and human stupidity; and I\'m not sure about the universe.”')) > False > True 仿成绩单 from enchant.checker import SpellChecker def is_in_english(quote): d = SpellChecker("en_US") d.set_text(quote) errors = [err.word for err in d] return False if ((len(errors) > 4) or len(quote.split()) < 3) else True print(is_in_english('“办理美国加州州立大学圣贝纳迪诺分校高仿成绩单Q/V2166384296加州州立大学圣贝纳迪诺分校学历学位authentication')) print(is_in_english('“Two things are infinite: the universe and human stupidity; and I\'m not sure about the universe.”')) > False > True