public void testStrategy()
        {
            FastIDSet itemIDsFromUser123 = new FastIDSet();
            itemIDsFromUser123.Add(1L);

            FastIDSet itemIDsFromUser456 = new FastIDSet();
            itemIDsFromUser456.Add(1L);
            itemIDsFromUser456.Add(2L);

            List<IPreference> prefs = new List<IPreference>();
            prefs.Add(new GenericPreference(123L, 1L, 1.0f));
            prefs.Add(new GenericPreference(456L, 1L, 1.0f));
            IPreferenceArray preferencesForItem1 = new GenericItemPreferenceArray(prefs);

            var dataModelMock = new DynamicMock(typeof(IDataModel));
            dataModelMock.ExpectAndReturn("GetPreferencesForItem", preferencesForItem1,  (1L));
            dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser123, (123L));
            dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser456, (456L));

            IPreferenceArray prefArrayOfUser123 =
            new GenericUserPreferenceArray( new List<IPreference>() {new GenericPreference(123L, 1L, 1.0f)} );

            ICandidateItemsStrategy strategy = new PreferredItemsNeighborhoodCandidateItemsStrategy();

            //EasyMock.replay(dataModel);

            FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance);
            Assert.AreEqual(1, candidateItems.Count());
            Assert.True(candidateItems.Contains(2L));

            dataModelMock.Verify(); //  EasyMock.verify(dataModel);
        }
コード例 #2
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 public void testGrow()
 {
     FastIDSet set = new FastIDSet(1);
     set.Add(1);
     set.Add(2);
     Assert.True(set.Contains(1));
     Assert.True(set.Contains(2));
 }
        public void testStrategy()
        {
            FastIDSet allItemIDs = new FastIDSet();

            allItemIDs.AddAll(new long[] { 1L, 2L, 3L });

            FastIDSet preferredItemIDs = new FastIDSet(1);

            preferredItemIDs.Add(2L);

            var dataModelMock = new DynamicMock(typeof(IDataModel));

            dataModelMock.ExpectAndReturn("GetNumItems", 3);
            dataModelMock.ExpectAndReturn("GetItemIDs", allItemIDs.GetEnumerator());

            IPreferenceArray prefArrayOfUser123 = new GenericUserPreferenceArray(new List <IPreference>()
            {
                new GenericPreference(123L, 2L, 1.0f)
            });

            ICandidateItemsStrategy strategy = new AllUnknownItemsCandidateItemsStrategy();

            //EasyMock.replay(dataModel);


            FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance);

            Assert.AreEqual(2, candidateItems.Count());
            Assert.True(candidateItems.Contains(1L));
            Assert.True(candidateItems.Contains(3L));

            dataModelMock.Verify();
            //EasyMock.verify(dataModel);
        }
        public void testStrategy()
        {
            FastIDSet allItemIDs = new FastIDSet();
            allItemIDs.AddAll(new long[] { 1L, 2L, 3L });

            FastIDSet preferredItemIDs = new FastIDSet(1);
            preferredItemIDs.Add(2L);

            var dataModelMock = new DynamicMock( typeof( IDataModel ));
            dataModelMock.ExpectAndReturn("GetNumItems", 3);
            dataModelMock.ExpectAndReturn("GetItemIDs", allItemIDs.GetEnumerator());

            IPreferenceArray prefArrayOfUser123 = new GenericUserPreferenceArray( new List<IPreference>() {
            new GenericPreference(123L, 2L, 1.0f) } );

            ICandidateItemsStrategy strategy = new AllUnknownItemsCandidateItemsStrategy();

            //EasyMock.replay(dataModel);

            FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance);
            Assert.AreEqual(2, candidateItems.Count() );
            Assert.True(candidateItems.Contains(1L));
            Assert.True(candidateItems.Contains(3L));

            dataModelMock.Verify();
            //EasyMock.verify(dataModel);
        }
コード例 #5
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 public void testContainsAndAdd()
 {
     FastIDSet set = new FastIDSet();
     Assert.False(set.Contains(1));
     set.Add(1);
     Assert.True(set.Contains(1));
 }
コード例 #6
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 public void testClear()
 {
     FastIDSet set = new FastIDSet();
     set.Add(1);
     set.Clear();
     Assert.AreEqual(0, set.Count());
     Assert.True(set.IsEmpty());
     Assert.False(set.Contains(1));
 }
