Extract Synonyms and Antonyms using Python NLTK library
Natural Language Toolkit(NLTK) was created by George Miller and Christiane Fellbaum of Princeton University. It is a module that gives us a platform to build Python programs. These programs can work with Human Language Data. It is mainly used for Natural Language Processing. It comes with a suite of text processing libraries for classification, stemming, tokenization, lemmatization, word count and semantic reasoning. NLTK helps with splitting sentences or words from main paragraphs to making a machine understand what the text is all about.
Wordnet package is a part of the NLTK module. It consists of a huge database for the English Language. Wordnet database contains Nouns, Adjectives, Adverbs, Verbs grouped in sets of cognitive synonyms also known as synsets. This package is used to find the meaning of words, antonyms, synonyms and much more.
For writing a python program to extract synonyms and antonyms, we first have to install and import the NLTK(Natural Language ToolKit) module in our system. The entire python relies on the ‘pip’ command for installing and managing additional libraries and dependencies. Type the following command in your terminal.
Next, is downloading the wordnet package from NLTK.
Installation:
# For Installing NLTK module !pip install nltk import nltk #download the wordnet package nltk.download('wordnet')
Example 1:
Demonstrating a few features and use cases of wordnet.
Similar words group together in a set known as synsets or synonym set. The words inside this set are Lemmas. For instance, chair is a lemma in the synset.
# Import wordnet package from the NLTK module from nltk.corpus import wordnet we are going to use the word "chair" synset = wordnet.synsets("chair") #Let's print the word itself print(f'Word : {synset[0].lemmas()[0].name()}') # Definition of the the word print(f'Definition : {synset[0].definition()}') # Example sentence consist of the respective word print(f'Example sentence : {str(synset[0].examples())}')
Output 1:
Word and synset : chair.n.01 Word : chair Definition : a seat for one person, with a support for the back Example sentence : ['he put his coat over the back of the chair and sat down']
- The function wordnet.synsets(“word”) returns an array consisting all synsets related to the word passed in the function argument. This array is empty for no such word present.
- lemma_names() provides an array of all the words inside the Synset.
- The definition() function returns a common definition for the lemmas or words in the Synset.
- synset.examples() returns an example sentence for the lemma to make the word more understandable.
Example 2:
Demonstration for extracting synonyms and antonyms from a word
Let us write a program to find synonym and antonym of the word “success” through wordnet. Wordnet is an NLTK corpus reader. Import wordnet with the following command from nltk.corpus.
Create two empty lists for appending synonyms and antonyms of the word. Synonyms of the word present in synsets append in the list synonym and antonyms of the word append in the list antonym.
- Use a ‘for’ loop for fetching every synset from synsets returned by wordnet.synsets.
- synset.lemmas() function gets all the similar lemmas and finds the synonym using name() function appending it in the synonym list
- Similarly, the antonym() function returns all the opposites or antonyms of the word and appends it in the antonym list.
#import NLTK module import nltk #import wordnet package from NLTK module from nltk.corpus import wordnet #declare 2 empty list : one for synonyms and one for antonyms synonym = [] antonym = [] for synset in wordnet.synsets("success"): for i in synset.lemmas(): synonym.append(i.name()) # add all the synonyms available if i.antonyms(): # check whether the antonyms for the given word are available or not antonym.append(i.antonyms()[0].name()) # add all the available antonyms in the list print(f'Synonym List: \n{set(synonym)}') print(f'Antonym List: \n{set(antonym)}')
Output 2:
Synonym List: {'simple', 'simpleton', 'mere', 'dim-witted', 'simple-minded', 'dewy-eyed', 'bare', 'unproblematic', 'unsubdivided', 'round-eyed', 'elementary', 'childlike', 'uncomplicated', 'wide-eyed'} Antonym List: {'complex', 'compound'}
Leave a Reply