pythonnltkwordnet

How to get synonyms from nltk WordNet Python


WordNet is great, but I'm having a hard time getting synonyms in nltk. If you search similar to for the word 'small' like here, it shows all of the synonyms.

Basically I just need to know the following: wn.synsets('word')[i].option() Where option can be hypernyms and antonyms, but what is the option for getting synonyms?


Solution

  • If you want the synonyms in the synset (aka the lemmas that make up the set), you can get them with lemma_names():

    >>> for ss in wn.synsets('small'):
    >>>     print(ss.name(), ss.lemma_names())
    
    small.n.01 ['small']
    small.n.02 ['small']
    small.a.01 ['small', 'little']
    minor.s.10 ['minor', 'modest', 'small', 'small-scale', 'pocket-size',  'pocket-sized']
    little.s.03 ['little', 'small']
    small.s.04 ['small']
    humble.s.01 ['humble', 'low', 'lowly', 'modest', 'small']    
    ...