コード例 #7
0
	/// <summary>
	/// Creates a new <see cref="GenericDataModel"/> from the given users (and their preferences). This
	/// <see cref="IDataModel"/> retains all this information in memory and is effectively immutable.
	/// </summary>
	/// <param name="userData">users to include; (see also <see cref="GenericDataModel.ToDataMap(FastByIDMap, bool)"/>)</param>
	/// <param name="timestamps">timestamps optionally, provided timestamps of preferences as milliseconds since the epoch. User IDs are mapped to maps of item IDs to long timestamps.</param>
	public GenericDataModel(FastByIDMap<IPreferenceArray> userData, FastByIDMap<FastByIDMap<DateTime?>> timestamps) {
		//Preconditions.checkArgument(userData != null, "userData is null");

		this.preferenceFromUsers = userData;
		FastByIDMap<IList<IPreference>> prefsForItems = new FastByIDMap<IList<IPreference>>();
		FastIDSet itemIDSet = new FastIDSet();
		int currentCount = 0;
		float maxPrefValue = float.NegativeInfinity;
		float minPrefValue = float.PositiveInfinity;
		foreach (var entry in preferenceFromUsers.EntrySet()) {
			IPreferenceArray prefs = entry.Value;
			prefs.SortByItem();
			foreach (IPreference preference in prefs) {
			long itemID = preference.GetItemID();
			itemIDSet.Add(itemID);
			var prefsForItem = prefsForItems.Get(itemID);
			if (prefsForItem == null) {
				prefsForItem = new List<IPreference>(2);
				prefsForItems.Put(itemID, prefsForItem);
			}
			prefsForItem.Add(preference);
			float value = preference.GetValue();
			if (value > maxPrefValue) {
				maxPrefValue = value;
			}
			if (value < minPrefValue) {
				minPrefValue = value;
			}
			}
			if (++currentCount % 10000 == 0) {
			log.Info("Processed {0} users", currentCount);
			}
		}
		log.Info("Processed {0} users", currentCount);

		setMinPreference(minPrefValue);
		setMaxPreference(maxPrefValue);

		this.itemIDs = itemIDSet.ToArray();
		itemIDSet = null; // Might help GC -- this is big
		Array.Sort(itemIDs);

		this.preferenceForItems = ToDataMap(prefsForItems, false);

		foreach (var entry in preferenceForItems.EntrySet()) {
			entry.Value.SortByUser();
	}

	this.userIDs = new long[userData.Count()];
	int i = 0;
	foreach (var v in userData.Keys) {
		userIDs[i++] = v;
	}
	Array.Sort(userIDs);

	this.timestamps = timestamps;
	}
 protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, IDataModel dataModel) {
   FastIDSet possibleItemIDs = new FastIDSet(dataModel.GetNumItems());
   var allItemIDs = dataModel.GetItemIDs();
   while (allItemIDs.MoveNext()) {
     possibleItemIDs.Add(allItemIDs.Current);
   }
   possibleItemIDs.RemoveAll(preferredItemIDs);
   return possibleItemIDs;
 }
        public void recommend()
        {
            var dataModelMock              = new DynamicMock(typeof(IDataModel));
            var preferencesFromUserMock    = new DynamicMock(typeof(IPreferenceArray));
            var candidateItemsStrategyMock = new DynamicMock(typeof(ICandidateItemsStrategy));
            var factorizerMock             = new DynamicMock(typeof(IFactorizer));
            var factorization              = new Factorization_recommend_TestMock();

            FastIDSet candidateItems = new FastIDSet();

            candidateItems.Add(5L);
            candidateItems.Add(3L);

            factorizerMock.ExpectAndReturn("Factorize", factorization);

            dataModelMock.ExpectAndReturn("GetPreferencesFromUser", preferencesFromUserMock.MockInstance, (1L));

            candidateItemsStrategyMock.ExpectAndReturn("GetCandidateItems", candidateItems,
                                                       1L, preferencesFromUserMock.MockInstance, dataModelMock.MockInstance);

            //EasyMock.replay(dataModel, candidateItemsStrategy, factorizer, factorization);

            SVDRecommender svdRecommender = new SVDRecommender(
                (IDataModel)dataModelMock.MockInstance,
                (IFactorizer)factorizerMock.MockInstance,
                (ICandidateItemsStrategy)candidateItemsStrategyMock.MockInstance);

            IList <IRecommendedItem> recommendedItems = svdRecommender.Recommend(1L, 5);

            Assert.AreEqual(2, recommendedItems.Count);
            Assert.AreEqual(3L, recommendedItems[0].GetItemID());
            Assert.AreEqual(2.0f, recommendedItems[0].GetValue(), EPSILON);
            Assert.AreEqual(5L, recommendedItems[1].GetItemID());
            Assert.AreEqual(1.0f, recommendedItems[1].GetValue(), EPSILON);

            dataModelMock.Verify();
            candidateItemsStrategyMock.Verify();
            factorizerMock.Verify();

            Assert.AreEqual(2, factorization.getItemFeaturesCallCount);
            Assert.AreEqual(2, factorization.getUserFeaturesCallCount);
            //EasyMock.verify(dataModel, candidateItemsStrategy, factorizer, factorization);
        }
