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?
To use the Wordnet, at first we have to install the NLTK module, then download the WordNet package. In the wordnet, there are some groups of words, whose meaning are same. In the first example, we will see how wordnet returns meaning and other details of a word.
Using the thesaurus, you can look up synonyms (different words with the same meaning) and antonyms (words with the opposite meaning).
WordNet categorizes English words into synonyms, referred to as Synsets (short for a set of synonyms). Every Synset contains a name, a part-of-speech (nouns, verbs, adverbs, and adjectives), and a number. Synsets are used to store synonyms, where each word in the Synset shares the same meaning.
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'] ...
You can use wordnet.synset
and lemmas in order to get all the synonyms:
example :
from itertools import chain from nltk.corpus import wordnet synonyms = wordnet.synsets(text) lemmas = set(chain.from_iterable([word.lemma_names() for word in synonyms]))
Demo:
>>> synonyms = wordnet.synsets('change') >>> set(chain.from_iterable([word.lemma_names() for word in synonyms])) set([u'interchange', u'convert', u'variety', u'vary', u'exchange', u'modify', u'alteration', u'switch', u'commute', u'shift', u'modification', u'deepen', u'transfer', u'alter', u'change'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With