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