private double GetDotProduct(DocumentBase d1, DocumentBase d2) { if (cache.ContainsKey(d1) && cache[d1].ContainsKey(d2)) { return(cache[d1][d2]); } if (cache.ContainsKey(d2) && cache[d2].ContainsKey(d1)) { return(cache[d2][d1]); } var weights1 = _tfIdfWeights[d1]; var weights2 = _tfIdfWeights[d2]; double sum = 0; foreach (var term in weights1.Keys) { var w1 = weights1[term]; var w2 = weights2[term]; sum += w1 * w2; } if (!cache.ContainsKey(d1)) { cache[d1] = new Dictionary <DocumentBase, double>(); } if (!cache[d1].ContainsKey(d2)) { cache[d1][d2] = sum; } return(sum); }
private double GetMagnitude(DocumentBase document) { if (!magnitudes.ContainsKey(document)) { var weights = _tfIdfWeights[document]; var magnitude = weights.Values.Sum(weight => weight * weight); magnitudes[document] = Math.Sqrt(magnitude); } return(magnitudes[document]); }
public Dictionary <string, double> GetWeights(DocumentBase document) { return(_tfIdfWeights.ContainsKey(document) ? _tfIdfWeights[document] : null); }