Python - WordNet Interface

Neha Kumawat

4 months ago

A wordNet is lexical database of an english dictionary that is part of the NLTK library of python.
It used to  find the meanings of words, synonyms, antonyms, and more.
 Import wordnet :
from nltk.corpus import wordnet

Synsets and Lemmas

In WordNet, a synset is a group of similar words. Every Synset has a name, a part-of-speech, and a number. Similar words in a Synset are called Lemmas.It returns array of words.

All Lemmas

#o install the missing module 
import nltk
nltk.download('wordnet')

from nltk.corpus import wordnet as wn
lemma_words=wn.synset('pawl.n.01').lemma_names()
print(lemma_words)
Output of the program is 
[nltk_data] Downloading package wordnet to /root/nltk_data...
[nltk_data]   Unzipping corpora/wordnet.zip.
True

['pawl', 'detent', 'click', 'dog']
       
 Recommended blog for you: NLP - Word Tokenization with Python

Word Definition

Using definition() method find the meaning of word of lemmas.it returns string.The input of method is word  from synset.

from nltk.corpus import wordnet as wn
result_def = wn.synset('pawl.n.01').definition()
print(result_def )
output of the program is

a hinged catch that fits into a notch of a ratchet to move a wheel forward or prevent it from moving backward

Usage example()

example() method is used to show the example of the word. 

from nltk.corpus import wordnet as wn
res_exm = wn.synset('dog.n.03').examples()
print (res_exm)
output of the program is

['you lucky dog']

Opposite Words

antonyms() function is used to to get the opposite word.

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

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

Conclusion
In this article we explained wordnet and its methods with example.
      
To know more about python programming language follow the insideaiml youtube channel.  
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…
     
Recommended Course for you
    
Recommended blog for you  

Submit Review

We're Online!

Chat now for any query