Saturday, May 30, 2015

nlp24. Wu-Palmer similarity in Python NLTK

Wu-Palmer similarity gives the distance of terms in the hypernym tree.


We find similarity with game, match, and stick. We note that match can have a meaning similar to either game or stick depending on which definition we use.

# nlp24.py
from __future__ import print_function
from nltk.corpus import wordnet
a = 'game.n.01'
b1 = 'match.n.01'
b2 = 'match.n.02'
c = 'stick.n.01'
A = wordnet.synset(a)
B1 = wordnet.synset(b1)
B2 = wordnet.synset(b2)
C = wordnet.synset(c)
print(a,A.definition())
print(b1,B1.definition())
print(b2,B2.definition())
print(c,C.definition())
print(a,b1,A.wup_similarity(B1))
print(a,b2,A.wup_similarity(B2))
print(a,c,A.wup_similarity(C))
print(b1,c,B1.wup_similarity(C))
print(b2,c,B2.wup_similarity(C))

# game.n.01 a contest with rules to determine a winner
# match.n.01 lighter consisting of a thin piece of wood or cardboard
# tipped with combustible chemical; ignites with friction
# match.n.02 a formal contest in which two or more persons or teams
# compete
# stick.n.01 an implement consisting of a length of wood
# game.n.01 match.n.01 0.125
# game.n.01 match.n.02 0.571428571429
# game.n.01 stick.n.01 0.133333333333
# match.n.01 stick.n.01 0.705882352941
# match.n.02 stick.n.01 0.133333333333

No comments:

Post a Comment