Module: RetrievalLite::Vector
- Defined in:
- lib/retrieval_lite/vector.rb
Class Method Summary collapse
-
.cosine_similarity(scores1, scores2) ⇒ Float
The cosine similarity of the two vectors representing the score of the documents.
-
.dot_product(scores1, scores2) ⇒ Float
The dot product of the two vectors representing the score of the documents.
-
.euclidean_length(scores) ⇒ Float
The euclidean length of the vectors representing the score of the document.
Class Method Details
.cosine_similarity(scores1, scores2) ⇒ Float
5 6 7 8 9 10 11 12 |
# File 'lib/retrieval_lite/vector.rb', line 5 def self.cosine_similarity(scores1, scores2) length = (euclidean_length(scores1) * euclidean_length(scores2)) if length == 0 return 0 else dot_product(scores1, scores2) / length end end |
.dot_product(scores1, scores2) ⇒ Float
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/retrieval_lite/vector.rb', line 17 def self.dot_product(scores1, scores2) raise "document vectors are not of same length" if scores1.size != scores2.size sum = 0 for i in 0...scores1.size sum += scores1[i]*scores2[i] end return sum end |
.euclidean_length(scores) ⇒ Float
30 31 32 33 34 35 36 37 38 |
# File 'lib/retrieval_lite/vector.rb', line 30 def self.euclidean_length(scores) sum = 0 for i in 0...scores.size sum += scores[i] * scores[i] end Math.sqrt(sum) end |