コード例 #10
0
 public virtual long[] AllSimilarItemIDs(long itemID) {
   FastIDSet allSimilarItemIDs = new FastIDSet();
   var allItemIDs = dataModel.GetItemIDs();
   while (allItemIDs.MoveNext()) {
     long possiblySimilarItemID = allItemIDs.Current;
     if (!Double.IsNaN(ItemSimilarity(itemID, possiblySimilarItemID))) {
       allSimilarItemIDs.Add(possiblySimilarItemID);
     }
   }
   return allSimilarItemIDs.ToArray();
 }
        public void recommend()
        {
            var dataModelMock = new DynamicMock( typeof(IDataModel) );
            var preferencesFromUserMock = new DynamicMock( typeof(IPreferenceArray) );
            var candidateItemsStrategyMock = new DynamicMock( typeof(ICandidateItemsStrategy) );
            var factorizerMock = new DynamicMock( typeof(IFactorizer) );
            var factorization = new Factorization_recommend_TestMock();

            FastIDSet candidateItems = new FastIDSet();
            candidateItems.Add(5L);
            candidateItems.Add(3L);

            factorizerMock.ExpectAndReturn("Factorize", factorization);

            dataModelMock.ExpectAndReturn("GetPreferencesFromUser", preferencesFromUserMock.MockInstance, (1L));

            candidateItemsStrategyMock.ExpectAndReturn("GetCandidateItems", candidateItems,
            1L, preferencesFromUserMock.MockInstance, dataModelMock.MockInstance);

            //EasyMock.replay(dataModel, candidateItemsStrategy, factorizer, factorization);

            SVDRecommender svdRecommender = new SVDRecommender(
            (IDataModel)dataModelMock.MockInstance,
            (IFactorizer)factorizerMock.MockInstance,
            (ICandidateItemsStrategy)candidateItemsStrategyMock.MockInstance);

            IList<IRecommendedItem> recommendedItems = svdRecommender.Recommend(1L, 5);
            Assert.AreEqual(2, recommendedItems.Count);
            Assert.AreEqual(3L, recommendedItems[0].GetItemID());
            Assert.AreEqual(2.0f, recommendedItems[0].GetValue(), EPSILON);
            Assert.AreEqual(5L, recommendedItems[1].GetItemID());
            Assert.AreEqual(1.0f, recommendedItems[1].GetValue(), EPSILON);

            dataModelMock.Verify();
            candidateItemsStrategyMock.Verify();
            factorizerMock.Verify();

            Assert.AreEqual(2, factorization.getItemFeaturesCallCount);
            Assert.AreEqual(2, factorization.getUserFeaturesCallCount);
            //EasyMock.verify(dataModel, candidateItemsStrategy, factorizer, factorization);
        }
コード例 #12
0
        protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, IDataModel dataModel)
        {
            FastIDSet possibleItemIDs = new FastIDSet(dataModel.GetNumItems());
            var       allItemIDs      = dataModel.GetItemIDs();

            while (allItemIDs.MoveNext())
            {
                possibleItemIDs.Add(allItemIDs.Current);
            }
            possibleItemIDs.RemoveAll(preferredItemIDs);
            return(possibleItemIDs);
        }
