All Courses

Python - WordNet Interface

Neha Kumawat

3 years ago

Python - WordNet Interface | Asktalos
A wordNet is lexical database of NLTK corpus ,It is an english dictionary of python.
It used to  find the meanings of words, synonyms, antonyms, and more.
and can be imported as follows:
from nltk.corpus import wordnet

Synsets and Lemmas


In WordNet, a synset is a a set of synonyms. Every Synset has a name, a part-of-speech, and a number.


Similar words in a Synset are called Lemmas. Synset may contains one or more lemmas.Lemmas returns array of words.
#to install the missing module 
import nltk'wordnet')

from nltk.corpus import wordnet as wn
Output of the program is 
[nltk_data] Downloading package wordnet to /root/nltk_data...
[nltk_data]   Unzipping corpora/
['pawl', 'detent', 'click', 'dog']
 Recommended blog for you: NLP - Word Tokenization with Python

Word Definition

Using the definition() method, finds the meaning of the word returns a string. The input to the method is the word of synset.

from nltk.corpus import wordnet as wn
print("Synset Meaning: ",wn.synset('pawl.n.01').definition())
output of the program is

Synset Meaning:  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 ('Synset example :',res_exm)
output of the program is

Synset example :['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 ("opposite Words: ",res_a)
output of the program is

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

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