user3089927 user3089927 - 1 year ago 123
Python Question

Wordnet Synonyms not returning all values nltk

I am trying to get all the synonyms or similar words using nltk's wordnet but it is not returning.

I am doing:

>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('swap')
[Synset('barter.n.01'), Synset('trade.v.04'), Synset('swap.v.02')]

I also tried doing (from one of the stackoverflow page):

>>> for ss in wn.synsets('swap'):
for sim in ss.similar_tos():
print(' {}'.format(sim))

But I am not getting all the synonyms. I do not want to add synonyms to the wordnet.
I am expecting it to return exchange,interchange, substitute etc.

How to achieve this?



Answer Source

To get synonyms using wordnet, simply do this:

>>> from nltk.corpus import wordnet as wn
>>> for synset in wn.synsets('swap'):
    for lemma in synset.lemmas():

barter swap swop trade trade swap swop switch swap  # note the overlap between the synsets

To obtain some of the words you mentioned, you may have to include hypernyms as well:

>>> for synset in wn.synsets('swap'):
    for hypernym in synset.hypernyms():
        for ss in hypernym.lemmas():  # now you need to iterate through each synset returned by synset.hypernyms()

exchange interchange exchange change interchange travel go move locomote  # again, some overlap
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download