2014-01-18 43 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

相關問題