如何获得NLP同义词

时间:2015-09-06 04:05:24

标签: python-2.7 nlp nltk wordnet

我有这个代码片段来获取同义词(我从其中一个帖子中得到了它)。我想在列表中获取同义词,而不是打印如下所示

from nltk.corpus import wordnet as wn
import nltk
from nltk.corpus.reader.plaintext import PlaintextCorpusReader

def me():
    T = []
    for i,j in enumerate(wn.synsets('small')):
        #print "Synonyms:", ", ".join(j.lemma_names())
        for item in [", ".join(j.lemma_names())]:
            print T.append(item)

如果我使用: print item, 我得到了这个答案:

small
small
small, little
minor, modest, small, small-scale, pocket-size, pocket-sized
little, small
small
humble, low, lowly, modest, small
little, minuscule, small
little, small
small
modest, small
belittled, diminished, small
small

如果我使用     print T.append(item), 我明白了:

None
None
None
None
None
None
None
None
None
None
None
None
None

我想要的是:

[ small, little, minor, modest, small-scale, pocket-size, pocket-sized, humble, low, lowly, minuscule, belittled, diminished] 

1 个答案:

答案 0 :(得分:2)

T.append(item)item添加到列表中并返回None。如果我理解正确,你想看到列表增长(对吧?)。然后你可以试试这个:

for item in [", ".join(j.lemma_names())]:
    T.append(item)
    print T

或(也许更好); {/ 1}}在您的方法结束时使用它:

return T