Python - WordNet Interface



WordNet is a dictionary of English, similar to a traditional thesaurus NLTK includes the English WordNet. We can use it as a reference for getting the meaning of words, usage example and definition. A collection of similar words is called lemmas. The words in WordNet are organized and nodes and edges where the nodes represent the word text and the edges represent the relations between the words. below we will see how we can use the WordNet module.

All Lemmas

from nltk.corpus import wordnet as wn
res=wn.synset('locomotive.n.01').lemma_names()
print res

When we run the above program, we get the following output −

[u'locomotive', u'engine', u'locomotive_engine', u'railway_locomotive']

Word Definition

The dictionary definition of a word can be obtained by using the definition function. It describes the meaning of the word as we can find in a normal dictionary.

from nltk.corpus import wordnet as wn
resdef = wn.synset('ocean.n.01').definition()
print resdef

When we run the above program, we get the following output −

a large body of water constituting a principal part of the hydrosphere

Usage Examples

We can get the example sentences showing some usage examples of the words using the exmaples() function.

from nltk.corpus import wordnet as wn
res_exm = wn.synset('good.n.01').examples()
print res_exm

When we run the above program we get the following output −

['for your own good', "what's the good of worrying?"]

Opposite Words

Get All the opposite words by using the antonym function.

from nltk.corpus import wordnet as wn
# get all the antonyms
res_a = wn.lemma('horizontal.a.01.horizontal').antonyms()
print res_a

When we run the above program we get the following output −

[Lemma('inclined.a.02.inclined'), Lemma('vertical.a.01.vertical')]
Advertisements