WordNet
Importing NLTK and WordNet

from nltk.corpus import wordnet as wn
Polysemy of words
Polysemy of the word 'man'

wn.synsets('man')
[Synset('man.n.01'), Synset('serviceman.n.01'), Synset('man.n.03'), Synset('homo.n.02'), Synset('man.n.05'), Synset('man.n.06'),
Synset('valet.n.01'), Synset('man.n.08'), Synset('man.n.09'), Synset('man.n.10'), Synset('world.n.08'), Synset('man.v.01'), Synset('man.v.02')]

wn.synsets('man', pos = wn.VERB)
[Synset('man.v.01'), Synset('man.v.02')]

Ambiguity degree of the the word 'man'

len(wn.synsets('man'))
13

Synonymy

Synset

Is numred as following: word.pos.number

Example man.n.01

Synonyms of a word in a synset

wn.synset('man.n.01').lemmas()
[Lemma('man.n.01.man'), Lemma('man.n.01.adult_male')]

Semantic richness
len(wn.synset('man.n.01').lemmas())
2
Definition of synset
wn.synset('man.n.01').definition()
'an adult person who is male (as opposed to a woman)'

Usage examples

wn.synset('man.n.01').definition()
'an adult person who is male (as opposed to a woman)'


Taxonomic relations

Hypernyms of a synset

man = wn.synset('man.n.01'); man.hypernyms()
[Synset('adult.n.01'), Synset('male.n.02')]
Hyponyms of a synset

man.hyponyms()

[Synset('adonis.n.01'), Synset('babu.n.01'), Synset('bachelor.n.01'), Synset('bey.n.01'), Synset('black_man.n.01'), Synset('boy.n.02'), Synset('boyfriend.n.01'), Synset('bull.n.02'), Synset('dandy.n.01'), Synset('ejaculator.n.01'), Synset('esquire.n.02'), Synset('eunuch.n.01'), Synset('ex-boyfriend.n.01'), Synset('ex-husband.n.01'), Synset('father-figure.n.01'), Synset('father_figure.n.01'), Synset('fellow.n.06'), Synset('galoot.n.01'), Synset('geezer.n.01'), Synset('gentleman.n.01'), Synset('grass_widower.n.01'), Synset('guy.n.01'), Synset('herr.n.01'), Synset('hooray_henry.n.01'), Synset('housefather.n.01'), Synset('hunk.n.01'), Synset('inamorato.n.01'), Synset('iron_man.n.01'), Synset('ironside.n.01'), Synset('middle-aged_man.n.01'), Synset('monsieur.n.01'), Synset('old_boy.n.01'), Synset('old_man.n.01'), Synset('patriarch.n.02'), Synset('peter_pan.n.01'), Synset('ponce.n.01'), Synset('posseman.n.01'), Synset('senhor.n.01'), Synset('shaver.n.01'), Synset('signor.n.01'), Synset('signore.n.01'), Synset('sir.n.01'), Synset('stiff.n.01'), Synset('stud.n.01'), Synset('tarzan.n.01'), Synset('white_man.n.01'), Synset('widower.n.01'), Synset('womanizer.n.01'), Synset('wonder_boy.n.01'), Synset('yellow_man.n.01'), Synset('young_buck.n.01')]

len(man.hyponyms())
51


The highest hypernym  of synset

man.root_hypernyms()
[Synset('entity.n.01')]


Meronym of synset

man.member_holonyms()
[]


Another example

wn.synsets('dog', pos=wn.NOUN)

[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01')]

dog = wn.synset('dog.n.01')

dog.member_holonyms()
[Synset('canis.n.01'), Synset('pack.n.06')]

wn.synset('canis.n.01').definition()
'type genus of the Canidae: domestic and wild dogs; wolves; jackals'


The lowest common hypernym of two sysnsets

wn.synset('man.n.01').lowest_common_hypernyms(wn.synset('woman.n.01'))
[Synset('adult.n.01')]


wn.synset('man.n.01').lowest_common_hypernyms(wn.synset('desk.n.01'))
[Synset('whole.n.02')]


Semantic similarity

dog = wn.synset('dog.n.01')
cat = wn.synset('cat.n.01')
dog.path_similarity(cat)

dog.lch_similarity(cat)

dog.wup_similarity(cat)

























Example


import nltk
nltk.download('wordnet')


from nltk.corpus import wordnet

# Example usage
synsets = wordnet.synsets('car')
print("Synsets for 'car':", synsets)

# Accessing lemma names of synsets
for synset in synsets:
    print("Lemma names:", synset.lemma_names())

# Finding definitions
for synset in synsets:
    print("Definition:", synset.definition())

# Finding examples
for synset in synsets:
    print("Examples:", synset.examples())

آخر تعديل: الخميس، 16 مايو 2024، 7:41 AM