Skip to content

Commit

Permalink
add some test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtduc693 authored and Duc Nguyen committed Oct 23, 2024
1 parent 823ad80 commit 8397f46
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions Algorithms.Tests/Algorithms.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="nunit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
Expand Down
29 changes: 24 additions & 5 deletions Algorithms.Tests/RecommenderSystem/CollaborativeFilteringTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Algorithms.RecommenderSystem;
using Moq;
using NUnit.Framework;
using System.Collections.Generic;

Expand All @@ -7,13 +8,15 @@ namespace Algorithms.Tests.RecommenderSystem
[TestFixture]
public class CollaborativeFilteringTests
{
private CollaborativeFiltering recommender = new();
private Mock<ISimilarityCalculator>? mockSimilarityCalculator;
private CollaborativeFiltering? recommender;
private Dictionary<string, Dictionary<string, double>> testRatings = null!;

[SetUp]
public void Setup()
{
recommender = new CollaborativeFiltering();
mockSimilarityCalculator = new Mock<ISimilarityCalculator>();
recommender = new CollaborativeFiltering(mockSimilarityCalculator.Object);

testRatings = new Dictionary<string, Dictionary<string, double>>
{
Expand Down Expand Up @@ -49,7 +52,7 @@ public void CalculateSimilarity_WithValidInputs_ReturnsExpectedResults(
var user1Ratings = new Dictionary<string, double> { [commonItem] = rating1 };
var user2Ratings = new Dictionary<string, double> { [commonItem] = rating2 };

var similarity = recommender.CalculateSimilarity(user1Ratings, user2Ratings);
var similarity = recommender?.CalculateSimilarity(user1Ratings, user2Ratings);

Assert.That(similarity, Is.InRange(-1.0, 1.0));
}
Expand All @@ -60,17 +63,33 @@ public void CalculateSimilarity_WithNoCommonItems_ReturnsZero()
var user1Ratings = new Dictionary<string, double> { ["item1"] = 5.0 };
var user2Ratings = new Dictionary<string, double> { ["item2"] = 4.0 };

var similarity = recommender.CalculateSimilarity(user1Ratings, user2Ratings);
var similarity = recommender?.CalculateSimilarity(user1Ratings, user2Ratings);

Assert.That(similarity, Is.EqualTo(0));
}

[Test]
public void PredictRating_WithNonexistentItem_ReturnsZero()
{
var predictedRating = recommender.PredictRating("nonexistentItem", "user1", testRatings);
var predictedRating = recommender?.PredictRating("nonexistentItem", "user1", testRatings);

Assert.That(predictedRating, Is.EqualTo(0));
}

[Test]
public void PredictRating_WithOtherUserHavingRatedTargetItem_ShouldCalculateSimilarityAndWeightedSum()
{
var targetItem = "item1";
var targetUser = "user1";

mockSimilarityCalculator?
.Setup(s => s.CalculateSimilarity(It.IsAny<Dictionary<string, double>>(), It.IsAny<Dictionary<string, double>>()))
.Returns(0.8);

var predictedRating = recommender?.PredictRating(targetItem, targetUser, testRatings);

Assert.That(predictedRating, Is.Not.EqualTo(0.0d));
Assert.That(predictedRating, Is.EqualTo(3.5d).Within(0.01));
}
}
}
9 changes: 8 additions & 1 deletion Algorithms/RecommenderSystem/CollaborativeFiltering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace Algorithms.RecommenderSystem
{
public class CollaborativeFiltering
{
private readonly ISimilarityCalculator similarityCalculator;

public CollaborativeFiltering(ISimilarityCalculator similarityCalculator)
{
this.similarityCalculator = similarityCalculator;
}

/// <summary>
/// Method to calculate similarity between two users using Pearson correlation.
/// </summary>
Expand Down Expand Up @@ -70,7 +77,7 @@ public double PredictRating(string targetItem, string targetUser, Dictionary<str
var otherUserRatings = ratings[otherUser];
if (otherUserRatings.ContainsKey(targetItem))
{
var similarity = CalculateSimilarity(targetUserRatings, otherUserRatings);
var similarity = similarityCalculator.CalculateSimilarity(targetUserRatings, otherUserRatings);
totalSimilarity += Math.Abs(similarity);
weightedSum += similarity * otherUserRatings[targetItem];
}
Expand Down
13 changes: 13 additions & 0 deletions Algorithms/RecommenderSystem/ISimilarityCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.RecommenderSystem
{
public interface ISimilarityCalculator
{
double CalculateSimilarity(Dictionary<string, double> user1Ratings, Dictionary<string, double> user2Ratings);
}
}

0 comments on commit 8397f46

Please sign in to comment.