Table of contents
1.
What is WordNet?
2.
Installing and Importing WordNet
2.1.
Let’s explore the synsets.
3.
Generating Synonyms
4.
Generating Antonyms
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Synonyms/Antonyms using WordNet

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

What is WordNet?

WordNet is a huge lexical database for the English Language. There are 155,287 words and 117,659 synonym sets included in the WordNet.

 

The synonymous words that express the same concept are present as groupings called synsets (set of synonyms) in WordNet.

Installing and Importing WordNet

WordNet can be downloaded from the NLTK library in python.

import nltk
nltk.download('wordnet')
You can also try this code with Online Python Compiler
Run Code

 

 

from nltk.corpus import wordnet as wn
You can also try this code with Online Python Compiler
Run Code

 

Let’s explore the synsets.

For example, if I take any word such as “Dog,” the synsets will have similar words to the word “Dog.”

syn = wn.synsets('dog')
print(syn)
You can also try this code with Online Python Compiler
Run Code

 

[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

 

print(syn[0].name())
You can also try this code with Online Python Compiler
Run Code

 

dog.n.01

The above representation signifies that the entry is for the word ‘dog,’ which is a noun ‘n,’ and the entry number is 01.

 

print(syn[0].definition())
You can also try this code with Online Python Compiler
Run Code

 

a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds

 

print(syn[0].examples())
You can also try this code with Online Python Compiler
Run Code

 

['the dog barked all night']

 

Generating Synonyms

 

Generating synonyms for a word is a fairly straightforward task since the words are grouped as synsets already. The Lemmas are the word’s synonyms, which we can append in a list.

 

synonyms = []
for synonym in wn.synsets('Animal'):
  for lemma in synonym.lemmas():
    synonyms.append(lemma.name())
print(synonyms)
You can also try this code with Online Python Compiler
Run Code

 

Output

['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual']

 

Generating Antonyms

 

Like we saw above, lemmas are the synonyms words; now, to get the antonyms of a word, we must get the antonyms of the lemmas, using the lemma.antonyms() function.

 

antonyms = []
for synonym in wn.synsets('single'):
  for lemma in synonym.lemmas():
    if lemma.antonyms():
      antonyms.append(lemma.antonyms()[0].name())
print(antonyms)
You can also try this code with Online Python Compiler
Run Code

 

Output

['common', 'double', 'multiple', 'married']

 

antonyms = []
for synonym in wn.synsets('tall'):
  for lemma in synonym.lemmas():
    if lemma.antonyms():
      antonyms.append(lemma.antonyms()[0].name())
print(antonyms)
You can also try this code with Online Python Compiler
Run Code

 

Output

['short']

 

FAQs

1. What is WordNet?

WordNet is a huge lexical database for the English Language.

 

2. How big is the WordNet Database?

The WordNet database has 155,287 English words and 117,659 synonym sets.

 

3. What is the similarity index?

The similarity index signifies how closely two words are related to each other, where one signifies the two words are the same and zero signifies completely different words.

Key Takeaways

Congratulations on finishing the blog!! Below, I have some blog suggestions for you. Go ahead and take a look at these informative articles.

 

In today’s scenario, more & more industries are adapting to AutoML applications in their products; with this rise, it has become clear that AutoML can be the next boon in the technology. Check this article to learn more about AutoML applications.

 

Check out this link if you are a Machine Learning enthusiast or want to brush up your knowledge with ML blogs.

 

If you are preparing for the upcoming Campus Placements, don't worry. Coding Ninjas has your back. Visit this link for a carefully crafted and designed course on-campus placements and interview preparation.

Live masterclass