2014-01-18 45 views
1

我想写一个简单的函数,通过NLTK查看WordNet中是否存在这个单词。为什么NLTK WordNet无法找到简单的单词?

def is_known(word): 
    """return True if this word "exists" in WordNet 
     (or at least in nltk.corpus.stopwords).""" 
    if word.lower() in nltk.corpus.stopwords.words('english'): 
     return True 
    synset = wn.synsets(word) 
    if len(synset) == 0: 
     return False 
    else: 
     return True 

为什么像could, since, without, although这样的词会返回False?他们不出现在WordNet中吗?有没有更好的方法来找出WN中是否存在一个单词(使用NLTK)?

我的第一个尝试是消除像“to, if, when, then, I, you”这样的词的“停用词”,但仍然有很常见的词(如could),这是我找不到的。

+0

为什么你返回True当它是一个停用词? – alvas

+1

这只是一个尝试忽略这些词。但我注意到并非所有常见的词都是停用词。 – Sadik

回答

6

WordNet不包含这些单词或像他们这样的单词。有关说明,请参阅从WordNet docs如下:

Q. Why is WordNet missing: of, an, the, and, about, above, because, etc. 
A. WordNet only contains "open-class words": nouns, verbs, adjectives, and adverbs. Thus, excluded words include determiners, prepositions, pronouns, conjunctions, and particles. 

你也不会在WordNet中的在线版本找到这类的话。

+0

thanx链接 – Sadik

0

你可以尝试提取WordNet中所有的引理,然后核对该列表:

from nltk.corpus import wordnet as wn 
from itertools import chain 
all_lemmas = set(chain(*[i.lemma_names for i in wn.all_synsets()])) 

def in_wordnet(word): 
    return True if word in all_lemmas else False 

print in_wordnet('can') 
print in_wordnet('could') 

[出]:

True 
False 

请注意,共发现含有引理不言。还要注意,词/词条可能是多义词,而不是一个真正的包含词,例如

I can foo bar. VS The water can is heavy

+0

in_wordnet给我的结果与is_known相同,但非常慢(当然不是函数本身) – Sadik

相关问题