Python synonyms and antonyms
Python Synonyms and Antonyms
Synonyms and antonyms are available as part of the English lexical database Wordnet. It can be accessed through the NLTK corpus. In Wordnet, synonyms are words that represent the same concept and can be used interchangeably in many contexts. Therefore, they are grouped into unordered sets (synsets). We use these synsets to derive synonyms and antonyms, as shown below.
from nltk.corpus import wordnet
synonyms = []
for syn in wordnet.synsets("Soil"):
for lm in syn.lemmas():
synonyms.append(lm.name())
print (set(synonyms))
When we run the above program, we get the following output –
set([grease', filth', dirt', begrime', soil',
grime', land', bemire', dirty', grunge',
stain', territory', colly', ground'])
To get 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))
When we run the above program, we get the following output –
set([backward', back'])