All Courses

Python - Synonyms and Antonyms

Neha Kumawat

2 years ago

Python Synonyms and Antonyms
            Python Synonyms and Antonyms with the help of python nltk library. As this is the part of Wordnet and it's part of Python Natural Language.
Synonyms and Antonyms are the 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))
    
Recommended blog for you :  Python - Tagging Words
      
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 uses 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))
output of the program as follows :

set([backward', back'])
          I hope after 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 :
                         
Recommended blogs for you :

Submit Review