lobjc
lobjc

Reputation: 2961

How to get NLP synonyms

I have this snippet for getting synonyms (I got it from one of the posts). I want to get the synonyms in a list and not as it prints as shown below

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)

If I use : print item, I get this answer:

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

If I use print T.append(item), I get this:

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

what I want is this:

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

Upvotes: 1

Views: 262

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46839

T.append(item) appends item to the list and returns None. if i understand correctly, you want to see the list growing (right?). then you could try this:

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

or (maybe better); return T at the end of your method and use it like this:

T = me()
print T

Upvotes: 2

Related Questions