コード例 #13
0
        public override FastIDSet GetItemIDsFromUser(long userID)
        {
            IPreferenceArray prefs = GetPreferencesFromUser(userID);
            int       size         = prefs.Length();
            FastIDSet result       = new FastIDSet(size);

            for (int i = 0; i < size; i++)
            {
                result.Add(prefs.GetItemID(i));
            }
            return(result);
        }
        public void testStrategy()
        {
            FastIDSet itemIDsFromUser123 = new FastIDSet();

            itemIDsFromUser123.Add(1L);

            FastIDSet itemIDsFromUser456 = new FastIDSet();

            itemIDsFromUser456.Add(1L);
            itemIDsFromUser456.Add(2L);

            List <IPreference> prefs = new List <IPreference>();

            prefs.Add(new GenericPreference(123L, 1L, 1.0f));
            prefs.Add(new GenericPreference(456L, 1L, 1.0f));
            IPreferenceArray preferencesForItem1 = new GenericItemPreferenceArray(prefs);

            var dataModelMock = new DynamicMock(typeof(IDataModel));

            dataModelMock.ExpectAndReturn("GetPreferencesForItem", preferencesForItem1, (1L));
            dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser123, (123L));
            dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser456, (456L));

            IPreferenceArray prefArrayOfUser123 =
                new GenericUserPreferenceArray(new List <IPreference>()
            {
                new GenericPreference(123L, 1L, 1.0f)
            });

            ICandidateItemsStrategy strategy = new PreferredItemsNeighborhoodCandidateItemsStrategy();

            //EasyMock.replay(dataModel);

            FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance);

            Assert.AreEqual(1, candidateItems.Count());
            Assert.True(candidateItems.Contains(2L));

            dataModelMock.Verify(); //  EasyMock.verify(dataModel);
        }
コード例 #15
0
        /// Sets temporary preferences for a given anonymous user.
        public void SetTempPrefs(IPreferenceArray prefs, long anonymousUserID)
        {
            //Preconditions.checkArgument(prefs != null && prefs.Length() > 0, "prefs is null or empty");

            this.tempPrefs[anonymousUserID] = prefs;
            FastIDSet userPrefItemIDs = new FastIDSet();

            for (int i = 0; i < prefs.Length(); i++)
            {
                userPrefItemIDs.Add(prefs.GetItemID(i));
            }

            this.prefItemIDs[anonymousUserID] = userPrefItemIDs;
        }
コード例 #16
0
 public FastIDSet GetRelevantItemsIDs(long userID,
                                      int at,
                                      double relevanceThreshold,
                                      IDataModel dataModel) {
   IPreferenceArray prefs = dataModel.GetPreferencesFromUser(userID);
   FastIDSet relevantItemIDs = new FastIDSet(at);
   prefs.SortByValueReversed();
   for (int i = 0; i < prefs.Length() && relevantItemIDs.Count() < at; i++) {
     if (prefs.GetValue(i) >= relevanceThreshold) {
       relevantItemIDs.Add(prefs.GetItemID(i));
     }
   }
   return relevantItemIDs;
 }
コード例 #17
0
        private static long setBits(FastIDSet modelSet, IList <IRecommendedItem> items, int max)
        {
            long maxItem = -1;

            for (int i = 0; i < items.Count && i < max; i++)
            {
                long itemID = items[i].GetItemID();
                modelSet.Add(itemID);
                if (itemID > maxItem)
                {
                    maxItem = itemID;
                }
            }
            return(maxItem);
        }
コード例 #18
0
        private static long setBits(FastIDSet modelSet, IPreferenceArray prefs, int max)
        {
            long maxItem = -1;

            for (int i = 0; i < prefs.Length() && i < max; i++)
            {
                long itemID = prefs.GetItemID(i);
                modelSet.Add(itemID);
                if (itemID > maxItem)
                {
                    maxItem = itemID;
                }
            }
            return(maxItem);
        }
コード例 #19
0
        public virtual long[] AllSimilarItemIDs(long itemID)
        {
            FastIDSet allSimilarItemIDs = new FastIDSet();
            var       allItemIDs        = dataModel.GetItemIDs();

            while (allItemIDs.MoveNext())
            {
                long possiblySimilarItemID = allItemIDs.Current;
                if (!Double.IsNaN(ItemSimilarity(itemID, possiblySimilarItemID)))
                {
                    allSimilarItemIDs.Add(possiblySimilarItemID);
                }
            }
            return(allSimilarItemIDs.ToArray());
        }
