我有一个字符串列表:
words1 = ['feds', 'move', 'to', 'require', 'cartocar', 'safety', 'communication']
我想使用NLTK wordnet synsets为每个单词找到一个synset。首先,我在列表中使用了一个字符串。 这是我的代码:
from nltk.corpus import wordnet as wn
word = ['feds']
data1 = ' '.join(word)
def getSynonyms(data1):
synonymList1 = []
wordnetSynset1 = wn.synsets(data1)
for synset1 in wordnetSynset1:
for synWords1 in synset1.lemma_names():
synonymList1.append(synWords1)
print synonymList1
print "list of synonyms : ", getSynonyms(data1)
它有效。结果如下:
list of synonyms : [u'Federal', u'Fed', u'federal_official', u'Federal_Reserve_System', u'Federal_Reserve', u'Fed', u'FRS']
但是当我使用字符串列表" words1"时,它不起作用,输出就像这样>> []。
任何人都可以帮忙吗?感谢答案 0 :(得分:1)
您需要单独传递单词,而不是在加入单词后传递。
from nltk.corpus import wordnet as wn
def getSynonyms(word1):
synonymList1 = []
for data1 in word1:
wordnetSynset1 = wn.synsets(data1)
tempList1=[]
for synset1 in wordnetSynset1:
for synWords1 in synset1.lemma_names():
tempList1.append(synWords1)
synonymList1.append(tempList1)
return synonymList1
word1 = ['feds', 'move', 'to', 'require', 'cartocar', 'safety', 'communication']
print getSynonyms(word1)