public void testRescorer() { IDataModel dataModel = getDataModel( new long[] { 1, 2, 3 }, new Double?[][] { new double?[] { 0.1, 0.2 }, new double?[] { 0.2, 0.3, 0.3, 0.6 }, new double?[] { 0.4, 0.4, 0.5, 0.9 }, }); var similarities = new List <GenericItemSimilarity.ItemItemSimilarity>(); similarities.Add(new GenericItemSimilarity.ItemItemSimilarity(0, 1, 1.0)); similarities.Add(new GenericItemSimilarity.ItemItemSimilarity(0, 2, 0.5)); similarities.Add(new GenericItemSimilarity.ItemItemSimilarity(0, 3, 0.2)); similarities.Add(new GenericItemSimilarity.ItemItemSimilarity(1, 2, 0.7)); similarities.Add(new GenericItemSimilarity.ItemItemSimilarity(1, 3, 0.5)); similarities.Add(new GenericItemSimilarity.ItemItemSimilarity(2, 3, 0.9)); IItemSimilarity similarity = new GenericItemSimilarity(similarities); IRecommender recommender = new GenericItemBasedRecommender(dataModel, similarity); IList <IRecommendedItem> originalRecommended = recommender.Recommend(1, 2); IList <IRecommendedItem> rescoredRecommended = recommender.Recommend(1, 2, new ReversingRescorer <long>()); Assert.NotNull(originalRecommended); Assert.NotNull(rescoredRecommended); Assert.AreEqual(2, originalRecommended.Count); Assert.AreEqual(2, rescoredRecommended.Count); Assert.AreEqual(originalRecommended[0].GetItemID(), rescoredRecommended[1].GetItemID()); Assert.AreEqual(originalRecommended[1].GetItemID(), rescoredRecommended[0].GetItemID()); }
public void testHowMany() { IDataModel dataModel = getDataModel( new long[] { 1, 2, 3, 4, 5 }, new Double?[][] { new double?[] { 0.1, 0.2 }, new double?[] { 0.2, 0.3, 0.3, 0.6 }, new double?[] { 0.4, 0.4, 0.5, 0.9 }, new double?[] { 0.1, 0.4, 0.5, 0.8, 0.9, 1.0 }, new double?[] { 0.2, 0.3, 0.6, 0.7, 0.1, 0.2 }, }); var similarities = new List <GenericItemSimilarity.ItemItemSimilarity>(); for (int i = 0; i < 6; i++) { for (int j = i + 1; j < 6; j++) { similarities.Add( new GenericItemSimilarity.ItemItemSimilarity(i, j, 1.0 / (1.0 + i + j))); } } IItemSimilarity similarity = new GenericItemSimilarity(similarities); IRecommender recommender = new GenericItemBasedRecommender(dataModel, similarity); IList <IRecommendedItem> fewRecommended = recommender.Recommend(1, 2); IList <IRecommendedItem> moreRecommended = recommender.Recommend(1, 4); for (int i = 0; i < fewRecommended.Count; i++) { Assert.AreEqual(fewRecommended[i].GetItemID(), moreRecommended[i].GetItemID()); } recommender.Refresh(null); for (int i = 0; i < fewRecommended.Count; i++) { Assert.AreEqual(fewRecommended[i].GetItemID(), moreRecommended[i].GetItemID()); } }
public void preferencesFetchedOnlyOnce() { var dataModelMock = new DynamicMock(typeof(IDataModel)); var itemSimilarityMock = new DynamicMock(typeof(IItemSimilarity)); var candidateItemsStrategyMock = new DynamicMock(typeof(ICandidateItemsStrategy)); var mostSimilarItemsCandidateItemsStrategyMock = new DynamicMock(typeof(IMostSimilarItemsCandidateItemsStrategy)); IPreferenceArray preferencesFromUser = new GenericUserPreferenceArray( new List <IPreference>() { new GenericPreference(1L, 1L, 5.0f), new GenericPreference(1L, 2L, 4.0f) }); dataModelMock.ExpectAndReturn("GetMinPreference", float.NaN); dataModelMock.ExpectAndReturn("GetMaxPreference", float.NaN); dataModelMock.ExpectAndReturn("GetPreferencesFromUser", preferencesFromUser, 1L); var dataModel = (IDataModel)dataModelMock.MockInstance; candidateItemsStrategyMock.ExpectAndReturn("GetCandidateItems", new FastIDSet(new long[] { 3L, 4L }), 1L, preferencesFromUser, dataModel); itemSimilarityMock.ExpectAndReturn("ItemSimilarities", new double[] { 0.5, 0.3 }, 3L, preferencesFromUser.GetIDs()); itemSimilarityMock.ExpectAndReturn("ItemSimilarities", new double[] { 0.4, 0.1 }, 4L, preferencesFromUser.GetIDs()); //EasyMock.replay(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); IRecommender recommender = new GenericItemBasedRecommender((IDataModel)dataModel, (IItemSimilarity)itemSimilarityMock.MockInstance, (ICandidateItemsStrategy)candidateItemsStrategyMock.MockInstance, (IMostSimilarItemsCandidateItemsStrategy)mostSimilarItemsCandidateItemsStrategyMock.MockInstance); recommender.Recommend(1L, 3); dataModelMock.Verify(); itemSimilarityMock.Verify(); candidateItemsStrategyMock.Verify(); mostSimilarItemsCandidateItemsStrategyMock.Verify(); //EasyMock.verify(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); }