Python – WordNet Interface
Python – WordNet Interface
WordNet is an English dictionary, similar to a traditional vocabulary, and NLTK includes WordNet for English. We can use WordNet as a reference to obtain word meanings, usage examples, and definitions. A collection of similar words is called a morphological token. WordNet words are organized into nodes and edges, where nodes represent word context and edges represent relationships between words. Below we will see how to use the WordNet module.
All morphological identifiers
from nltk.corpus import wordnet as wn
res = wn.synset('locomotive.n.01').lemma_names()
print res
When we run the above program, we will get the following output –
[u'locomotive', u'engine', u'locomotive_engine', u'railway_locomotive']
Word Definition
You can use the definition function to get the dictionary definition of a word. It describes the meaning of the word, just like we would find 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 will get the following output –
a large body of water constituting a principal part of the hydrosphere
Usage Examples
We can use the examples() function to get example sentences showing examples of word usage.
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 will get the following output –
['for your own good', "what's the good of worrying?"]
Antonyms
Use the antonym function to get all the antonyms.
from nltk.corpus import wordnet as wn
res_a = wn.lemma('horizontal.a.01.horizontal').antonyms()
print res_a
When we run the above program, we will get the following output –
[Lemma('inclined.a.02.inclined'), Lemma('vertical.a.01.vertical')]