Python ProgrammingPython Programming

How to get a list of antonyms using TextBlob?

The TextBlob library uses the NLTK library's WordNet interface, this will provide definitions, synonyms and antonyms of specific word.


Getting Antonyms using TextBlob

from textblob import TextBlob
from textblob import Word

text_word = Word('safe')

antonyms = set()
for synset in text_word.synsets:
    for lemma in synset.lemmas():        
        if lemma.antonyms():
            antonyms.add(lemma.antonyms()[0].name())        

print(antonyms)

{'dangerous', 'out'}