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 –
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 .
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().name())
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 .
Lets see the overall output before converting the list into set python –
As we can we so many duplicate values here. Let’s convert the list to set .
As the output shows now we have unique values for Synonyms and Antonyms.
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.
Data Science Learner Team