How to get synonyms/antonyms from NLTK WordNet in Python?

The NLTK Wordnet can be used to find word synonyms and antonyms. To comprehend the lexical semantics of the terms in the document, the NLTK Corpus package is used to read the corpus.

WordNet is a big English lexical database. The nouns, verbs, adjectives, and adverbs are grouped into some set of cognitive synonyms, which are called Synsets. Synsets are linked together by conceptual-semantic and lexical relationships.

A WordNet is a lexical database that contains semantic relationships between words and their meanings. WordNet semantic relations include hypernyms, synonyms, holonyms, hyponyms, and meronyms. The NLTK WordNet allows the use of synsets to find words inside the WordNet together with their usages, meanings, and examples. The goal of NLTK WordNet is to find representations between senses. With lexical semantics, relationship type detection is connected to WordNet. A dog can be a mammal, as represented by a “IS-A” relationship type sentence.

Thus, NLTK Wordnet is used to identify relationships between words in a document, as well as spam detection, duplication detection, and characteristics of words inside a written text based on their POS Tags.

The structure of WordNet makes it an excellent tool for computational linguistics and natural language processing.

  • WordNet appears to be similar to a thesaurus in that it puts words together depending on their meanings. There are, however, some key distinctions.
  • First, WordNet connects not just word forms (strings of letters), but also precise senses of words. As a result, words in the network that are close to one another are semantically disambiguate.
  • Second, WordNet labels the semantic relationships between words, whereas thesaurus word groupings do not follow any defined pattern other than meaning similarity.

Python Program to get synonyms/antonyms from NLTK WordNet

Follow the below steps to execute the methods.

Steps:

pip install nltk

Output:

Collecting nltk
Downloading nltk-3.7-py3-none-any.whl (1.5 MB)
---------------------------------------- 1.5/1.5 MB 1.8 MB/s eta 0:00:00
Collecting regex>=2021.8.3
Downloading regex-2022.8.17-cp310-cp310-win_amd64.whl (263 kB)
---------------------------------------- 263.0/263.0 kB 2.7 MB/s eta 0:00:00
Collecting joblib
Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
---------------------------------------- 307.0/307.0 kB 4.7 MB/s eta 0:00:00
Collecting tqdm
Downloading tqdm-4.64.0-py2.py3-none-any.whl (78 kB)
---------------------------------------- 78.4/78.4 kB 2.2 MB/s eta 0:00:00
Requirement already satisfied: click in c:\users\cirus\appdata\local\programs\python\python310\lib\site-packages (from nltk) (8.1.2)
Requirement already satisfied: colorama in c:\users\cirus\appdata\local\programs\python\python310\lib\site-packages (from click->nltk) (0.4.4)
Installing collected packages: tqdm, regex, joblib, nltk

Successfully installed joblib-1.1.0 nltk-3.7 regex-2022.8.17

2)downloading wordnet and omw-1.4

import nltk.corpus
nltk.download('wordnet')

Output:

[nltk_data] Downloading package wordnet to /root/nltk_data...
True
nltk.download('omw-1.4')

Output:

[nltk_data] Downloading package omw-1.4 to /root/nltk_data...
True

Method #1: Getting the definition,type and examples of the word

Approach:

  • Import wordnet from corpus of nltk module using the import keyword.
  • Pass the word to find synsets to the synsets() function.
  • Print the example, and type of the word using the name() function.
  • Print just the word.
  • Print the definition of the word using the definition() function.
  • Print the sample sentences where the word is used using the examples() function.
  • The Exit of the Program.

Below is the implementation:

# Import wordnet from corpus of nltk module using the import keyword
from nltk.corpus import wordnet

# Pass the word to find synsets like so:
synsetsofWord = wordnet.synsets("Price")

# Printing the example,type of the word using the name() function
print('Word and type of the word {price}',synsetsofWord[0].name())

# Printing just the word
print('Printing the word',synsetsofWord[0].lemmas()[0].name())

# Printing the definition of the word using the definition() function
print('Definition of the word:',synsetsofWord[0].definition())

# Printing the sample sentences where the word is used using the examples() function
print('Example sentences where the word is used:',synsetsofWord[0].examples())

Output:

Word and type of the word {price} monetary_value.n.01
Printing the word monetary_value
Definition of the word: the property of having material worth (often indicated by the amount of money something would bring if sold)
Example sentences where the word is used: ['the fluctuating monetary value of gold and silver', 'he puts a high price on his services', "he couldn't calculate the cost of the collection"]

Method #2: Getting the synonyms and antonyms list of the word

Approach:

  • Import nltk module using the import keyword
  • Import wordnet from corpus of nltk module using the import keyword
  • Create an empty list to store the synonyms
  • Create an empty list to store the antonyms
  • Loop in the synsets of the word using the for loop
  • Loop in the above using another nested for loop
  • Add the synonym of the word to the synonyms list using the append() function
  • Add the antonyms to the antonyms list.
  • Print all the unique synonyms from the above resultant synonyms list using the set() function.
  • Print all the unique antonyms from the above resultant antonyms list using the set() function.
  • The Exit of the Program.

Below is the implementation:

# Import nltk module using the import keyword
import nltk
# Import wordnet from corpus of nltk module using the import keyword
from nltk.corpus import wordnet
# Create an empty list to store the synonyms
synonyms_lst = []
# Create an empty list to store the antonyms
antonyms_lst = []

# Loop in the synsets of the word using the for loop
for syn in wordnet.synsets("good"):
  # Loop in the above  using another nested for loop
    for l in syn.lemmas():
        # Add the synonym of the word to the synonyms list using the append() function
        synonyms_lst.append(l.name())
        # Adding the antonyms to the antonyms list
        if l.antonyms():
            antonyms_lst.append(l.antonyms()[0].name())

# Print all the unique synonyms from the above resultant synonyms list
# using the set() function
print('The synonyms of the word {good}',set(synonyms_lst))
# Print all the unique antonyms from the above resultant antonyms list
# using the set() function
print('The antonyms of the word {good}',set(antonyms_lst))

Output:

The synonyms of the word {good} {'in_force', 'undecomposed', 'safe', 'well', 'trade_good', 'proficient', 'unspoilt', 'effective', 'ripe', 'just', 'dependable', 'good', 'expert', 'sound', 'thoroughly', 'beneficial', 'skillful', 'salutary', 'honest', 'right', 'respectable', 'estimable', 'in_effect', 'secure', 'unspoiled', 'upright', 'full', 'dear', 'serious', 'commodity', 'skilful', 'near', 'goodness', 'adept', 'honorable', 'practiced', 'soundly'}
The antonyms of the word {good} {'evilness', 'bad', 'evil', 'badness', 'ill'}

Method #3: Comparing the Similarity Index of Two Words

Below is the implementation:

# Import nltk module using the import keyword
import nltk
# Import wordnet from corpus of nltk module using the import keyword
from nltk.corpus import wordnet

# Pass any two random words as an argument to the synset() function of wordnet to
# find similarity of the both the words and store them in two separate variables
# Here v represents the tag verb
word_1 = wordnet.synset('price.v.01') 
word_2 = wordnet.synset('run.v.01')
# Printing the similarity of the both the words passed using the wup_similarity() function
print(word_1.wup_similarity(word_2))

Output:

0.2857142857142857