Synsets, Hypernyms and Hyponyms
¶
WordNet is the lexical database i.e. dictionary for the English language, specifically designed for natural language processing.
Synset is a special kind of a simple interface that is present in NLTK to look up words in WordNet. Synset instances are the groupings of synonymous words that express the same concept. Some of the words have only one Synset and some have several.
- Noun : n
- Verb : v
- Adjective : a
- Adverb : r
In [12]:
from nltk.corpus import wordnet
syn = wordnet.synsets('welcome')
print(syn)
syn=syn[2]
print ("Synset name : ", syn.name())
# Defining the word
print ("\nSynset meaning : ", syn.definition())
# list of phrases that use the word in context
print ("\nSynset example : ", syn.examples())
[Synset('welcome.n.01'), Synset('welcome.n.02'), Synset('welcome.v.01'), Synset('welcome.v.02'), Synset('welcome.v.03'), Synset('welcome.a.01')] Synset name : welcome.v.01 Synset meaning : accept gladly Synset example : ['I welcome your proposals']
wordnet.synsets(word) can be used to get a list of Synsets. This list can be empty (if no such word is found) or can have few elements.
- Hypernyms: More abstract terms
- Hyponyms: More specific terms.
In [23]:
from nltk.corpus import wordnet
syn = wordnet.synsets('car')[0]
print(syn)
print ("Synset name : ", syn.name())
print ("\nSynset abstract term : ", syn.hypernyms())
print ("\nSynset specific term : ",syn.hypernyms()[0].hyponyms())
Synset('car.n.01') Synset name : car.n.01 Synset abstract term : [Synset('motor_vehicle.n.01')] Synset specific term : [Synset('amphibian.n.01'), Synset('bloodmobile.n.01'), Synset('car.n.01'), Synset('doodlebug.n.01'), Synset('four-wheel_drive.n.01'), Synset('go-kart.n.01'), Synset('golfcart.n.01'), Synset('hearse.n.01'), Synset('motorcycle.n.01'), Synset('snowplow.n.01'), Synset('truck.n.01')]
In [13]:
syn = wordnet.synsets('hello')[0]
print ("Syn tag : ", syn.pos())
syn = wordnet.synsets('doing')[0]
print ("Syn tag : ", syn.pos())
syn = wordnet.synsets('beautiful')[0]
print ("Syn tag : ", syn.pos())
syn = wordnet.synsets('quickly')[0]
print ("Syn tag : ", syn.pos())
syn = wordnet.synsets('pretty')
print ("Syn tag : ", syn)
import nltk
nltk.pos_tag(["pretty"])
Syn tag : n Syn tag : v Syn tag : a Syn tag : r Syn tag : [Synset('pretty.s.01'), Synset('pretty.s.02'), Synset('reasonably.r.01')]
Out[13]:
[('pretty', 'RB')]
In [9]:
from nltk.corpus import wordnet
word1="Knife"
synArray = wordnet.synsets(word1)
print(synArray)
a=synArray[0]
print(a)
print(a.definition())
print(a.pos())
print(a.hypernyms())
print(a.hyponyms())
print(a.hyponyms()[3])
print(a.hyponyms()[3].definition())
[Synset('knife.n.01'), Synset('knife.n.02'), Synset('tongue.n.03'), Synset('knife.v.01')] Synset('knife.n.01') edge tool used as a cutting instrument; has a pointed blade with a sharp edge and a handle n [Synset('edge_tool.n.01')] [Synset('barong.n.01'), Synset('bolo.n.02'), Synset('bowie_knife.n.01'), Synset('bread_knife.n.01'), Synset('butcher_knife.n.01'), Synset('carving_knife.n.01'), Synset('case_knife.n.02'), Synset('cleaver.n.01'), Synset('drawknife.n.01'), Synset('hunting_knife.n.01'), Synset('letter_opener.n.01'), Synset('linoleum_knife.n.01'), Synset('parang.n.01'), Synset('parer.n.02'), Synset('pocketknife.n.01'), Synset('pruning_knife.n.01'), Synset('slicer.n.03'), Synset('surgical_knife.n.01'), Synset('table_knife.n.01')] Synset('bread_knife.n.01') a knife used to cut bread
Lemmas, Synonyms and Antonyms
¶
In [12]:
sArr=wordnet.synsets('car')
print(sArr)
a=sArr[0]
print(a.pos()) # Verb
print(a.definition())
print(a.lemmas())
b=a.lemmas()[0]
print(b)
print(b.name())
[Synset('car.n.01'), Synset('car.n.02'), Synset('car.n.03'), Synset('car.n.04'), Synset('cable_car.n.01')] n a motor vehicle with four wheels; usually propelled by an internal combustion engine [Lemma('car.n.01.car'), Lemma('car.n.01.auto'), Lemma('car.n.01.automobile'), Lemma('car.n.01.machine'), Lemma('car.n.01.motorcar')] Lemma('car.n.01.car') car
In [13]:
sArr=wordnet.synsets('car')
print(sArr)
synArr=[]
for syn in sArr:
for lem in syn.lemmas():
synArr.append(lem.name())
print(synArr)
print(len(synArr)) #But some are repeating..
print(len(set(synArr)))
[Synset('car.n.01'), Synset('car.n.02'), Synset('car.n.03'), Synset('car.n.04'), Synset('cable_car.n.01')] ['car', 'auto', 'automobile', 'machine', 'motorcar', 'car', 'railcar', 'railway_car', 'railroad_car', 'car', 'gondola', 'car', 'elevator_car', 'cable_car', 'car'] 15 11
Antonyms¶
In [29]:
sArr=wordnet.synsets('win')
print(sArr)
a=sArr[1]
print(a.lemmas())
b=a.lemmas()[0]
print(b)
antArr=[]
print(b.antonyms())
print(b.antonyms()[0])
print(b.antonyms()[0].name())
for syn in sArr:
for lem in syn.lemmas():
for ant in lem.antonyms():
antArr.append(ant.name())
print(antArr)
print(set(antArr))
[Synset('win.n.01'), Synset('winnings.n.01'), Synset('win.v.01'), Synset('acquire.v.05'), Synset('gain.v.05'), Synset('succeed.v.01')] [Lemma('winnings.n.01.winnings'), Lemma('winnings.n.01.win'), Lemma('winnings.n.01.profits')] Lemma('winnings.n.01.winnings') [Lemma('losings.n.01.losings')] Lemma('losings.n.01.losings') losings ['losings', 'lose', 'lose', 'fall_back', 'fail'] {'lose', 'fall_back', 'losings', 'fail'}
In [ ]: