Class: Company::Mapping::CosineSimilarity

Inherits:
Object
  • Object
show all
Defined in:
lib/company/mapping/vector_similarity/cosine_similarity.rb

Overview

Implements Cosine Similarity between two non zero vectors and it measures the cosine of the angle between them.

Instance Method Summary collapse

Instance Method Details

#calculate(doc1, doc2) ⇒ Object

Calculates cosine similarity between two documents. The documents are expressed as vectors of tokens (bag of words model).



8
9
10
# File 'lib/company/mapping/vector_similarity/cosine_similarity.rb', line 8

def calculate(doc1, doc2)
  (dotProduct(doc1, doc2) / (Math.sqrt(d(doc1)) * Math.sqrt(d(doc2)))).round(4)
end