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 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());
        }
        /// <p>
        /// Creates a new {@link GenericDataModel} from the given users (and their preferences). This
        /// {@link DataModel} retains all this information in memory and is effectively immutable.
        /// </p>
        ///
        /// @param userData users to include
        /// @param timestamps optionally, provided timestamps of preferences as milliseconds since the epoch.
        ///  User IDs are mapped to maps of item IDs to long timestamps.
        public GenericBooleanPrefDataModel(FastByIDMap <FastIDSet> userData, FastByIDMap <FastByIDMap <DateTime?> > timestamps)
        {
            //Preconditions.checkArgument(userData != null, "userData is null");

            this.preferenceFromUsers = userData;
            this.preferenceForItems  = new FastByIDMap <FastIDSet>();
            FastIDSet itemIDSet = new FastIDSet();

            foreach (var entry in preferenceFromUsers.EntrySet())
            {
                long      userID   = entry.Key;
                FastIDSet itemIDs1 = entry.Value;
                itemIDSet.AddAll(itemIDs1);
                var it = itemIDs1.GetEnumerator();
                while (it.MoveNext())
                {
                    long      itemID   = it.Current;
                    FastIDSet userIDs1 = preferenceForItems.Get(itemID);
                    if (userIDs1 == null)
                    {
                        userIDs1 = new FastIDSet(2);
                        preferenceForItems.Put(itemID, userIDs1);
                    }
                    userIDs1.Add(userID);
                }
            }

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

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

            while (it1.MoveNext())
            {
                userIDs[i++] = it1.Current;
            }
            Array.Sort(userIDs);

            this.timestamps = timestamps;
        }
        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());
        }
   /// <p>
   /// Creates a new {@link GenericDataModel} from the given users (and their preferences). This
   /// {@link DataModel} retains all this information in memory and is effectively immutable.
   /// </p>
   ///
   /// @param userData users to include
   /// @param timestamps optionally, provided timestamps of preferences as milliseconds since the epoch.
   ///  User IDs are mapped to maps of item IDs to long timestamps.
  public GenericBooleanPrefDataModel(FastByIDMap<FastIDSet> userData, FastByIDMap<FastByIDMap<DateTime?>> timestamps) {
    //Preconditions.checkArgument(userData != null, "userData is null");

    this.preferenceFromUsers = userData;
    this.preferenceForItems = new FastByIDMap<FastIDSet>();
    FastIDSet itemIDSet = new FastIDSet();
    foreach (var entry in preferenceFromUsers.EntrySet()) {
      long userID = entry.Key;
      FastIDSet itemIDs1 = entry.Value;
      itemIDSet.AddAll(itemIDs1);
      var it = itemIDs1.GetEnumerator();
      while (it.MoveNext()) {
        long itemID = it.Current;
        FastIDSet userIDs1 = preferenceForItems.Get(itemID);
        if (userIDs1 == null) {
          userIDs1 = new FastIDSet(2);
          preferenceForItems.Put(itemID, userIDs1);
        }
        userIDs1.Add(userID);
      }
    }

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

    this.userIDs = new long[userData.Count()];
    int i = 0;
    var it1 = userData.Keys.GetEnumerator();
    while (it1.MoveNext()) {
      userIDs[i++] = it1.Current;
    }
    Array.Sort(userIDs);

    this.timestamps = timestamps;
  }
        /// <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;
        }
	/// <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;
	}
Exemple #8
0
        public long[] AllSimilarItemIDs(long itemID)
        {
            FastIDSet similarItemIDs = similarItemIDsIndex.Get(itemID);

            return(similarItemIDs != null?similarItemIDs.ToArray() : NO_IDS);
        }
 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();
 }