简体   繁体   中英

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?

Thanks

Abhi

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():
        print lemma.name(),

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()
            print ss.name(),

exchange interchange exchange change interchange travel go move locomote  # again, some overlap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM