private UsersSimilarity ComputeSimilarityBetweenUsers(int userId, int comparedUserId, DistanceSimilarityEnum similarityDistance) { var similarity = _usersSelector.SelectMutualAndUniqueBooksForUsers(userId, comparedUserId); if (similarity == null) { return(null); } switch (similarityDistance) { case DistanceSimilarityEnum.CosineSimilarity: similarity.Similarity = GetCosineDistance(similarity.UserRatesForMutualBooks, similarity.ComparedUserRatesForMutualBooks); break; case DistanceSimilarityEnum.PearsonSimilarity: similarity.Similarity = GetPearsonCorrelationSimilarity(similarity.UserRatesForMutualBooks, similarity.ComparedUserRatesForMutualBooks); break; default: throw new ArgumentOutOfRangeException(); } similarity.SimilarityType = (int)similarityDistance; return(similarity); }
public void GetNearestNeighbors_WhenBookRatesAreDifferent_ShouldReturnMinSimilarity( DistanceSimilarityEnum distance, double expected) { _settings.SimilarityDistance = distance; var similarity = new UsersSimilarity { ComparedUserRatesForMutualBooks = _helpers.CreateBookScores(2, new[] { "1", "2", "3" }, new short[] { 1, 1, 1 }), UserRatesForMutualBooks = _helpers.CreateBookScores(2, new[] { "1", "2", "3" }, new short[] { 10, 10, 10 }), }; _usersSelector.SelectMutualAndUniqueBooksForUsers(1, 2).Returns(similarity); var actual = _sut.GetNearestNeighbors(1, new List <int>() { 2 }, _settings).FirstOrDefault(); actual.ShouldNotBeNull(); actual.Similarity.Value.ShouldBe(expected, 2); }
public void GetNearestNeighbors_ShouldReturnTwoMostSimilarUsers(DistanceSimilarityEnum distance) { _settings.SimilarityDistance = distance; var similarity_1 = new UsersSimilarity { ComparedUserRatesForMutualBooks = _helpers.CreateBookScores(2, new[] { "1", "2", "3" }, new short[] { 1, 2, 2 }), UserRatesForMutualBooks = _helpers.CreateBookScores(1, new[] { "1", "2", "3" }, new short[] { 1, 2, 2 }), ComparedUserId = 2 }; var similarity_2 = new UsersSimilarity { ComparedUserRatesForMutualBooks = _helpers.CreateBookScores(3, new[] { "1", "2", "3" }, new short[] { 1, 2, 2 }), UserRatesForMutualBooks = _helpers.CreateBookScores(1, new[] { "1", "2", "3" }, new short[] { 1, 4, 2 }), ComparedUserId = 3 }; var similarity_3 = new UsersSimilarity { ComparedUserRatesForMutualBooks = _helpers.CreateBookScores(4, new[] { "1", "2", "3" }, new short[] { 1, 2, 2 }), UserRatesForMutualBooks = _helpers.CreateBookScores(1, new[] { "1", "2", "3" }, new short[] { 10, 5, 3 }), ComparedUserId = 4 }; _usersSelector.SelectMutualAndUniqueBooksForUsers(1, 2).Returns(similarity_1); _usersSelector.SelectMutualAndUniqueBooksForUsers(1, 3).Returns(similarity_2); _usersSelector.SelectMutualAndUniqueBooksForUsers(1, 4).Returns(similarity_3); var actual = _sut.GetNearestNeighbors(1, new List <int>() { 2, 3, 4 }, _settings); actual.ShouldNotBeNull(); actual.Select(x => x.ComparedUserId).ShouldBe(new[] { 2, 3 }); actual.Count.ShouldBe(2); }