Finding Antonyms And
Synonym
Using
Python Natural Language
Processing Toolkit
www.bisptrainings.com
Synonyms/Antonyms Using Python NLTK
Step#1 Import NLTK and Wordnet
import nltk
from nltk.corpus import wordnet
Wordnet is a large collection of words and vocabulary from the English language that
are related to each other and are grouped in some way. A collection of similar words is
called lemmas.
www.bispsolutions.com
Synonyms/Antonyms Using Python NLTK
Step#2 Declare 2 arrays to collect Synonyms and Antonyms
array_synonyms = []
array_antonyms = []
Step#3 Retrieve all synonym using loop
Synsets: synonyms are words that have similar meanings. A synonym set, or synset, is
a group of synonyms. A synset, therefore, corresponds to an abstract concept.
for vsyn in wordnet.synsets("good"):
Step#4 Retrieve synonym names (using lemmas name property)
And append it to array
The synonyms contained within a synset are called lemmas. You can access the string versions
of these synonyms via a Synset's lemma_names property
for l in vsyn.lemmas():
print(l.name())
array_synonyms.append(l.name())
www.bispsolutions.com
Synonyms/Antonyms Using Python NLTK
Step#5 Check Lemmas Antonyms method
And append it to array_antonyms array
if l.antonyms():
array_antonyms.append(l.antonyms()[0].name())
Step#6 Print Array output
print(array_synonyms)
print(array_antonyms)
www.bispsolutions.com
Any Question?
www.bisptrainings.com