Domanda

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

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top