public void TestHowMany()
 {
     List<User> users = new List<User>(3);
     users.Add(GetUser("test1", 0.1, 0.2));
     users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
     users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
     users.Add(GetUser("test4", 0.1, 0.4, 0.5, 0.8, 0.9, 1.0));
     users.Add(GetUser("test5", 0.2, 0.3, 0.6, 0.7, 0.1, 0.2));
     DataModel dataModel = new GenericDataModel(users);
     IList<GenericItemCorrelation.ItemItemCorrelation> correlations =
        new List<GenericItemCorrelation.ItemItemCorrelation>(6);
     for (int i = 0; i < 6; i++)
     {
         for (int j = i + 1; j < 6; j++)
         {
             correlations.Add(
                 new GenericItemCorrelation.ItemItemCorrelation(new GenericItem<String>(i.ToString()),
                                                                new GenericItem<String>(j.ToString()),
                                                                1.0 / (1.0 + (double)i + (double)j)));
         }
     }
     ItemCorrelation correlation = new GenericItemCorrelation(correlations);
     Recommender recommender = new GenericItemBasedRecommender(dataModel, correlation);
     IList<RecommendedItem> fewRecommended = recommender.Recommend("test1", 2);
     IList<RecommendedItem> moreRecommended = recommender.Recommend("test1", 4);
     for (int i = 0; i < fewRecommended.Count; i++)
     {
         Assert.AreEqual(fewRecommended[i].Item, moreRecommended[i].Item);
     }
 }
示例#2
0
        public void TestRescorer()
        {
            List <User> users = new List <User>(3);

            users.Add(GetUser("test1", 0.1, 0.2));
            users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
            users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
            DataModel dataModel = new GenericDataModel(users);
            Item      item1     = new GenericItem <String>("0");
            Item      item2     = new GenericItem <String>("1");
            Item      item3     = new GenericItem <String>("2");
            Item      item4     = new GenericItem <String>("3");
            ICollection <GenericItemCorrelation.ItemItemCorrelation> correlations =
                new List <GenericItemCorrelation.ItemItemCorrelation>(6);

            correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item1, item2, 1.0));
            correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item1, item3, 0.5));
            correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item1, item4, 0.2));
            correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item2, item3, 0.7));
            correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item2, item4, 0.5));
            correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item3, item4, 0.9));
            ItemCorrelation         correlation         = new GenericItemCorrelation(correlations);
            Recommender             recommender         = new GenericItemBasedRecommender(dataModel, correlation);
            IList <RecommendedItem> originalRecommended = recommender.Recommend("test1", 2);
            IList <RecommendedItem> rescoredRecommended =
                recommender.Recommend("test1", 2, new ReversingRescorer <Item>());

            Assert.IsNotNull(originalRecommended);
            Assert.IsNotNull(rescoredRecommended);
            Assert.AreEqual(2, originalRecommended.Count);
            Assert.AreEqual(2, rescoredRecommended.Count);
            Assert.AreEqual(originalRecommended[0].Item, rescoredRecommended[1].Item);
            Assert.AreEqual(originalRecommended[1].Item, rescoredRecommended[0].Item);
        }
示例#3
0
        public void TestHowMany()
        {
            List <User> users = new List <User>(3);

            users.Add(GetUser("test1", 0.1, 0.2));
            users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
            users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
            users.Add(GetUser("test4", 0.1, 0.4, 0.5, 0.8, 0.9, 1.0));
            users.Add(GetUser("test5", 0.2, 0.3, 0.6, 0.7, 0.1, 0.2));
            DataModel dataModel = new GenericDataModel(users);
            IList <GenericItemCorrelation.ItemItemCorrelation> correlations =
                new List <GenericItemCorrelation.ItemItemCorrelation>(6);

            for (int i = 0; i < 6; i++)
            {
                for (int j = i + 1; j < 6; j++)
                {
                    correlations.Add(
                        new GenericItemCorrelation.ItemItemCorrelation(new GenericItem <String>(i.ToString()),
                                                                       new GenericItem <String>(j.ToString()),
                                                                       1.0 / (1.0 + (double)i + (double)j)));
                }
            }
            ItemCorrelation         correlation     = new GenericItemCorrelation(correlations);
            Recommender             recommender     = new GenericItemBasedRecommender(dataModel, correlation);
            IList <RecommendedItem> fewRecommended  = recommender.Recommend("test1", 2);
            IList <RecommendedItem> moreRecommended = recommender.Recommend("test1", 4);

            for (int i = 0; i < fewRecommended.Count; i++)
            {
                Assert.AreEqual(fewRecommended[i].Item, moreRecommended[i].Item);
            }
        }
        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);
        }
 public void TestRescorer()
 {
     List<User> users = new List<User>(3);
     users.Add(GetUser("test1", 0.1, 0.2));
     users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
     users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
     DataModel dataModel = new GenericDataModel(users);
     Item item1 = new GenericItem<String>("0");
     Item item2 = new GenericItem<String>("1");
     Item item3 = new GenericItem<String>("2");
     Item item4 = new GenericItem<String>("3");
     ICollection<GenericItemCorrelation.ItemItemCorrelation> correlations =
        new List<GenericItemCorrelation.ItemItemCorrelation>(6);
     correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item1, item2, 1.0));
     correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item1, item3, 0.5));
     correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item1, item4, 0.2));
     correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item2, item3, 0.7));
     correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item2, item4, 0.5));
     correlations.Add(new GenericItemCorrelation.ItemItemCorrelation(item3, item4, 0.9));
     ItemCorrelation correlation = new GenericItemCorrelation(correlations);
     Recommender recommender = new GenericItemBasedRecommender(dataModel, correlation);
     IList<RecommendedItem> originalRecommended = recommender.Recommend("test1", 2);
     IList<RecommendedItem> rescoredRecommended =
        recommender.Recommend("test1", 2, new ReversingRescorer<Item>());
     Assert.IsNotNull(originalRecommended);
     Assert.IsNotNull(rescoredRecommended);
     Assert.AreEqual(2, originalRecommended.Count);
     Assert.AreEqual(2, rescoredRecommended.Count);
     Assert.AreEqual(originalRecommended[0].Item, rescoredRecommended[1].Item);
     Assert.AreEqual(originalRecommended[1].Item, rescoredRecommended[0].Item);
 }
        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());
            }
        }