Permalink
Browse files

implemented Euclidean Distance similarity score

  • Loading branch information...
1 parent 7e3dd26 commit 2576cfc93f65f775e2777df329563f22075aaaad @robertgreiner committed Nov 2, 2011
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using Recommender.Data;
+
+namespace Recommender.Tests
+{
+ [TestFixture]
+ class EuclideanDistanceTests
+ {
+ private EuclideanDistance euclideanDistance;
+
+ [SetUp]
+ public void SetUp()
+ {
+ euclideanDistance = new EuclideanDistance(ReviewerBuilder.BuildReviewer1(), ReviewerBuilder.BuildReviewer2());
+ }
+
+ [Test]
+ public void TwoReviewersWithSomeSimilarReviewsShouldHaveAScoreBetweenZeroAndOne()
+ {
+ Assert.AreEqual(0.148, euclideanDistance.Score());
+ }
+
+ [Test]
+ public void ReviewersThatHaveReviewedUniqueTitlesShouldNotBeSimilar()
+ {
+ var r1 = ReviewerBuilder.BuildReviewer1();
+ var r2 = ReviewerBuilder.BuildAReviewerThatReviewedSomethingUnique();
+ euclideanDistance = new EuclideanDistance(r1, r2);
+ Assert.AreEqual(0, euclideanDistance.Score());
+ }
+
+ [Test]
+ public void ReviewersThatHaveTheSameTasteShouldHaveAPerfectScore()
+ {
+ var r1 = ReviewerBuilder.BuildReviewer1();
+ euclideanDistance = new EuclideanDistance(r1, r1);
+ Assert.AreEqual(1.0, euclideanDistance.Score());
+ }
+ }
+}
@@ -47,6 +47,7 @@
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="EuclideanDistanceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reviews\ReviewerTests.cs" />
<Compile Include="Steps\SumDifferenceSquaresTests.cs" />
@@ -47,15 +47,15 @@ public void TwoSimilarReviewersShouldHaveOneLikeReview()
{
var r1 = ReviewerBuilder.BuildOneReviewMax();
var r2 = ReviewerBuilder.BuildOneReviewMin();
- Assert.AreEqual(1, r1.SimilarItems(r2.Reviews).Count);
+ Assert.AreEqual(1, r1.SimilarReviews(r2.Reviews).Count);
}
[Test]
public void ReviewersThatHaveTheSameTasteShouldHaveAllSimilarItems()
{
var r1 = ReviewerBuilder.BuildReviewer1();
var r2 = ReviewerBuilder.BuildReviewer1();
- Assert.AreEqual(6, r1.SimilarItems(r2.Reviews).Count);
+ Assert.AreEqual(6, r1.SimilarReviews(r2.Reviews).Count);
}
}
}
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Recommender.Reviews;
+using Recommender.Steps;
+
+namespace Recommender
+{
+ public class EuclideanDistance : SimilarityScore
+ {
+ private readonly Reviewer R1;
+ private readonly Reviewer R2;
+
+ public EuclideanDistance(Reviewer r1, Reviewer r2)
+ {
+ R1 = r1;
+ R2 = r2;
+ }
+
+ public double Score()
+ {
+ var similarReviews = R1.SimilarReviews(R2.Reviews);
+
+ if (similarReviews.Count == 0)
+ {
+ return 0.0;
+ }
+
+ var sumDifferenceSquares = new SumDifferenceSquares(similarReviews, R1.Reviews, R2.Reviews).Calculate();
+ return Math.Round(1 / (1 + sumDifferenceSquares), 3);
+ }
+ }
+}
@@ -44,7 +44,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Data\ReviewerBuilder.cs" />
+ <Compile Include="EuclideanDistance.cs" />
<Compile Include="Reviews\Reviewer.cs" />
+ <Compile Include="SimilarityScore.cs" />
<Compile Include="SimilarityScoreRunner.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Steps\SimilarityScoreCommand.cs" />
@@ -20,7 +20,7 @@ public void AddReview(string title, double score)
Reviews.Add(title, score);
}
- public List<string> SimilarItems(Dictionary<string, double> compareTo)
+ public List<string> SimilarReviews(Dictionary<string, double> compareTo)
{
return (from r in Reviews where compareTo.ContainsKey(r.Key) select r.Key).ToList();
}
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Recommender
+{
+ interface SimilarityScore
+ {
+ double Score();
+ }
+}

0 comments on commit 2576cfc

Please sign in to comment.