How to find word similarity in python NLTK
In this tutorial, you will learn how to write a program to find similarity between words in python using nltk library.
In this program, we are going to use WordNet corpus find similarity.
#python NLTK program.
from nltk.corpus import wordnet
w1 = wordnet.synset('cat.n.01')
w2 = wordnet.synset('dog.n.01')
print(w1.wup_similarity(w2))
w1 = wordnet.synset('cat.n.01')
w2 = wordnet.synset('animal.n.01')
print(w1.wup_similarity(w2))
w1 = wordnet.synset('cat.n.01')
w2 = wordnet.synset('human.n.01')
print(w1.wup_similarity(w2))
w1 = wordnet.synset('cat.n.01')
w2 = wordnet.synset('pet.n.01')
print(w1.wup_similarity(w2))
Output:
0.8571428571428571
0.6666666666666666
0.7857142857142857
0.6363636363636364