Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- def relation(self, concordance1, concordance2):
- if type(concordance1) != dict:
- raise ValueError('Supplied Argument 1 should be of type dict')
- if type(concordance2) != dict:
- raise ValueError('Supplied Argument 2 should be of type dict')
- relevance = 0
- # calculate topvalue = max{count1*count2}
- topvalue = 0
- for word, count in concordance1.items():
- if word in concordance2:
- topvalue += count * concordance2[word]
- # return topvalue / (|v1||v2|) which is a measurement of how relevant the two
- # vectors are to each other
- if (self.magnitude(concordance1) * self.magnitude(concordance2)) != 0:
- return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))
- else:
- return 0
Advertisement
Add Comment
Please, Sign In to add comment