Get Synonyms and Antonyms of Word Using NLTK WordNet in Python

In this tutorial, we will introduce the way to get synonyms and antonyms of word using NLTK WordNet in python.

1. Import library

import nltk 
from nltk.corpus import wordnet

2. Get synonyms and antonyms of word “good”

synonyms = [] 
antonyms = [] 
  
for syn in wordnet.synsets("good"): 
    for l in syn.lemmas(): 
        synonyms.append(l.name()) 
        if l.antonyms(): 
            antonyms.append(l.antonyms()[0].name()) 
  
print(set(synonyms)) 
print(set(antonyms))

Run this code, you will get synonyms and antonyms of word “good” are:

{'beneficial', 'just', 'upright', 'thoroughly', 'in_force', 'well', 'skilful', 'skillful', 'sound', 'unspoiled', 'expert', 'proficient', 'in_effect', 'honorable', 'adept', 'secure', 'commodity', 'estimable', 'soundly', 'right', 'respectable', 'good', 'serious', 'ripe', 'salutary', 'dear', 'practiced', 'goodness', 'safe', 'effective', 'unspoilt', 'dependable', 'undecomposed', 'honest', 'full', 'near', 'trade_good'} {'evil', 'evilness', 'bad', 'badness', 'ill'}