コード例 #20
0
 private void addSomeOf(FastIDSet possibleItemIDs, FastIDSet itemIDs)
 {
     if (itemIDs.Count() > maxItemsPerUser)
     {
         var it =
             new SamplinglongPrimitiveIterator(itemIDs.GetEnumerator(), (double)maxItemsPerUser / itemIDs.Count());
         while (it.MoveNext())
         {
             possibleItemIDs.Add(it.Current);
         }
     }
     else
     {
         possibleItemIDs.AddAll(itemIDs);
     }
 }
コード例 #21
0
ファイル: TasteTestCase.cs プロジェクト: nreco/recommender
 public static IDataModel getBooleanDataModel(long[] userIDs, bool[][] prefs)
 {
     FastByIDMap<FastIDSet> result = new FastByIDMap<FastIDSet>();
     for (int i = 0; i < userIDs.Length; i++) {
       FastIDSet prefsSet = new FastIDSet();
       for (int j = 0; j < prefs[i].Length; j++) {
     if (prefs[i][j]) {
       prefsSet.Add(j);
     }
       }
       if (!prefsSet.IsEmpty()) {
     result.Put(userIDs[i], prefsSet);
       }
     }
     return new GenericBooleanPrefDataModel(result);
 }
コード例 #22
0
        public static FastByIDMap <FastIDSet> toDataMap(FastByIDMap <IPreferenceArray> data)
        {
            var res = new FastByIDMap <FastIDSet>(data.Count());

            foreach (var entry in data.EntrySet())
            {
                IPreferenceArray prefArray = entry.Value;
                int       size             = prefArray.Length();
                FastIDSet itemIDs          = new FastIDSet(size);
                for (int i = 0; i < size; i++)
                {
                    itemIDs.Add(prefArray.GetItemID(i));
                }

                res.Put(entry.Key, itemIDs);
            }
            return(res);
        }
コード例 #23
0
        public FastIDSet GetRelevantItemsIDs(long userID,
                                             int at,
                                             double relevanceThreshold,
                                             IDataModel dataModel)
        {
            IPreferenceArray prefs           = dataModel.GetPreferencesFromUser(userID);
            FastIDSet        relevantItemIDs = new FastIDSet(at);

            prefs.SortByValueReversed();
            for (int i = 0; i < prefs.Length() && relevantItemIDs.Count() < at; i++)
            {
                if (prefs.GetValue(i) >= relevanceThreshold)
                {
                    relevantItemIDs.Add(prefs.GetItemID(i));
                }
            }
            return(relevantItemIDs);
        }
コード例 #24
0
        public List <IRecommendedItem> RecommendedBecause(long userID, long itemID, int howMany)
        {
            //Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");

            IDataModel model = GetDataModel();

            TopItems.IEstimator <long> estimator = new RecommendedBecauseEstimator(this, userID, itemID);

            IPreferenceArray prefs = model.GetPreferencesFromUser(userID);
            int       size         = prefs.Length();
            FastIDSet allUserItems = new FastIDSet(size);

            for (int i = 0; i < size; i++)
            {
                allUserItems.Add(prefs.GetItemID(i));
            }
            allUserItems.Remove(itemID);

            return(TopItems.GetTopItems(howMany, allUserItems.GetEnumerator(), null, estimator));
        }
コード例 #25
0
        public static IDataModel getBooleanDataModel(long[] userIDs, bool[][] prefs)
        {
            FastByIDMap <FastIDSet> result = new FastByIDMap <FastIDSet>();

            for (int i = 0; i < userIDs.Length; i++)
            {
                FastIDSet prefsSet = new FastIDSet();
                for (int j = 0; j < prefs[i].Length; j++)
                {
                    if (prefs[i][j])
                    {
                        prefsSet.Add(j);
                    }
                }
                if (!prefsSet.IsEmpty())
                {
                    result.Put(userIDs[i], prefsSet);
                }
            }
            return(new GenericBooleanPrefDataModel(result));
        }
コード例 #26
0
        public override long[] GetUserNeighborhood(long userID)
        {
            IDataModel dataModel     = getDataModel();
            FastIDSet  neighborhood  = new FastIDSet();
            var        usersIterable = SamplinglongPrimitiveIterator.MaybeWrapIterator(dataModel
                                                                                       .GetUserIDs(), getSamplingRate());
            IUserSimilarity userSimilarityImpl = getUserSimilarity();

            while (usersIterable.MoveNext())
            {
                long otherUserID = usersIterable.Current;
                if (userID != otherUserID)
                {
                    double theSimilarity = userSimilarityImpl.UserSimilarity(userID, otherUserID);
                    if (!Double.IsNaN(theSimilarity) && theSimilarity >= threshold)
                    {
                        neighborhood.Add(otherUserID);
                    }
                }
            }

            return(neighborhood.ToArray());
        }
