Python ProgrammingPython Programming

How to get definition and Synonyms using TextBlob?

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


Getting Definitions and Synonyms from WordNet

from textblob import TextBlob
from textblob import Word

text_word = Word('safe')

print(text_word.definitions)

synonyms = set()
for synset in text_word.synsets:
    for lemma in synset.lemmas():
        synonyms.add(lemma.name())
        
print(synonyms)

['strongbox where valuables can be safely kept', 'a ventilated or refrigerated cupboard for securing provisions from pests', 'contraceptive device consisting of a sheath of thin rubber or latex that is worn over the penis during intercourse', 'free from danger or the risk of harm', '(of an undertaking) secure from risk', 'having reached a base without being put out', 'financially sound']
{'secure', 'rubber', 'good', 'safety', 'safe', 'dependable', 'condom', 'prophylactic'}