Advertisement
sreejith2904

Untitled

Jul 29th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. import nltk
  2.  
  3. __author__ = "Sreejith Sreekumar"
  4. __email__ = "sreekumar.s@husky.neu.edu"
  5. __version__ = "0.0.1"
  6.  
  7. nltk.download('wordnet')
  8. from nltk.corpus import wordnet
  9.  
  10.  
  11. #wordnet.synsets('dog')
  12.  
  13. def calc_sim(vectors, words):
  14.    
  15.     num = np.dot(vectors[words[0]], vectors[words[1]])
  16.     den = (np.linalg.norm(vectors[words[0]]) * np.linalg.norm(vectors[words[1]]))
  17.     return (num/den)
  18.  
  19.  
  20.  
  21. def alg1(w1, w2):
  22.  
  23.     wordFromList1 = wordnet.synsets(w1)
  24.     wordFromList2 = wordnet.synsets(w2)
  25.  
  26.     res = []
  27.     if wordFromList1 and wordFromList2: #Thanks to @alexis' note
  28.         s = wordFromList1[0].wup_similarity(wordFromList2[0])
  29.         res.append(s)
  30.     else:
  31.         res = 0.5
  32.  
  33.     return res
  34.    
  35.  
  36.  
  37. alg1('planet','moon')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement