AllenYuan

searchengine - concordance and size

Apr 26th, 2020
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import math
  2.  
  3. # the concordance of a document is a frequency map of the words in the document
  4.  
  5. class VectorCompare:
  6.     # return the frequency map of words in a document
  7.     def concordance(self, document):
  8.         if type(document) != str:
  9.             raise ValueError('Supplied argument should be of type string')
  10.         con = {}
  11.         for word in document.split(' '):
  12.             if word in con:
  13.                 con[word] = con[word] + 1
  14.             else:
  15.                 con[word] = 1
  16.         return con
  17.  
  18.     # calculate the Euclidean norm of the vector (it's length, by the Pythagorean theorem)
  19.     def magnitude(self, concordance):
  20.         if type(concordance) != dict:
  21.             raise ValueError('Supplied argument should be of type dict')
  22.         total = 0
  23.         for word, count in concordance.items():
  24.             total += count ** 2
  25.         return math.sqrt(total)
  26. // ...
Advertisement
Add Comment
Please, Sign In to add comment