How to find Synonyms and antonyms from NLTK WordNet in Python

Spread the love

In this tutorial, you will learn how write a program in python to get Synonyms and antonyms from NLTK WordNet.

Wordnet is large lexical database. It has groups of synonyms with examples. Its useful for automatic text analysis and artificial intelligence applications. It has many other languages in it’s collection. It’s a combination of dictionary and thesaurus.

Here we are going to use wordnet to find synonyms and antonyms

#Python nltk wordnet program

from nltk.corpus import wordnet sys = wordnet.synsets("booking") print(sys[0].name()) #lemmatizing - to get root word print(sys[0].lemmas()[0].name()) # we can meaning of that word print(sys[0].definition()) # here we will get realtime examples for this word print(sys[0].examples()) print(sys[1].name()) #lemmatizing - to get root word print(sys[1].lemmas()[1].name()) # we can meaning of that word print(sys[1].definition()) # here we will get realtime examples for this word print(sys[1].examples())


employment for performers or performing groups that lasts for a limited period of time
['the play had bookings throughout the summer']
the act of reserving (a place or passage) or engaging the services of (a person or group)
['wondered who had made the booking']

In the above out ‘n’ refers to the noun and  ‘v’ refers to verb.

Come to main object of our program to find antonyms and synonyms.

import nltk from nltk.corpus import wordnet synonyms = [] antonyms = [] for syn in wordnet.synsets("going"): for l in syn.lemmas(): synonyms.append( if l.antonyms(): antonyms.append(l.antonyms()[0].name()) print('Synonyms for booking',set(synonyms)) print('Antonyms for booking',set(antonyms))


Synonyms for booking {‘exit’, ‘hold_up’, ‘die’, ‘conk_out’, ‘last’, ‘expire’, ‘live’, ‘go_away’, ‘lead’, ‘going_away’, ‘get_going’, ‘rifle’, ‘proceed’, ‘leaving’, ‘choke’, ‘live_on’, ‘decease’, ‘buy_the_farm’, ‘break’, “cash_in_one’s_chips”, ‘drop_dead’, ‘going’, ‘expiration’, ‘perish’, ‘pass’, ‘give-up_the_ghost’, ‘travel’, ‘snuff_it’, ‘belong’, ‘function’, ‘break_down’, ‘move’, ‘give_way’, ‘sledding’, ‘give_out’, ‘run_low’, ‘fit’, ‘work’, ‘kick_the_bucket’, ‘plump’, ‘go_bad’, ‘run’, ‘get’, ‘release’, ‘endure’, ‘operate’, ‘departure’, ‘passing’, ‘conk’, ‘fail’, ‘locomote’, ‘run_short’, ‘go’, ‘depart’, ‘hold_out’, ‘sound’, ‘pop_off’, ‘croak’, ‘loss’, ‘start’, ‘blend’, ‘blend_in’, ‘survive’, ‘extend’, ‘become’, ‘pass_away’}
Antonyms for booking {‘stay_in_place’, ‘malfunction’, ‘come’, ‘stop’, ‘be_born’}


Leave a Reply

Your email address will not be published. Required fields are marked *