Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- # the concordance of a document is a frequency map of the words in the document
- class VectorCompare:
- # return the frequency map of words in a document
- def concordance(self, document):
- if type(document) != str:
- raise ValueError('Supplied argument should be of type string')
- con = {}
- for word in document.split(' '):
- if word in con:
- con[word] = con[word] + 1
- else:
- con[word] = 1
- return con
- # calculate the Euclidean norm of the vector (it's length, by the Pythagorean theorem)
- def magnitude(self, concordance):
- if type(concordance) != dict:
- raise ValueError('Supplied argument should be of type dict')
- total = 0
- for word, count in concordance.items():
- total += count ** 2
- return math.sqrt(total)
- // ...
Advertisement
Add Comment
Please, Sign In to add comment