如何在字符串列表中只获取一个字符串的同义词?

时间:2017-07-28 07:16:28

标签: python string list nltk synonym

我正在尝试在列表中查找字符串的同义词。这是我的代码:

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

这是结果:

[[[(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')]]]

但如果我只想获得一个字符串的同义词怎么办?

例如,如果我获得'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')]

如果我获得'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')]

以及'bench'

也是如此

我试过print getSynonyms(word1[0])但结果真的很奇怪。

任何人都可以提供帮助?感谢

1 个答案:

答案 0 :(得分:2)

该函数需要一个数组,这就是为什么你会通过单个字符串传递奇怪的结果。如果您传递一个单词,它将查找单词的每个字母的同义词。您可以传递一个具有一个值的数组:

print getSynonyms([word1[0]])

您还可以重写该函数以删除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