2
import nltk
from nltk import *
from nltk.corpus import wordnet as wn
output=[]
wordlist=[]
entries = nltk.corpus.cmudict.entries()
for entry in entries[:200]: #create a list of words, without the pronounciation since.pos_tag only works with a list
wordlist.append(entry[0])
for word in nltk.pos_tag(wordlist): #create a list of nouns
if(word[1]=='NN'):
output.append(word[0])
for word in output:
x = wn.synsets(word) #remove all words which does not have synsets (this is the problem)
if len(x)<1:
output.remove(word)
for word in output[:200]:
print (word," ",len(wn.synsets(word)))
我想刪除所有單詞沒有synsets,但由於某種原因,它不工作。在運行程序時,我發現即使一個單詞有len(wn.synsets(word))= 0,它也不會從我的列表中刪除。有人可以告訴我哪裏出了問題?Python的IF語句與nltk.wordnet.synsets