In [1]:
from nltk.corpus import wordnet

In [2]:
syn = wordnet.synsets('program')
syn


Out[2]:
[Synset('plan.n.01'),
 Synset('program.n.02'),
 Synset('broadcast.n.02'),
 Synset('platform.n.02'),
 Synset('program.n.05'),
 Synset('course_of_study.n.01'),
 Synset('program.n.07'),
 Synset('program.n.08'),
 Synset('program.v.01'),
 Synset('program.v.02')]

In [3]:
syn[0]


Out[3]:
Synset('plan.n.01')

synset


In [4]:
syn[0].name()


Out[4]:
'plan.n.01'

In [5]:
syn[0].definition()


Out[5]:
'a series of steps to be carried out or goals to be accomplished'

In [6]:
syn[0].lemmas()


Out[6]:
[Lemma('plan.n.01.plan'),
 Lemma('plan.n.01.program'),
 Lemma('plan.n.01.programme')]

In [7]:
syn[0].name()


Out[7]:
'plan.n.01'

Just the word


In [8]:
syn[0].lemmas()[0].name()


Out[8]:
'plan'

Examples


In [9]:
syn[0].examples()


Out[9]:
['they drew up a six-step plan', 'they discussed plans for a new bond issue']

Synonyms and Antonyms


In [10]:
synonyms = []
antonyms = []

In [13]:
for syn in wordnet.synsets('good'):
    for l in syn.lemmas():
        synonyms.append(l.name())
        if l.antonyms():
            antonyms.append(l.antonyms()[0].name())

## lemmas are just like synonyms            
            
print('synonyms : ', synonyms)
print('antonyms : ', antonyms)

new_synonyms = set(synonyms)
new_antonyms = set(antonyms)
print('new_synonyms : ', new_synonyms)
print('new_antonyms : ', new_antonyms)


synonyms :  ['probable', 'probable', 'likely', 'probable', 'effective', 'effectual', 'efficacious', 'effective', 'efficient', 'effective', 'effective', 'good', 'in_effect', 'in_force', 'effective', 'effective', 'good', 'good', 'goodness', 'good', 'goodness', 'commodity', 'trade_good', 'good', 'good', 'full', 'good', 'good', 'estimable', 'good', 'honorable', 'respectable', 'beneficial', 'good', 'good', 'good', 'just', 'upright', 'adept', 'expert', 'good', 'practiced', 'proficient', 'skillful', 'skilful', 'good', 'dear', 'good', 'near', 'dependable', 'good', 'safe', 'secure', 'good', 'right', 'ripe', 'good', 'well', 'effective', 'good', 'in_effect', 'in_force', 'good', 'good', 'serious', 'good', 'sound', 'good', 'salutary', 'good', 'honest', 'good', 'undecomposed', 'unspoiled', 'unspoilt', 'good', 'well', 'good', 'thoroughly', 'soundly', 'good']
antonyms :  ['improbable', 'ineffective', 'evil', 'evilness', 'bad', 'badness', 'bad', 'evil', 'ill']
new_synonyms :  {'right', 'near', 'undecomposed', 'upright', 'goodness', 'trade_good', 'full', 'likely', 'estimable', 'skilful', 'good', 'in_effect', 'thoroughly', 'beneficial', 'salutary', 'in_force', 'safe', 'well', 'effective', 'adept', 'soundly', 'dependable', 'ripe', 'efficacious', 'secure', 'unspoilt', 'sound', 'effectual', 'respectable', 'honorable', 'proficient', 'honest', 'practiced', 'unspoiled', 'expert', 'dear', 'commodity', 'efficient', 'just', 'serious', 'probable', 'skillful'}
new_antonyms :  {'badness', 'ineffective', 'improbable', 'evilness', 'bad', 'evil', 'ill'}

Word similarity (semantically)


In [14]:
word1 = wordnet.synset('ship.n.01')
word2 = wordnet.synset('boat.n.01')

Comparing semantic similarity between the two words


In [17]:
word1.wup_similarity(word2)


Out[17]:
0.9090909090909091

In [20]:
word3 = wordnet.synset('cat.n.01')
word1.wup_similarity(word3)


Out[20]:
0.32