Skip to content

Instantly share code, notes, and snippets.

@robertgreiner
Created December 29, 2011 23:16
Show Gist options
  • Save robertgreiner/1536650 to your computer and use it in GitHub Desktop.
Save robertgreiner/1536650 to your computer and use it in GitHub Desktop.
public class EuclideanDistance : SimilarityScore
{
private readonly Reviewer CompareTo;
private readonly Reviewer CompareWith;
public EuclideanDistance(Reviewer compareTo, Reviewer compareWith)
{
CompareTo = compareTo;
CompareWith = compareWith;
}
public double Score()
{
var similarTitles = FindSharedItems();
if (similarTitles.Count == 0)
{
return 0.0;
}
double sumOfSquares = similarTitles.Sum(title =>
Math.Pow(CompareTo.Reviews[title] - CompareWith.Reviews[title], 2));
return Math.Round(1 / (1 + sumOfSquares), 3);
}
public List<string> FindSharedItems()
{
return (from r in CompareTo.Reviews where
CompareWith.Reviews.ContainsKey(r.Key) select r.Key).ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment