Created
December 29, 2011 23:16
-
-
Save robertgreiner/1536650 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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