NLP | WuPalmer — WordNet similarities

How Wu & amp; Palmer Similarity ?
It calculates the relationship given the depth of the two syntaxes in WordNet taxonomies, as well as the depth of the LCS (Least Common Consumer).

The score can be 0 & lt; score & lt; = 1. The score can never be zero, because LCS depth is never zero (taxonomy root depth is one). 
It calculates similarity based on how similar the meanings of words are and where Synsets appear relative to each other in the hyper tree.

Code # 1: Introducing Synsets

from nltk.corpus import wordnet


syn1 = wordnet.synsets ( `hello` ) [ 0 ]

syn2 = wordnet.synsets ( `selling` ) [ 0 ]


print ( " hello name: " , ())

print ( "selling name: " , ())


 hello name: hello .n.01 selling name: selling.n.01 

Code # 2: Similarity

syn1.wup_similarity (syn2)



hello and sales seem to be 27% similar This is because they share common hyperimages further up two.

Code # 3: Let`s check the hyper names in between.


sorted (syn1.common_hypernyms (syn2))


 [Synset (`abstraction.n.06`), Synset (` entity.n.01`) ] 

One of the main metrics used to calculate similarity is the shortest path, the distance between two Synsets and their shared hypernerm.

Code # 4: Let`s understand the use of hypernerms .

ref = syn1 .hypernyms () [ 0 ]

print ( "Self comprised:"

  syn1.shortest_path_distance (ref))


print ( "Distance of hello from greeting:"

syn1.shortest_path_distance (syn2))


print ( "Distance of greeting from hello:"

syn2.shortest_path_distance (syn1))


 Self comprisedon: 1 Distance of hello from greeting: 11 Distance of greeting from hello: 11 

Note: the similarity score is very high, meaning they are many steps away apart because they are not very similar. The codes mentioned here use “noun”, but any part of speech (POS) can be used.