def cosine_similarity(repr1, repr2):
"""Calculates cosine similarity (https://en.wikipedia.org/wiki/Cosine_similarity)."""
if repr1 is None or repr2 is None:
return 0
assert not (np.isnan(repr2).any() or np.isinf(repr2).any())
assert not (np.isnan(repr1).any() or np.isinf(repr1).any())
sim = 1 - scipy.spatial.distance.cosine(repr1, repr2)
if np.isnan(sim):
# the similarity is nan if no term in the document is in the vocabulary
return 0
return sim
评论列表
文章目录