Python — Synonyms and Antonyms
Today, In this article you will be going to learn how we can use the python nltk library and we can perform some of the basic tasks. So let’s start…
Synonyms and Antonyms are part of the WordNet. WordNet is a large lexical database for the English language. It is available as part of Python’s Natural Language Toolkit. In wordnet, Synonyms (synsets) are the words that denote the same concept and are interchangeable in many contexts so that they are grouped into unordered sets.
We use synsets to extract the synonyms and antonyms as shown in the below programs.
We can use lemmas() function of the synset. It returns synonyms and antonyms of that particular synset.
from nltk.corpus import wordnet
synonyms = []
for syn in wordnet.synsets("Soil"):
for lm in syn.lemmas():
synonyms.append(lm.name())
print (set(synonyms))
A recommended blog for you: Python — Tagging Words
The output of the program −
set([grease', filth', dirt', begrime', soil',
grime', land', bemire', dirty', grunge',
stain', territory', colly', ground'])
To extract the antonyms we simply use the antonym function.
from nltk.corpus import wordnet
antonyms = []
for syn in wordnet.synsets("ahead"):
for lm in syn.lemmas():
if lm.antonyms():
antonyms.append(lm.antonyms()[0].name())
print(set(antonyms))
The output of the program as follows :
set([backward', back'])
I hope you enjoyed reading this article and finally, you came to know about how we can find Synonyms and Antonyms in python using nltk package. This a very basic task we can perform using python.
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 :
Machine Learning With Python & Statistics
Recommended blogs for you :
Natural Language Processing using NLTK package
Counting Token in Paragraphs using Python
Natural Language Processing — A Simple Introduction
Difference between Natural Language Processing (NLP) and Speech Recognition