private static PreferenceArray cloneAndMergeInto(PreferenceArray delegatePrefs, long itemID, long newUserID, float value) { int num4; int num = (delegatePrefs == null) ? 0 : delegatePrefs.length(); int size = num + 1; PreferenceArray array = new GenericItemPreferenceArray(size); array.setItemID(0, itemID); int i = 0; while ((i < num) && (newUserID > delegatePrefs.getUserID(i))) { i++; } for (num4 = 0; num4 < i; num4++) { array.setUserID(num4, delegatePrefs.getUserID(num4)); array.setValue(num4, delegatePrefs.getValue(num4)); } array.setUserID(i, newUserID); array.setValue(i, value); for (num4 = i + 1; num4 < size; num4++) { array.setUserID(num4, delegatePrefs.getUserID(num4 - 1)); array.setValue(num4, delegatePrefs.getValue(num4 - 1)); } return(array); }
public override int getNumUsersWithPreferenceFor(long itemID1, long itemID2) { PreferenceArray array = this.preferenceForItems.get(itemID1); if (array == null) { return(0); } PreferenceArray array2 = this.preferenceForItems.get(itemID2); if (array2 == null) { return(0); } int num = array.length(); int num2 = array2.length(); int num3 = 0; int i = 0; int num5 = 0; long num6 = array.getUserID(0); long num7 = array2.getUserID(0); while (true) { if (num6 < num7) { if (++i == num) { return(num3); } num6 = array.getUserID(i); } else if (num6 > num7) { if (++num5 == num2) { return(num3); } num7 = array2.getUserID(num5); } else { num3++; if ((++i == num) || (++num5 == num2)) { return(num3); } num6 = array.getUserID(i); num7 = array2.getUserID(num5); } } }
protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, DataModel dataModel) { FastIDSet set = new FastIDSet(); foreach (long num in preferredItemIDs) { PreferenceArray array = dataModel.getPreferencesForItem(num); int num2 = array.length(); for (int i = 0; i < num2; i++) { set.addAll(dataModel.getItemIDsFromUser(array.getUserID(i))); } } set.removeAll(preferredItemIDs); return(set); }
protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, DataModel dataModel) { IEnumerator <long> enumerator = ((IEnumerable <long>)preferredItemIDs).GetEnumerator(); if (preferredItemIDs.Length > this.maxItems) { double samplingRate = ((double)this.maxItems) / ((double)preferredItemIDs.Length); log.info("preferredItemIDs.Length {0}, samplingRate {1}", new object[] { preferredItemIDs.Length, samplingRate }); enumerator = new SamplingLongPrimitiveIterator(enumerator, samplingRate); } FastIDSet possibleItemIDs = new FastIDSet(); while (enumerator.MoveNext()) { long current = enumerator.Current; PreferenceArray array = dataModel.getPreferencesForItem(current); int num3 = array.length(); if (num3 > this.maxUsersPerItem) { FixedSizeSamplingIterator <Preference> iterator = new FixedSizeSamplingIterator <Preference>(this.maxUsersPerItem, array.GetEnumerator()); while (iterator.MoveNext()) { this.addSomeOf(possibleItemIDs, dataModel.getItemIDsFromUser(iterator.Current.getUserID())); } } else { for (int i = 0; i < num3; i++) { this.addSomeOf(possibleItemIDs, dataModel.getItemIDsFromUser(array.getUserID(i))); } } } possibleItemIDs.removeAll(preferredItemIDs); return(possibleItemIDs); }
public override double itemSimilarity(long itemID1, long itemID2) { double num18; DataModel model = base.getDataModel(); PreferenceArray array = model.getPreferencesForItem(itemID1); PreferenceArray array2 = model.getPreferencesForItem(itemID2); int num = array.length(); int num2 = array2.length(); if ((num == 0) || (num2 == 0)) { return(double.NaN); } long num3 = array.getUserID(0); long num4 = array2.getUserID(0); int i = 0; int num6 = 0; double num7 = 0.0; double num8 = 0.0; double num9 = 0.0; double num10 = 0.0; double sumXY = 0.0; double num12 = 0.0; int n = 0; while (true) { int num14 = (num3 < num4) ? -1 : ((num3 > num4) ? 1 : 0); if (num14 == 0) { double num15 = array.getValue(i); double num16 = array2.getValue(num6); sumXY += num15 * num16; num7 += num15; num8 += num15 * num15; num9 += num16; num10 += num16 * num16; double num17 = num15 - num16; num12 += num17 * num17; n++; } if (num14 <= 0) { if (++i == num) { break; } num3 = array.getUserID(i); } if (num14 >= 0) { if (++num6 == num2) { break; } num4 = array2.getUserID(num6); } } if (this.centerData) { double num19 = n; double num20 = num7 / num19; double num21 = num9 / num19; double num22 = sumXY - (num21 * num7); double num23 = num8 - (num20 * num7); double num24 = num10 - (num21 * num9); num18 = this.computeResult(n, num22, num23, num24, num12); } else { num18 = this.computeResult(n, sumXY, num8, num10, num12); } if (!double.IsNaN(num18)) { num18 = this.normalizeWeightResult(num18, n, this.cachedNumUsers); } return(num18); }
protected override float doEstimatePreference(long theUserID, PreferenceArray preferencesFromUser, long itemID) { DataModel dataModel = getDataModel(); int size = preferencesFromUser.length(); FastIDSet possibleItemIDs = new FastIDSet(size); for (int i = 0; i < size; i++) { possibleItemIDs.add(preferencesFromUser.getItemID(i)); } possibleItemIDs.remove(itemID); List <RecommendedItem> mostSimilar = mostSimilarItems(itemID, possibleItemIDs.GetEnumerator(), neighborhoodSize, null); long[] theNeighborhood = new long[mostSimilar.Count() + 1]; theNeighborhood[0] = -1; List <long> usersRatedNeighborhood = new List <long>(); int nOffset = 0; foreach (RecommendedItem rec in mostSimilar) { theNeighborhood[nOffset++] = rec.getItemID(); } if (mostSimilar.Count != 0) { theNeighborhood[mostSimilar.Count] = itemID; for (int i = 0; i < theNeighborhood.Length; i++) { PreferenceArray usersNeighborhood = dataModel.getPreferencesForItem(theNeighborhood[i]); int size1 = usersRatedNeighborhood.Count == 0 ? usersNeighborhood.length() : usersRatedNeighborhood.Count; for (int j = 0; j < size1; j++) { if (i == 0) { usersRatedNeighborhood.Add(usersNeighborhood.getUserID(j)); } else { if (j >= usersRatedNeighborhood.Count) { break; } long index = usersRatedNeighborhood[j]; if (!usersNeighborhood.hasPrefWithUserID(index) || index == theUserID) { usersRatedNeighborhood.Remove(index); j--; } } } } } double[] weights = null; if (mostSimilar.Count != 0) { weights = getInterpolations(itemID, theNeighborhood, usersRatedNeighborhood); } int n = 0; double preference = 0.0; double totalSimilarity = 0.0; foreach (long jitem in theNeighborhood) { float?pref = dataModel.getPreferenceValue(theUserID, jitem); if (pref != null) { double weight = weights[n]; preference += pref.Value * weight; totalSimilarity += weight; } n++; } return(totalSimilarity == 0.0 ? float.NaN : (float)(preference / totalSimilarity)); }