DEV Community

Cover image for Wordnet, Synonym, Antonym - NLP
datatoinfinity
datatoinfinity

Posted on

Wordnet, Synonym, Antonym - NLP

As you are aware of Synonym and Antonym, so we are going to work with it with help of wordnet.

What is Wordnet?

WordNet is the lexical database i.e. dictionary for the English language, specifically designed for natural language processing.

definition() function will tell you the word definition or meaning of the word.

import nltk
from nltk.corpus import wordnet

syn=wordnet.synsets('Comedy')
print(syn[0].definition())
Output:
light and humorous drama with a happy ending

Synonym

synonym=[]
for s in wordnet.synsets("Happy"):
    for lemma in s.lemmas():
        synonym.append(lemma.name())
print(synonym)
Output:
['happy', 'felicitous', 'happy', 'glad', 'happy', 'happy', 'well-chosen']

Antonym

ant=[]
for a in wordnet.synsets('Healthy'):
    for lemma in a.lemmas():
        if lemma.antonyms():
            ant.append(lemma.antonyms()[0].name())
    
print(ant)

['unhealthy']

Top comments (0)