Bar*_*emy 124 python nltk wordnet
我想检查一个Python程序,如果一个单词在英语词典中.
我相信nltk wordnet界面可能是要走的路,但我不知道如何将它用于这么简单的任务.
def is_english_word(word):
pass # how to I implement is_english_word?
is_english_word(token.lower())
Run Code Online (Sandbox Code Playgroud)
在将来,我可能想检查一个单词的单数形式是否在字典中(例如,属性 - >属性 - >英语单词).我怎么做到这一点?
Kat*_*iel 195
对于(更多)更强大的功能和灵活性,请使用专用的拼写检查库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"]
>>>
Run Code Online (Sandbox Code Playgroud)
PyEnchant
附带一些词典(en_GB,en_US,de_DE,fr_FR),但如果你想要更多语言,可以使用任何OpenOffice词典.
似乎有一个叫做多元化的库inflect
,但我不知道它是否有用.
Sad*_*dik 42
它不适用于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
Run Code Online (Sandbox Code Playgroud)
Sus*_*adi 40
使用NLTK:
from nltk.corpus import wordnet
if not wordnet.synsets(word_to_test):
#Not an English Word
else:
#English Word
Run Code Online (Sandbox Code Playgroud)
如果您在安装wordnet时遇到问题或想尝试其他方法,请参阅本文.
kin*_*all 35
使用一个集来存储单词列表因为查找它们会更快:
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
Run Code Online (Sandbox Code Playgroud)
要回答问题的第二部分,复数已经在一个好的单词列表中,但如果你想出于某种原因专门从列表中排除那些,你可以写一个函数来处理它.但是英语复数规则很棘手,我只是在单词列表中包含复数.
至于在哪里找到英文单词列表,我通过谷歌搜索"英文单词列表"找到了几个.这是一个:http://www.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt 如果你想特别使用其中一种方言,你可以谷歌英语或美国英语.
Jam*_*ood 17
如果您的操作系统使用 Linux 内核,则有一种简单的方法可以从英语/美国词典中获取所有单词。在目录中/usr/share/dict
有一个words
文件。还有一个更具体的american-english
文件british-english
。这些包含该特定语言中的所有单词。您可以通过每种编程语言访问它,这就是为什么我认为您可能想了解这一点。
现在,对于 python 特定用户,下面的 python 代码应该分配列表单词以具有每个单词的值:
import re
file = open("/usr/share/dict/words", "r")
words = re.sub("[^\w]", " ", file.read()).split()
file.close()
def is_word(word):
return word.lower() in words
is_word("tarts") ## Returns true
is_word("jwiefjiojrfiorj") ## Returns False
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
编辑:如果您找不到该words
文件或类似的内容,请参阅下面菲尔博士的评论。
对于更快的基于NLTK的解决方案,您可以对单词集进行散列以避免线性搜索.
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
Run Code Online (Sandbox Code Playgroud)
小智 5
我发现有 3 个基于包的解决方案可以解决这个问题。它们是 pyenchant、wordnet 和语料库(自定义或来自 ntlk)。Pyenchant 无法使用 py3在win64 中轻松安装。Wordnet 不能很好地工作,因为它的语料库不完整。所以对我来说,我选择 @Sadik 回答的解决方案,并使用 'set(words.words())' 来加速。
第一的:
pip3 install nltk
python3
import nltk
nltk.download('words')
Run Code Online (Sandbox Code Playgroud)
然后:
from nltk.corpus import words
setofwords = set(words.words())
print("hello" in setofwords)
>>True
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
146768 次 |
最近记录: |