Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 422
user2758113 Avatar asked Oct 08 '13 21:10

user2758113


People also ask

How do you use WordNet in Python?

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.

What tool can you use in the library to find synonyms?

Using the thesaurus, you can look up synonyms (different words with the same meaning) and antonyms (words with the opposite meaning).

What is Synset in WordNet?

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.


2 Answers

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']     ... 
like image 175
Francis Bond Avatar answered Sep 29 '22 15:09

Francis Bond


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']) 
like image 32
Mazdak Avatar answered Sep 29 '22 15:09

Mazdak