Python WordNet interface
Python WordNet Interface
WordNet is an English dictionary, similar to a traditional thesaurus. NLTK includes WordNet for English. We can use it as a reference to find word meanings, usage examples, and definitions. Groups of similar words are called word forms. Words in WordNet are organized into nodes and edges, where nodes represent word literals and edges represent relationships between words. Below we will see how to use the WordNet module.
All word forms
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
Use the define function to get the dictionary definition of a word. This describes the meaning of the word as we would find it in a regular 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
Example Usage
We can use the exmaples() function to get example sentences showing the usage of some words.
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?"]
Opposites
Use the antonym function to get all the opposite words.
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')]