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

Python Methods and Functions

WordNet looks like a thesaurus in the sense that it groups words according to their meanings. However, there are some important differences.

  • First, WordNet connects more than just word forms — chains of letters — but also specific meanings of words. As a result, words that are in close proximity to each other on the network are semantically ambiguous.
  • Second, WordNet labels semantic relationships between words, whereas word groupings in a thesaurus do not follow any explicit pattern. except for the similarity value.

# First, you need to import wordnet:

from nltk.corpus import wordnet

 
# Then we'll use the term “program” to find the syntax like this:

syns = wordnet.synsets ( "program" )

  
# Syntax example:

print (syns [ 0 ]. name ())

 
# Just a word:

print (syns [ 0 ]. lemmas () [ 0 ]. name ())

 
# This first set is defined:

print (syns [ 0 ]. definition ())

  
# Examples of using a word in sentences:

print (syns [ 0 ]. examples ())

The output will look like this:
plan.n.01
make plans
a series of steps to be taken or goals to follow
[“They came up with a six-step plan”, “They discussed plans for a new bond issue”]

Next, how can we distinguish between synonyms and antonyms of a word? Lemmas will be synonymous, and then you can use .antonyms to find antonyms for lemmas. This way we can fill in some lists like:

import nltk

from nltk.corpus import wordnet

synonyms = []

antonyms = []

 

for syn in wordnet.synsets ( "good" ):

for l in syn.lemmas ():

synonyms.append (l. name ())

if l .antonyms ():

antonyms.append (l.antonyms () [ 0 ]. name ())

 

print ( set (synonyms))

print ( set (antonyms))

The output will be two sets of synonyms and antonyms
{& # 39; profitable & # 39 ;, & # 39; just & # 39 ;, & # 39; vertically & # 39 ;, & # 39; thoroughly & # 39;, & # 39; in_force & # 39;, & # 39; good & # 39 ;, & # 39; skillfully & # 39;, & # 39; skillfully & # 39;, & # 39; strong & # 39;, & # 39; unspoiled & # 39;, & # 39; expert & # 39;, & # 39; experienced & # 39 ;, & # 39; & # 39; in_effect & # 39;, & # 39; honorable & # 39;, & # 39; adept & # 39;, & # 39; secure & # 39;, & # 39; goods & # 39;, & # 39; this & # 39 ;, & # 39; reasonably & # 39;, & # 39; right & # 39;, & # 39; respectable & # 39;, & # 39; good & # 39; , & # 39; seriously & # 39;, & # 39; ripe & # 39;, & # 39; salutary & # 39; , & # 39; dear & # 39 ;, & # 39; practiced & # 39;, & # 39; kindness & # 39;, & # 39; safe & # 39;, & # 39; effective & # 39;, & # 39; unspoiled & # 39 ;, & # 39; reliable & # 39;, & # 39; not disbanded & # 39;, & # 39; honest & # 39;, & # 39; full & # 39;, & # 39; close & # 39; , & # 39; trade_good & # 39;} {"evil", "evil", "bad", "bad", "bad"}

Now let's compare the similarity index of any two words

import nltk

from nltk.corpus import wordnet

# Let's compare the noun "ship" and "boat:"

  

w1 = wordnet.synset ( 'run.v.01' ) # v denotes a verb tag

w2 = wordnet.synset ( ' sprint.v.01' )

print (w1.wup_similarity (w2))

Output:
+0.857142857143

w1 = wordnet.synset ( 'ship.n.01' )

w2 = wordnet.synset ( ' boat.n.01' ) # n stands for a noun

print  (w1.wup_similarity (w2))

Output:
0.9090909090909091

This article courtesy of Pratima Upadhyay . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.