public void ratingVector() { IPreferenceArray prefs = dataModel.GetPreferencesFromUser(1); double[] ratingVector = ALSWRFactorizer.ratingVector(prefs); Assert.AreEqual(prefs.Length(), ratingVector.Length); Assert.AreEqual(prefs.Get(0).GetValue(), ratingVector[0], EPSILON); Assert.AreEqual(prefs.Get(1).GetValue(), ratingVector[1], EPSILON); Assert.AreEqual(prefs.Get(2).GetValue(), ratingVector[2], EPSILON); }
public void testPreferencesForItem() { IPreferenceArray prefs = model.GetPreferencesForItem(456); Assert.NotNull(prefs); IPreference pref1 = prefs.Get(0); Assert.AreEqual(123, pref1.GetUserID()); Assert.AreEqual(456, pref1.GetItemID()); IPreference pref2 = prefs.Get(1); Assert.AreEqual(456, pref2.GetUserID()); Assert.AreEqual(456, pref2.GetItemID()); Assert.AreEqual(2, prefs.Length()); }
public override IPreferenceArray GetPreferencesForItem(long itemID) { if (tempPrefs.Count == 0) { return(getDelegate().GetPreferencesForItem(itemID)); } IPreferenceArray delegatePrefs = null; try { delegatePrefs = getDelegate().GetPreferencesForItem(itemID); } catch (NoSuchItemException nsie) { // OK. Probably an item that only the anonymous user has //if (log.isDebugEnabled()) { // log.debug("Item {} unknown", itemID); //} } List <IPreference> anonymousPreferences = new List <IPreference>(); foreach (var prefsMap in tempPrefs) { IPreferenceArray singleUserTempPrefs = prefsMap.Value; for (int i = 0; i < singleUserTempPrefs.Length(); i++) { if (singleUserTempPrefs.GetItemID(i) == itemID) { anonymousPreferences.Add(singleUserTempPrefs.Get(i)); } } } int delegateLength = delegatePrefs == null ? 0 : delegatePrefs.Length(); int anonymousPrefsLength = anonymousPreferences.Count; int prefsCounter = 0; // Merge the delegate and anonymous preferences into a single array IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(delegateLength + anonymousPrefsLength); for (int i = 0; i < delegateLength; i++) { newPreferenceArray.Set(prefsCounter++, delegatePrefs.Get(i)); } foreach (IPreference anonymousPreference in anonymousPreferences) { newPreferenceArray.Set(prefsCounter++, anonymousPreference); } if (newPreferenceArray.Length() == 0) { // No, didn't find it among the anonymous user prefs throw new NoSuchItemException(itemID); } return(newPreferenceArray); }
public static double[] ratingVector(IPreferenceArray prefs) { double[] ratings = new double[prefs.Length()]; for (int n = 0; n < prefs.Length(); n++) { ratings[n] = prefs.Get(n).GetValue(); } return(ratings); //, true); new DenseVector( }
public static double[] ratingVector(IPreferenceArray prefs) { double[] ratings = new double[prefs.Length()]; for (int n = 0; n < prefs.Length(); n++) { ratings[n] = prefs.Get(n).GetValue(); } return ratings; //, true); new DenseVector( }
/// <p> /// Reads one line from the input file and adds the data to a {@link FastByIDMap} data structure which maps user IDs /// to preferences. This assumes that each line of the input file corresponds to one preference. After /// reading a line and determining which user and item the preference pertains to, the method should look to /// see if the data contains a mapping for the user ID already, and if not, add an empty data structure of preferences /// as appropriate to the data. /// </p> /// /// <p> /// Note that if the line is empty or begins with '#' it will be ignored as a comment. /// </p> /// /// @param line /// line from input data file /// @param data /// all data read so far, as a mapping from user IDs to preferences /// @param fromPriorData an implementation detail -- if true, data will map IDs to /// {@link PreferenceArray} since the framework is attempting to read and update raw /// data that is already in memory. Otherwise it maps to {@link Collection}s of /// {@link Preference}s, since it's reading fresh data. Subclasses must be prepared /// to handle this wrinkle. protected void processLine <T>(string line, FastByIDMap <T> data, FastByIDMap <FastByIDMap <DateTime?> > timestamps, bool fromPriorData) { // Ignore empty lines and comments if (line.Length == 0 || line[0] == COMMENT_CHAR) { return; } var tokens = SplitLine(line); string userIDString = tokens[0]; string itemIDString = tokens[1]; string preferenceValueString = tokens[2]; bool hasTimestamp = tokens.Length > 3; string timestampString = hasTimestamp ? tokens[3] : null; long userID = readUserIDFromString(userIDString); long itemID = readItemIDFromString(itemIDString); if (transpose) { long tmp = userID; userID = itemID; itemID = tmp; } // This is kind of gross but need to handle two types of storage var maybePrefs = data.Get(userID); if (fromPriorData) { // Data are PreferenceArray IPreferenceArray prefs = (IPreferenceArray)maybePrefs; if (!hasTimestamp && String.IsNullOrWhiteSpace(preferenceValueString)) { // Then line is of form "userID,itemID,", meaning remove if (prefs != null) { bool exists = false; int length = prefs.Length(); for (int i = 0; i < length; i++) { if (prefs.GetItemID(i) == itemID) { exists = true; break; } } if (exists) { if (length == 1) { data.Remove(userID); } else { IPreferenceArray newPrefs = new GenericUserPreferenceArray(length - 1); for (int i = 0, j = 0; i < length; i++, j++) { if (prefs.GetItemID(i) == itemID) { j--; } else { newPrefs.Set(j, prefs.Get(i)); } } data.Put(userID, (T)newPrefs); } } } removeTimestamp(userID, itemID, timestamps); } else { float preferenceValue = float.Parse(preferenceValueString, CultureInfo.InvariantCulture); bool exists = false; if (uniqueUserItemCheck && prefs != null) { for (int i = 0; i < prefs.Length(); i++) { if (prefs.GetItemID(i) == itemID) { exists = true; prefs.SetValue(i, preferenceValue); break; } } } if (!exists) { if (prefs == null) { prefs = new GenericUserPreferenceArray(1); } else { IPreferenceArray newPrefs = new GenericUserPreferenceArray(prefs.Length() + 1); for (int i = 0, j = 1; i < prefs.Length(); i++, j++) { newPrefs.Set(j, prefs.Get(i)); } prefs = newPrefs; } prefs.SetUserID(0, userID); prefs.SetItemID(0, itemID); prefs.SetValue(0, preferenceValue); data.Put(userID, (T)prefs); } } addTimestamp(userID, itemID, timestampString, timestamps); } else { // Data are IEnumerable<Preference> IEnumerable <IPreference> prefs = ((IEnumerable <IPreference>)maybePrefs); if (!hasTimestamp && String.IsNullOrWhiteSpace(preferenceValueString)) { // Then line is of form "userID,itemID,", meaning remove if (prefs != null) { // remove pref var prefsIterator = ((IEnumerable <IPreference>)prefs.ToArray()).GetEnumerator(); while (prefsIterator.MoveNext()) { IPreference pref = prefsIterator.Current; if (pref.GetItemID() == itemID) { if (prefs is IList <IPreference> ) { ((IList <IPreference>)maybePrefs).Remove(pref);// prefsIterator.remove() } break; } } } removeTimestamp(userID, itemID, timestamps); } else { float preferenceValue = float.Parse(preferenceValueString, CultureInfo.InvariantCulture); bool exists = false; if (uniqueUserItemCheck && prefs != null) { foreach (IPreference pref in prefs) { if (pref.GetItemID() == itemID) { exists = true; pref.SetValue(preferenceValue); break; } } } if (!exists) { if (prefs == null) { prefs = new List <IPreference>(5); data.Put(userID, (T)prefs); } if (prefs is IList <IPreference> ) { ((IList <IPreference>)prefs).Add(new GenericPreference(userID, itemID, preferenceValue)); } } addTimestamp(userID, itemID, timestampString, timestamps); } } }