[英]how to get a synsets for only one string in a list of strings?
I'm trying to find synsets of strings inside a list. 我正在尝试在列表中查找字符串的同义词集。 here's my code : 这是我的代码:
from nltk.corpus import wordnet as wn
from nltk import pos_tag
word1 = ['orange','man','bench']
def getSynonyms(word1):
synonymList1 = []
for data1 in word1:
wordnetSynset1 = wn.synsets(data1)
tempList1=[]
for synset1 in wordnetSynset1:
synLemmas = synset1.lemma_names()
for i in xrange(len(synLemmas)):
word = synLemmas[i].replace('_',' ')
if pos_tag(word.split()) not in tempList1:
tempList1.append(pos_tag(word.split()))
synonymList1.append(tempList1)
return synonymList1
print getSynonyms(word1)
print
and here's the result : 结果如下:
[[[(u'orange', 'NN')], [(u'orangeness', 'NN')], [(u'orange', 'NN'),
(u'tree', 'NN')], [(u'Orange', 'NN')], [(u'Orange', 'NNP'), (u'River',
'NNP')], [(u'orangish', 'JJ')]], [[(u'man', 'NN')], [(u'adult', 'NN'),
(u'male', 'NN')], [(u'serviceman', 'NN')], [(u'military', 'JJ'), (u'man',
'NN')], [(u'military', 'JJ'), (u'personnel', 'NNS')], [(u'homo', 'NN')],
[(u'human', 'JJ'), (u'being', 'VBG')], [(u'human', 'NN')], [(u'valet',
'NN')], [(u'valet', 'NN'), (u'de', 'IN'), (u'chambre', 'NN')],
[(u'gentleman', 'NN')], [(u"gentleman's", 'NN'), (u'gentleman', 'NN')],
[(u'Man', 'NN')], [(u'Isle', 'NNP'), (u'of', 'IN'), (u'Man', 'NNP')],
[(u'piece', 'NN')], [(u'world', 'NN')], [(u'human', 'JJ'), (u'race', 'NN')],
[(u'humanity', 'NN')], [(u'humankind', 'NN')], [(u'human', 'JJ'),
(u'beings', 'NNS')], [(u'humans', 'NNS')], [(u'mankind', 'NN')]],
[[(u'bench', 'NN')], [(u'terrace', 'NN')], [(u'judiciary', 'NN')],
[(u'workbench', 'NN')], [(u'work', 'NN'), (u'bench', 'NN')], [(u'Bench',
'NN')]]]
but what if I just want to get a synsets of one string? 但是,如果我只想获得一个字符串的同义词集怎么办?
for examples, if I get a synsets for 'orange'
, I will only get and print : 例如,如果我得到'orange'
的同义词集,我只会得到并打印:
[(u'orange', 'NN')], [(u'orangeness', 'NN')], [(u'orange', 'NN'),
(u'tree', 'NN')], [(u'Orange', 'NN')], [(u'Orange', 'NNP'), (u'River',
'NNP')], [(u'orangish', 'JJ')]
if I get a synsets for 'man'
, I will only get and print : 如果我得到'man'
的同义词,我只会得到并打印:
[(u'man', 'NN')], [(u'adult', 'NN'),
(u'male', 'NN')], [(u'serviceman', 'NN')], [(u'military', 'JJ'), (u'man',
'NN')], [(u'military', 'JJ'), (u'personnel', 'NNS')], [(u'homo', 'NN')],
[(u'human', 'JJ'), (u'being', 'VBG')], [(u'human', 'NN')], [(u'valet',
'NN')], [(u'valet', 'NN'), (u'de', 'IN'), (u'chambre', 'NN')],
[(u'gentleman', 'NN')], [(u"gentleman's", 'NN'), (u'gentleman', 'NN')],
[(u'Man', 'NN')], [(u'Isle', 'NNP'), (u'of', 'IN'), (u'Man', 'NNP')],
[(u'piece', 'NN')], [(u'world', 'NN')], [(u'human', 'JJ'), (u'race', 'NN')],
[(u'humanity', 'NN')], [(u'humankind', 'NN')], [(u'human', 'JJ'),
(u'beings', 'NNS')], [(u'humans', 'NNS')], [(u'mankind', 'NN')]
and so does for 'bench'
'bench'
也是如此
I've tried print getSynonyms(word1[0])
but the result is really weird. 我试过print getSynonyms(word1[0])
但结果确实很奇怪。
Anyone can help? 有人可以帮忙吗? thanks 谢谢
That function wants an array, which is why you get weird results passing a single string. 该函数需要一个数组,这就是为什么您通过单个字符串获得奇怪结果的原因。 If you pass it a single word it will look for synsets for each letter of the word. 如果将其传递给一个单词,它将为该单词的每个字母寻找同义词集。 You can pass an array with one value though: 您可以通过一个值传递数组:
print getSynonyms([word1[0]])
You could also rewrite the function to remove the out loop allowing you to pass single words in with something like: 您还可以重写该函数以删除out循环,从而使您可以像这样传递单个单词:
def getSynonyms(word):
synonymList1 = []
wordnetSynset1 = wn.synsets(word)
tempList1=[]
for synset1 in wordnetSynset1:
synLemmas = synset1.lemma_names()
for i in xrange(len(synLemmas)):
word = synLemmas[i].replace('_',' ')
if pos_tag(word.split()) not in tempList1:
tempList1.append(pos_tag(word.split()))
synonymList1.append(tempList1)
return synonymList1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.