コード例 #27
0
 public override long[] GetUserNeighborhood(long userID) {
   
   IDataModel dataModel = getDataModel();
   FastIDSet neighborhood = new FastIDSet();
   var usersIterable = SamplinglongPrimitiveIterator.MaybeWrapIterator(dataModel
       .GetUserIDs(), getSamplingRate());
   IUserSimilarity userSimilarityImpl = getUserSimilarity();
   
   while (usersIterable.MoveNext()) {
     long otherUserID = usersIterable.Current;
     if (userID != otherUserID) {
       double theSimilarity = userSimilarityImpl.UserSimilarity(userID, otherUserID);
       if (!Double.IsNaN(theSimilarity) && theSimilarity >= threshold) {
         neighborhood.Add(otherUserID);
       }
     }
   }
   
   return neighborhood.ToArray();
 }
コード例 #28
0
 private static long setBits(FastIDSet modelSet, IList<IRecommendedItem> items, int max) {
   long maxItem = -1;
   for (int i = 0; i < items.Count && i < max; i++) {
     long itemID = items[i].GetItemID();
     modelSet.Add(itemID);
     if (itemID > maxItem) {
       maxItem = itemID;
     }
   }
   return maxItem;
 }
コード例 #29
0
 private void addSomeOf(FastIDSet possibleItemIDs, FastIDSet itemIDs) {
   if (itemIDs.Count() > maxItemsPerUser) {
     var it =
         new SamplinglongPrimitiveIterator(itemIDs.GetEnumerator(), (double) maxItemsPerUser / itemIDs.Count() );
     while (it.MoveNext()) {
       possibleItemIDs.Add(it.Current);
     }
   } else {
     possibleItemIDs.AddAll(itemIDs);
   }
 }
コード例 #30
0
  public static FastByIDMap<FastIDSet> toDataMap(FastByIDMap<IPreferenceArray> data) {
    var res = new FastByIDMap<FastIDSet>( data.Count() );
	foreach (var entry in data.EntrySet()) {
      IPreferenceArray prefArray = entry.Value;
      int size = prefArray.Length();
      FastIDSet itemIDs = new FastIDSet(size);
      for (int i = 0; i < size; i++) {
        itemIDs.Add(prefArray.GetItemID(i));
      }
	 
	  res.Put( entry.Key, itemIDs );
    }
	return res;
  }
コード例 #31
0
 private static long setBits(FastIDSet modelSet, IPreferenceArray prefs, int max) {
   long maxItem = -1;
   for (int i = 0; i < prefs.Length() && i < max; i++) {
     long itemID = prefs.GetItemID(i);
     modelSet.Add(itemID);
     if (itemID > maxItem) {
       maxItem = itemID;
     }
   }
   return maxItem;
 }
コード例 #32
0
 public override FastIDSet GetItemIDsFromUser(long userID) {
   IPreferenceArray prefs = GetPreferencesFromUser(userID);
   int size = prefs.Length();
   FastIDSet result = new FastIDSet(size);
   for (int i = 0; i < size; i++) {
     result.Add(prefs.GetItemID(i));
   }
   return result;
 }
