Permalink
Browse files

Added code to sum the products of each similar review between two users.

  • Loading branch information...
1 parent 101da36 commit 195f3aad7ebb3ddb76c6a8b5ef695cf0657e05d2 @robertgreiner committed Nov 7, 2011
@@ -52,6 +52,7 @@
<Compile Include="PearsonCorrelationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reviews\ReviewerTests.cs" />
+ <Compile Include="Steps\SumProductsTests.cs" />
<Compile Include="Steps\SumSquaresOfSimilarScoresTests.cs" />
<Compile Include="Steps\SumDifferenceSquaresTests.cs" />
<Compile Include="Steps\SumSimilarScoresTests.cs" />
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using Recommender.Common;
+using Recommender.Data;
+using Recommender.Reviews;
+using Recommender.Steps;
+
+namespace Recommender.Tests.Steps
+{
+ [TestFixture]
+ class SumProductsTests
+ {
+ private Reviewer r1;
+ private Reviewer r2;
+ private List<String> similarReviews;
+
+ [SetUp]
+ public void SetUp()
+ {
+ r1 = ReviewerBuilder.BuildReviewer1();
+ r2 = ReviewerBuilder.BuildReviewer2();
+ similarReviews = new FindSimilarReviews(r1.Reviews, r2.Reviews).Calculate();
+ }
+
+ [Test]
+ public void ShouldSumTheProductsOfBothReviewers()
+ {
+ Assert.AreEqual(59.5, new SumProducts(similarReviews, r1.Reviews, r2.Reviews).Calculate());
+ }
+ }
+}
@@ -52,6 +52,7 @@
<Compile Include="SimilarityScoreRunner.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Steps\SimilarityScoreCommand.cs" />
+ <Compile Include="Steps\SumProducts.cs" />
<Compile Include="Steps\SumSquaresOfSimilarScores.cs" />
<Compile Include="Steps\SumDifferenceSquares.cs" />
<Compile Include="Steps\SumSimilarScores.cs" />
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Recommender.Steps
+{
+ public class SumProducts : SimilarityScoreCommand
+ {
+ private readonly List<string> SimilarReviews;
+ private readonly Dictionary<string, double> FirstUserReviews;
+ private readonly Dictionary<string, double> SecondUserReviews;
+
+
+ public SumProducts(List<string> similarReviews, Dictionary<string, double> firstUserReviews, Dictionary<string, double> secondUserReviews)
+ {
+ SimilarReviews = similarReviews;
+ FirstUserReviews = firstUserReviews;
+ SecondUserReviews = secondUserReviews;
+ }
+
+ public double Calculate()
+ {
+ return SimilarReviews.Sum(title => FirstUserReviews[title] * SecondUserReviews[title]);
+ }
+ }
+}

0 comments on commit 195f3aa

Please sign in to comment.