Permalink
Browse files

Working on ranking a list of reviewers with respect to a single revie…

…wer. This is being implemented with PearsonCorrelation and will be improved later to work with any SimilarityScore algorithm.
  • Loading branch information...
1 parent 5b203fa commit b3b106160662973d60cbb26550ac60c317147e99 @robertgreiner committed Nov 9, 2011
@@ -0,0 +1,71 @@
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework;
+using Recommender.Data;
+using Recommender.Reviews;
+
+namespace Recommender.Tests
+{
+ [TestFixture]
+ class RankReviewersTests
+ {
+ private List<Reviewer> reviewers;
+ IOrderedEnumerable<KeyValuePair<Reviewer, double>> sortedReviewers;
+ private RankReviewers rankReviewers;
+
+ [SetUp]
+ public void SetUp()
+ {
+ var currentUser = BuildCurrentUser();
+ reviewers = BuildReviewers();
+
+ sortedReviewers = new RankReviewers(BuildCurrentUser(), BuildReviewers()).Rank();
+ }
+
+ private List<Reviewer> BuildReviewers()
+ {
+ return new List<Reviewer>
+ {
+ {ReviewerBuilder.BuildReviewer2()},
+ {ReviewerBuilder.BuildReviewer3()},
+ {ReviewerBuilder.BuildReviewer4()},
+ {ReviewerBuilder.BuildReviewer5()},
+ };
+ }
+
+ private static Reviewer BuildCurrentUser()
+ {
+ return ReviewerBuilder.BuildReviewer1();
+ }
+
+ [Test]
+ public void OrderingOfSortedReviewersShouldBeDifferentThanInitialListOfReviewers()
+ {
+ Assert.AreNotEqual(reviewers.First(), sortedReviewers.First().Key);
+ }
+
+ [Test]
+ public void TheMostSimilarReviewerShouldBeR4()
+ {
+ Assert.AreEqual(reviewers.ElementAt(2).Name, sortedReviewers.ElementAt(0).Key.Name);
+ }
+
+ [Test]
+ public void TheSecondRankedReviewerShouldBeR5()
+ {
+ Assert.AreEqual(reviewers.ElementAt(3).Name, sortedReviewers.ElementAt(1).Key.Name);
+ }
+
+ [Test]
+ public void TheThirdRankedReviewerShouldBeR2()
+ {
+ Assert.AreEqual(reviewers.ElementAt(0).Name, sortedReviewers.ElementAt(2).Key.Name);
+ }
+
+ [Test]
+ public void TheLeastSimilarReviewerShouldBeR3()
+ {
+ Assert.AreEqual(reviewers.ElementAt(1).Name, sortedReviewers.ElementAt(3).Key.Name);
+ }
+ }
+}
@@ -49,6 +49,7 @@
<ItemGroup>
<Compile Include="Common\FindSimilarReviewsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RankReviewersTests.cs" />
<Compile Include="Reviews\ReviewerTests.cs" />
<Compile Include="Instructions\SumProductsTests.cs" />
<Compile Include="Instructions\SumSquaresTests.cs" />
@@ -37,5 +37,15 @@ public void TwoReviewersWithSomeSimilarReviewsShouldHaveAScoreBetweenZeroAndOne(
{
Assert.AreEqual(0.396, pearsonCorrelationScore.Score());
}
+
+ [Test]
+ public void TestValuesOfReviewers()
+ {
+ var currentUser = ReviewerBuilder.BuildReviewer1();
+ Assert.AreEqual(0.396, new PearsonCorrelation(currentUser, ReviewerBuilder.BuildReviewer2()).Score());
+ Assert.AreEqual(-0.141, new PearsonCorrelation(currentUser, ReviewerBuilder.BuildReviewer3()).Score());
+ Assert.AreEqual(0.567, new PearsonCorrelation(currentUser, ReviewerBuilder.BuildReviewer4()).Score());
+ Assert.AreEqual(0.423, new PearsonCorrelation(currentUser, ReviewerBuilder.BuildReviewer5()).Score());
+ }
}
}
@@ -95,10 +95,10 @@ public static Reviewer BuildReviewer2()
public static Reviewer BuildReviewer3()
{
var reviewer = new Reviewer {Name = "Michael"};
- reviewer.AddReview("Code Complete", 2.5);
- reviewer.AddReview("C# in Depth", 3.0);
- reviewer.AddReview("Unit Testing", 3.5);
- reviewer.AddReview("Refactoring", 4.0);
+ reviewer.AddReview("Code Complete", 1.5);
+ reviewer.AddReview("C# in Depth", 1.0);
+ reviewer.AddReview("Unit Testing", 4.5);
+ reviewer.AddReview("Refactoring", 5.0);
return reviewer;
}
@@ -0,0 +1,26 @@
+using System.Collections.Generic;
+using System.Linq;
+using Recommender.Reviews;
+using Recommender.SimilarityScore;
+
+namespace Recommender
+{
+ public class RankReviewers
+ {
+ private readonly Reviewer CurrentUser;
+ private readonly List<Reviewer> Reviewers;
+
+ public RankReviewers(Reviewer currentUser, List<Reviewer> reviewers)
+ {
+ CurrentUser = currentUser;
+ Reviewers = reviewers;
+ }
+
+ public IOrderedEnumerable<KeyValuePair<Reviewer, double>> Rank()
+ {
+ var scores = Reviewers.ToDictionary(r => r, r => new PearsonCorrelation(CurrentUser, r).Score());
+
+ return scores.OrderByDescending(key => key.Value);
+ }
+ }
+}
@@ -45,6 +45,7 @@
<ItemGroup>
<Compile Include="Common\FindSimilarReviews.cs" />
<Compile Include="Data\ReviewerBuilder.cs" />
+ <Compile Include="RankReviewers.cs" />
<Compile Include="Reviews\Reviewer.cs" />
<Compile Include="SimilarityScoreRunner.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

0 comments on commit b3b1061

Please sign in to comment.