cosineSimilarity method

double cosineSimilarity(
  1. Map<String, num> other
)

Returns the cosine similarity between this vector and other.

Calculates the similarity of the vectors measured as the cosine of the angle between them, i.e. the dot product of the vectors divided by the product of their euclidian lengths

Implementation

double cosineSimilarity(Map<String, num> other) {
  // initialize square of euclidian length for the document.
  double eLDSq = 0.0;
  // initialize square of euclidian length for query.
  double eLQSq = 0.0;
  double dotProduct = 0.0;
  final terms = keys.toSet().union(other.keys.toSet());
  for (final t in terms) {
    final vQ = other[t] ?? 0.0;
    final vD = this[t] ?? 0.0;
    eLQSq = eLQSq + pow(vQ, 2);
    eLDSq = eLDSq + pow(vD, 2);
    dotProduct = dotProduct + vQ * vD;
  }
  return dotProduct / (sqrt(eLQSq) * sqrt(eLDSq));
}