Get Synonyms and Antonyms using Python NLTK

How to get Synonyms and Antonyms using Python NLTK

NPL is one of the fastest field in computer science and artificial intelligence . Most of the NLP tasks requires synonyms and antonyms for a particular word . This article will precisely tell you How to get synonyms and antonyms using Python NLTK  . So Lets begin .

Synonyms and Antonyms using NLTK –

Step 1-

Import the require libraries for Synonyms and Antonyms.

import nltk
from nltk.corpus import wordnet

Here NLTK is NLP library in Python which contains a wordnet module. NLTK has so many other functions apart from this .

Step 2-

Creating an empty list for holding Synonyms and Antonyms.

list_synonyms = []
list_antonyms = []

Step 3 –

Here is the code to get the Synonyms for the word “open”. This will append the Synonyms in the list list_synonyms. Let’s see the below code.

for syn in wordnet.synsets("open"): 
    for lemm in syn.lemmas(): 
        list_synonyms.append(lemm.name())

Step 4 –

This code is for finding Antonyms for the same word open. It is also on quite same pattern as Synonyms .

for syn in wordnet.synsets("open"): 
    for lemm in syn.lemmas(): 
        
        if lemm.antonyms(): 
            list_antonyms.append(lemm.antonyms()[0].name())

Output –

Till step 4 we have appended all elements to the corresponding list. Let’s print them. But before printing them we need to convert them into a set .

print(list_synonyms)
print(list_antonyms)

Lets see the overall output before converting the list into set python –

Synonyms and Antonyms using Python NLTK
Synonyms and Antonyms using Python NLTK

As we can we so many duplicate values here. Let’s convert the list to set .

 

Synonyms and Antonyms using Python NLTK
Synonyms and Antonyms using Python NLTK

As the output shows now we have unique values for Synonyms and Antonyms.

Conclusion –

Well NLTK is really good Natural Language Processing API. NLTK is capable of performing various NLP stuffs like lemmatization, stemmer, POS tagging, etc . It has lots of Natural Language Corpus for programming usages. It is also open source.

I hope you find this article more clear and precise. Keep reading the data science learner’s article.

Thanks 

Data Science Learner Team 

 

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner