MCPcopy Create free account
hub / github.com/clips/pattern / cosine_similarity

Method cosine_similarity

pattern/vector/__init__.py:1020–1043  ·  view source on GitHub ↗

Returns the similarity between two documents in the model as a number between 0.0-1.0, based on the document feature weight (e.g., tf * idf of words in the text). cos = dot(v1, v2) / (norm(v1) * norm(v2))

(self, document1, document2)

Source from the content-addressed store, hash-verified

1018 sets = frequent = frequent_concept_sets
1019
1020 def cosine_similarity(self, document1, document2):
1021 """ Returns the similarity between two documents in the model as a number between 0.0-1.0,
1022 based on the document feature weight (e.g., tf * idf of words in the text).
1023 cos = dot(v1, v2) / (norm(v1) * norm(v2))
1024 """
1025 # If we already calculated similarity between two given documents,
1026 # it is available in cache for reuse.
1027 id1 = document1.id
1028 id2 = document2.id
1029 if (id1, id2) in self._cos: return self._cos[(id1, id2)]
1030 if (id2, id1) in self._cos: return self._cos[(id2, id1)]
1031 # Calculate the matrix multiplication of the document vectors.
1032 if not getattr(self, "lsa", None):
1033 v1 = document1.vector
1034 v2 = document2.vector
1035 s = cosine_similarity(v1, v2)
1036 else:
1037 # Using LSA concept space:
1038 v1 = id1 in self.lsa and self.lsa[id1] or self._lsa.transform(document1)
1039 v2 = id2 in self.lsa and self.lsa[id2] or self._lsa.transform(document2)
1040 s = sum(a * b for a, b in izip(v1.itervalues(), v2.itervalues())) / (v1.norm * v2.norm or 1)
1041 # Cache the similarity weight for reuse.
1042 self._cos[(id1, id2)] = s
1043 return s
1044
1045 similarity = cosine_similarity
1046

Callers 3

saveMethod · 0.95
nearest_neighborsMethod · 0.95
cosine_similarityMethod · 0.45

Calls 4

cosine_similarityFunction · 0.85
sumFunction · 0.85
transformMethod · 0.80
itervaluesMethod · 0.80

Tested by

no test coverage detected