public GenericDataModel(FastByIDMap <PreferenceArray> userData, FastByIDMap <FastByIDMap <DateTime?> > timestamps) { this.preferenceFromUsers = userData; FastByIDMap <List <Preference> > data = new FastByIDMap <List <Preference> >(); FastIDSet set = new FastIDSet(); int num = 0; float negativeInfinity = float.NegativeInfinity; float positiveInfinity = float.PositiveInfinity; foreach (KeyValuePair <long, PreferenceArray> pair in this.preferenceFromUsers.entrySet()) { PreferenceArray array = pair.Value; array.sortByItem(); foreach (Preference preference in array) { long key = preference.getItemID(); set.add(key); List <Preference> list = data.get(key); if (list == null) { list = new List <Preference>(2); data.put(key, list); } list.Add(preference); float num5 = preference.getValue(); if (num5 > negativeInfinity) { negativeInfinity = num5; } if (num5 < positiveInfinity) { positiveInfinity = num5; } } if ((++num % 0x2710) == 0) { log.info("Processed {0} users", new object[] { num }); } } log.info("Processed {0} users", new object[] { num }); this.setMinPreference(positiveInfinity); this.setMaxPreference(negativeInfinity); this.itemIDs = set.toArray(); set = null; Array.Sort <long>(this.itemIDs); this.preferenceForItems = toDataMap(data, false); foreach (KeyValuePair <long, PreferenceArray> pair in this.preferenceForItems.entrySet()) { pair.Value.sortByUser(); } this.userIDs = new long[userData.size()]; int num6 = 0; foreach (long num7 in userData.Keys) { this.userIDs[num6++] = num7; } Array.Sort <long>(this.userIDs); this.timestamps = timestamps; }
public double userSimilarity(long userID1, long userID2) { int num4; PreferenceArray array = this.dataModel.getPreferencesFromUser(userID1); PreferenceArray array2 = this.dataModel.getPreferencesFromUser(userID2); int num = array.length(); int num2 = array2.length(); if ((num <= 1) || (num2 <= 1)) { return(double.NaN); } array = array.clone(); array2 = array2.clone(); array.sortByValue(); array2.sortByValue(); float num3 = 1f; for (num4 = 0; num4 < num; num4++) { if (array2.hasPrefWithItemID(array.getItemID(num4))) { array.setValue(num4, num3); num3++; } } num3 = 1f; for (num4 = 0; num4 < num2; num4++) { if (array.hasPrefWithItemID(array2.getItemID(num4))) { array2.setValue(num4, num3); num3++; } } array.sortByItem(); array2.sortByItem(); long num5 = array.getItemID(0); long num6 = array2.getItemID(0); int i = 0; int num8 = 0; double num9 = 0.0; int num10 = 0; while (true) { int num11 = (num5 < num6) ? -1 : ((num5 > num6) ? 1 : 0); if (num11 == 0) { double num12 = array.getValue(i) - array2.getValue(num8); num9 += num12 * num12; num10++; } if (num11 <= 0) { if (++i >= num) { break; } num5 = array.getItemID(i); } if (num11 >= 0) { if (++num8 >= num2) { break; } num6 = array2.getItemID(num8); } } if (num10 <= 1) { return(double.NaN); } return(1.0 - ((6.0 * num9) / ((double)(num10 * ((num10 * num10) - 1))))); }