如何使用 NLTK 同义词集在字符串列表中查找同义词?
How to find a synonyms in a list of strings using NLTK synsets?
我有一个字符串列表:
words1 = ['feds', 'move', 'to', 'require', 'cartocar', 'safety', 'communication']
我想使用 NLTK wordnet 同义词集为每个单词找到一个同义词集。首先,我在列表中使用了一个字符串。
这是我的代码:
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" 时,它不起作用并且输出是 none 像这样 >> [].
有人可以帮忙吗?谢谢
你需要单独传递单词,而不是在加入它们之后。
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)
我有一个字符串列表:
words1 = ['feds', 'move', 'to', 'require', 'cartocar', 'safety', 'communication']
我想使用 NLTK wordnet 同义词集为每个单词找到一个同义词集。首先,我在列表中使用了一个字符串。 这是我的代码:
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" 时,它不起作用并且输出是 none 像这样 >> [].
有人可以帮忙吗?谢谢
你需要单独传递单词,而不是在加入它们之后。
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)