コード例 #33
0
        /// <summary>
        /// Creates a new <see cref="GenericDataModel"/> from the given users (and their preferences). This
        /// <see cref="IDataModel"/> retains all this information in memory and is effectively immutable.
        /// </summary>
        /// <param name="userData">users to include; (see also <see cref="GenericDataModel.ToDataMap(FastByIDMap, bool)"/>)</param>
        /// <param name="timestamps">timestamps optionally, provided timestamps of preferences as milliseconds since the epoch. User IDs are mapped to maps of item IDs to long timestamps.</param>
        public GenericDataModel(FastByIDMap <IPreferenceArray> userData, FastByIDMap <FastByIDMap <DateTime?> > timestamps)
        {
            //Preconditions.checkArgument(userData != null, "userData is null");

            this.preferenceFromUsers = userData;
            FastByIDMap <IList <IPreference> > prefsForItems = new FastByIDMap <IList <IPreference> >();
            FastIDSet itemIDSet    = new FastIDSet();
            int       currentCount = 0;
            float     maxPrefValue = float.NegativeInfinity;
            float     minPrefValue = float.PositiveInfinity;

            foreach (var entry in preferenceFromUsers.EntrySet())
            {
                IPreferenceArray prefs = entry.Value;
                prefs.SortByItem();
                foreach (IPreference preference in prefs)
                {
                    long itemID = preference.GetItemID();
                    itemIDSet.Add(itemID);
                    var prefsForItem = prefsForItems.Get(itemID);
                    if (prefsForItem == null)
                    {
                        prefsForItem = new List <IPreference>(2);
                        prefsForItems.Put(itemID, prefsForItem);
                    }
                    prefsForItem.Add(preference);
                    float value = preference.GetValue();
                    if (value > maxPrefValue)
                    {
                        maxPrefValue = value;
                    }
                    if (value < minPrefValue)
                    {
                        minPrefValue = value;
                    }
                }
                if (++currentCount % 10000 == 0)
                {
                    log.Info("Processed {0} users", currentCount);
                }
            }
            log.Info("Processed {0} users", currentCount);

            setMinPreference(minPrefValue);
            setMaxPreference(maxPrefValue);

            this.itemIDs = itemIDSet.ToArray();
            itemIDSet    = null;  // Might help GC -- this is big
            Array.Sort(itemIDs);

            this.preferenceForItems = ToDataMap(prefsForItems, false);

            foreach (var entry in preferenceForItems.EntrySet())
            {
                entry.Value.SortByUser();
            }

            this.userIDs = new long[userData.Count()];
            int i = 0;

            foreach (var v in userData.Keys)
            {
                userIDs[i++] = v;
            }
            Array.Sort(userIDs);

            this.timestamps = timestamps;
        }
   /// Sets temporary preferences for a given anonymous user.
  public void SetTempPrefs(IPreferenceArray prefs, long anonymousUserID) {
    //Preconditions.checkArgument(prefs != null && prefs.Length() > 0, "prefs is null or empty");

    this.tempPrefs[anonymousUserID] = prefs;
    FastIDSet userPrefItemIDs = new FastIDSet();

    for (int i = 0; i < prefs.Length(); i++) {
      userPrefItemIDs.Add(prefs.GetItemID(i));
    }

    this.prefItemIDs[anonymousUserID] = userPrefItemIDs;
  }
コード例 #35
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 public void testReservedValues()
 {
     FastIDSet set = new FastIDSet();
     try {
       set.Add(Int64.MinValue);
       Assert.Fail("Should have thrown IllegalArgumentException");
     } catch (ArgumentException iae) { //IllegalArgumentException
       // good
     }
     Assert.False(set.Contains(Int64.MinValue));
     try {
       set.Add(long.MaxValue);
       Assert.Fail("Should have thrown IllegalArgumentException");
     } catch (ArgumentException iae) {
       // good
     }
     Assert.False(set.Contains(long.MaxValue));
 }
コード例 #36
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 public void testSizeEmpty()
 {
     FastIDSet set = new FastIDSet();
     Assert.AreEqual(0, set.Count());
     Assert.True(set.IsEmpty());
     set.Add(1);
     Assert.AreEqual(1, set.Count());
     Assert.False(set.IsEmpty());
     set.Remove(1);
     Assert.AreEqual(0, set.Count());
     Assert.True(set.IsEmpty());
 }
コード例 #37
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 private static FastIDSet buildTestFastSet()
 {
     FastIDSet set = new FastIDSet();
     set.Add(1);
     set.Add(2);
     set.Add(3);
     return set;
 }
コード例 #38
0
ファイル: FastIDSetTest.cs プロジェクト: nreco/recommender
 public void testVersusHashSet()
 {
     FastIDSet actual = new FastIDSet(1);
     var expected = new HashSet<int>(); //1000000
     var r = RandomUtils.getRandom();
     for (int i = 0; i < 1000000; i++) {
       double d = r.nextDouble();
       var key = r.nextInt(100);
       if (d < 0.4) {
     Assert.AreEqual(expected.Contains(key), actual.Contains(key));
       } else {
     if (d < 0.7) {
       Assert.AreEqual(expected.Add(key), actual.Add(key));
     } else {
       Assert.AreEqual(expected.Remove(key), actual.Remove(key));
     }
     Assert.AreEqual(expected.Count, actual.Count() );
     Assert.AreEqual(expected.Count==0, actual.IsEmpty());
       }
     }
 }