Skip to content
Browse files

Added functionality to find opposite reviews between two users.

  • Loading branch information...
1 parent a0684ab commit 7a06753bdeefa31cff93b92fc62f002d76ef32b1 @robertgreiner committed Nov 14, 2011
View
47 Recommender.Tests/Common/FindOppositeReviewsTests.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using Recommender.Common;
+using Recommender.Data;
+
+namespace Recommender.Tests.Common
+{
+ [TestFixture]
+ class FindOppositeReviewsTests
+ {
+
+ [Test]
+ public void TwoDistinctReviewersShouldHaveOneOppositeReview()
+ {
+ var r1 = ReviewerBuilder.BuildOneReviewMax();
+ var r2 = ReviewerBuilder.BuildAReviewerThatReviewedSomethingUnique();
+ Assert.AreEqual(1, new FindOppositeReviews(r1.Reviews, r2.Reviews).Calculate().Count);
+ }
+
+ [Test]
+ public void ShouldReturnSecondUsersUniqueReview()
+ {
+ var r1 = ReviewerBuilder.BuildOneReviewMax();
+ var r2 = ReviewerBuilder.BuildAReviewerThatReviewedSomethingUnique();
+ Assert.AreEqual("Chess", new FindOppositeReviews(r1.Reviews, r2.Reviews).Calculate().First());
+ }
+
+ [Test]
+ public void ReviewersThatHaveTheExactSameTasteShouldHaveNoOppositeItems()
+ {
+ var r1 = ReviewerBuilder.BuildReviewer1();
+ var r2 = ReviewerBuilder.BuildReviewer1();
+ Assert.AreEqual(0, new FindOppositeReviews(r1.Reviews, r2.Reviews).Calculate().Count);
+ }
+
+ [Test]
+ public void ReviewsWithAScoreOfZeroShouldBeIgnored()
+ {
+ var r1 = ReviewerBuilder.BuildAReviewerThatReviewedSomethingUnique();
+ var invalidReviewer = ReviewerBuilder.BuildReviewerWithInvalidReviews();
+ Assert.AreEqual(0, new FindOppositeReviews(r1.Reviews, invalidReviewer.Reviews).Calculate().Count);
+ }
+ }
+}
View
10 Recommender.Tests/Common/FindSimilarReviewsTests.cs
@@ -35,16 +35,16 @@ public void ReviewersThatHaveTheSameTasteShouldHaveAllSimilarItems()
public void ReviewsWithAScoreOfZeroShouldBeIgnored()
{
var r1 = ReviewerBuilder.BuildReviewer1();
- var empty = ReviewerBuilder.BuildReviewerWithInvalidReviews();
- Assert.AreEqual(0, new FindSimilarReviews(r1.Reviews, empty.Reviews).Calculate().Count);
+ var invalidReviewer = ReviewerBuilder.BuildReviewerWithInvalidReviews();
+ Assert.AreEqual(0, new FindSimilarReviews(r1.Reviews, invalidReviewer.Reviews).Calculate().Count);
}
[Test]
public void ReviewsWithAScoreOfZeroShouldBeIgnoredSwitchUsers()
{
- var r1 = ReviewerBuilder.BuildReviewer1();
- var empty = ReviewerBuilder.BuildReviewerWithInvalidReviews();
- Assert.AreEqual(0, new FindSimilarReviews(empty.Reviews, r1.Reviews).Calculate().Count);
+ var r1 = ReviewerBuilder.BuildAReviewerThatReviewedSomethingUnique();
+ var invalidReviewer = ReviewerBuilder.BuildReviewerWithInvalidReviews();
+ Assert.AreEqual(0, new FindSimilarReviews(invalidReviewer.Reviews, r1.Reviews).Calculate().Count);
}
}
}
View
39 Recommender/Common/FindOppositeReviews.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Recommender.Common
+{
+ public class FindOppositeReviews
+ {
+ private readonly Dictionary<string, double> FirstUserReviews;
+ private readonly Dictionary<string, double> SecondUserReviews;
+
+ public FindOppositeReviews(Dictionary<string, double> firstUserReviews, Dictionary<string, double> secondUserReviews)
+ {
+ FirstUserReviews = firstUserReviews;
+ SecondUserReviews = secondUserReviews;
+ }
+
+ public List<string> Calculate()
+ {
+ return (from r in SecondUserReviews where ShouldCountReviewAsOpposite(r) select r.Key).ToList();
+ }
+
+ private bool ShouldCountReviewAsOpposite(KeyValuePair<string, double> r)
+ {
+ return IsReviewUnique(r) && IsReviewValid(SecondUserReviews[r.Key]);
+ }
+
+ private static bool IsReviewValid(double value)
+ {
+ return value > 0.0;
+ }
+
+ private bool IsReviewUnique(KeyValuePair<string, double> r)
+ {
+ return !FirstUserReviews.ContainsKey(r.Key);
+ }
+ }
+}

0 comments on commit 7a06753

Please sign in to comment.
Something went wrong with that request. Please try again.