AllenYuan

searchengine - vectorcompare

Apr 26th, 2020
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. class VectorCompare:
  2.     # return the frequency map of words in a document
  3.     def concordance(self, document):
  4.         if type(document) != str:
  5.             raise ValueError('Supplied argument should be of type string')
  6.         con = {}
  7.         for word in document.split(' '):
  8.             if word in con:
  9.                 con[word] = con[word] + 1
  10.             else:
  11.                 con[word] = 1
  12.         return con
  13.  
  14.     # calculate the Euclidean norm of the vector (it's length, by the Pythagorean theorem)
  15.     def magnitude(self, concordance):
  16.         if type(concordance) != dict:
  17.             raise ValueError('Supplied argument should be of type dict')
  18.         total = 0
  19.         for word, count in concordance.items():
  20.             total += count ** 2
  21.         return math.sqrt(total)
  22.  
  23.     def relation(self, concordance1, concordance2):
  24.         if type(concordance1) != dict:
  25.             raise ValueError('Supplied Argument 1 should be of type dict')
  26.         if type(concordance2) != dict:
  27.             raise ValueError('Supplied Argument 2 should be of type dict')
  28.         relevance = 0
  29.         # calculate topvalue = max{count1*count2}
  30.         topvalue = 0
  31.         for word, count in concordance1.items():
  32.             if word in concordance2:
  33.                 topvalue += count * concordance2[word]
  34.         # return topvalue / (|v1||v2|) which is a measurement of how relevant the two
  35.         # vectors are to each other
  36.         if (self.magnitude(concordance1) * self.magnitude(concordance2)) != 0:
  37.             return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))
  38.         else:
  39.             return 0
Advertisement
Add Comment
Please, Sign In to add comment