org.apache.mahout.cf.taste.impl.model
Class PlusAnonymousUserDataModel

java.lang.Object
  extended by org.apache.mahout.cf.taste.impl.model.PlusAnonymousUserDataModel
All Implemented Interfaces:
Serializable, Refreshable, DataModel
Direct Known Subclasses:
PlusAnonymousConcurrentUserDataModel

public class PlusAnonymousUserDataModel
extends Object
implements DataModel

This DataModel decorator class is useful in a situation where you wish to recommend to a user that doesn't really exist yet in your actual DataModel. For example maybe you wish to recommend DVDs to a user who has browsed a few titles on your DVD store site, but, the user is not yet registered.

This enables you to temporarily add a temporary user to an existing DataModel in a way that recommenders can then produce recommendations anyway. To do so, wrap your real implementation in this class:

 DataModel realModel = ...;
 DataModel plusModel = new PlusAnonymousUserDataModel(realModel);
 ...
 ItemSimilarity similarity = new LogLikelihoodSimilarity(realModel); // not plusModel
 

But, you may continue to use realModel as input to other components. To recommend, first construct and set the temporary user information on the model and then simply call the recommender. The synchronized block exists to remind you that this is of course not thread-safe. Only one set of temp data can be inserted into the model and used at one time.

 Recommender recommender = ...;
 ...
 synchronized(...) {
   PreferenceArray tempPrefs = ...;
   plusModel.setTempPrefs(tempPrefs);
   recommender.recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 10);
   plusModel.setTempPrefs(null);
 }
 

See Also:
Serialized Form

Field Summary
static long TEMP_USER_ID
           
 
Constructor Summary
PlusAnonymousUserDataModel(DataModel delegate)
           
 
Method Summary
 void clearTempPrefs()
           
protected  DataModel getDelegate()
           
 LongPrimitiveIterator getItemIDs()
           
 FastIDSet getItemIDsFromUser(long userID)
           
 float getMaxPreference()
           
 float getMinPreference()
           
 int getNumItems()
           
 int getNumUsers()
           
 int getNumUsersWithPreferenceFor(long itemID)
           
 int getNumUsersWithPreferenceFor(long itemID1, long itemID2)
           
 PreferenceArray getPreferencesForItem(long itemID)
           
 PreferenceArray getPreferencesFromUser(long userID)
           
 Long getPreferenceTime(long userID, long itemID)
          Retrieves the time at which a preference value from a user and item was set, if known.
 Float getPreferenceValue(long userID, long itemID)
          Retrieves the preference value for a single user and item.
 LongPrimitiveIterator getUserIDs()
           
 boolean hasPreferenceValues()
           
 void refresh(Collection<Refreshable> alreadyRefreshed)
           Triggers "refresh" -- whatever that means -- of the implementation.
 void removePreference(long userID, long itemID)
           Removes a particular preference for a user.
 void setPreference(long userID, long itemID, float value)
           Sets a particular preference (item plus rating) for a user.
 void setTempPrefs(PreferenceArray prefs)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMP_USER_ID

public static final long TEMP_USER_ID
See Also:
Constant Field Values
Constructor Detail

PlusAnonymousUserDataModel

public PlusAnonymousUserDataModel(DataModel delegate)
Method Detail

getDelegate

protected DataModel getDelegate()

setTempPrefs

public void setTempPrefs(PreferenceArray prefs)

clearTempPrefs

public void clearTempPrefs()

getUserIDs

public LongPrimitiveIterator getUserIDs()
                                 throws TasteException
Specified by:
getUserIDs in interface DataModel
Returns:
all user IDs in the model, in order
Throws:
TasteException - if an error occurs while accessing the data

getPreferencesFromUser

public PreferenceArray getPreferencesFromUser(long userID)
                                       throws TasteException
Specified by:
getPreferencesFromUser in interface DataModel
Parameters:
userID - ID of user to get prefs for
Returns:
user's preferences, ordered by item ID
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getItemIDsFromUser

public FastIDSet getItemIDsFromUser(long userID)
                             throws TasteException
Specified by:
getItemIDsFromUser in interface DataModel
Parameters:
userID - ID of user to get prefs for
Returns:
IDs of items user expresses a preference for
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getItemIDs

public LongPrimitiveIterator getItemIDs()
                                 throws TasteException
Specified by:
getItemIDs in interface DataModel
Returns:
a LongPrimitiveIterator of all item IDs in the model, in order
Throws:
TasteException - if an error occurs while accessing the data

getPreferencesForItem

public PreferenceArray getPreferencesForItem(long itemID)
                                      throws TasteException
Specified by:
getPreferencesForItem in interface DataModel
Parameters:
itemID - item ID
Returns:
all existing Preferences expressed for that item, ordered by user ID, as an array
Throws:
NoSuchItemException - if the item does not exist
TasteException - if an error occurs while accessing the data

getPreferenceValue

public Float getPreferenceValue(long userID,
                                long itemID)
                         throws TasteException
Description copied from interface: DataModel
Retrieves the preference value for a single user and item.

Specified by:
getPreferenceValue in interface DataModel
Parameters:
userID - user ID to get pref value from
itemID - item ID to get pref value for
Returns:
preference value from the given user for the given item or null if none exists
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getPreferenceTime

public Long getPreferenceTime(long userID,
                              long itemID)
                       throws TasteException
Description copied from interface: DataModel
Retrieves the time at which a preference value from a user and item was set, if known. Time is expressed in the usual way, as a number of milliseconds since the epoch.

Specified by:
getPreferenceTime in interface DataModel
Parameters:
userID - user ID for preference in question
itemID - item ID for preference in question
Returns:
time at which preference was set or null if no preference exists or its time is not known
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getNumItems

public int getNumItems()
                throws TasteException
Specified by:
getNumItems in interface DataModel
Returns:
total number of items known to the model. This is generally the union of all items preferred by at least one user but could include more.
Throws:
TasteException - if an error occurs while accessing the data

getNumUsers

public int getNumUsers()
                throws TasteException
Specified by:
getNumUsers in interface DataModel
Returns:
total number of users known to the model.
Throws:
TasteException - if an error occurs while accessing the data

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long itemID)
                                 throws TasteException
Specified by:
getNumUsersWithPreferenceFor in interface DataModel
Parameters:
itemID - item ID to check for
Returns:
the number of users who have expressed a preference for the item
Throws:
TasteException - if an error occurs while accessing the data

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long itemID1,
                                        long itemID2)
                                 throws TasteException
Specified by:
getNumUsersWithPreferenceFor in interface DataModel
Parameters:
itemID1 - first item ID to check for
itemID2 - second item ID to check for
Returns:
the number of users who have expressed a preference for the items
Throws:
TasteException - if an error occurs while accessing the data

setPreference

public void setPreference(long userID,
                          long itemID,
                          float value)
                   throws TasteException
Description copied from interface: DataModel

Sets a particular preference (item plus rating) for a user.

Specified by:
setPreference in interface DataModel
Parameters:
userID - user to set preference for
itemID - item to set preference for
value - preference value
Throws:
NoSuchItemException - if the item does not exist
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

removePreference

public void removePreference(long userID,
                             long itemID)
                      throws TasteException
Description copied from interface: DataModel

Removes a particular preference for a user.

Specified by:
removePreference in interface DataModel
Parameters:
userID - user from which to remove preference
itemID - item to remove preference for
Throws:
NoSuchItemException - if the item does not exist
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

refresh

public void refresh(Collection<Refreshable> alreadyRefreshed)
Description copied from interface: Refreshable

Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any Refreshable should always leave itself in a consistent, operational state, and that the refresh atomically updates internal state from old to new.

Specified by:
refresh in interface Refreshable
Parameters:
alreadyRefreshed - Refreshables that are known to have already been refreshed as a result of an initial call to a Refreshable.refresh(Collection) method on some object. This ensure that objects in a refresh dependency graph aren't refreshed twice needlessly.

hasPreferenceValues

public boolean hasPreferenceValues()
Specified by:
hasPreferenceValues in interface DataModel
Returns:
true if this implementation actually stores and returns distinct preference values; that is, if it is not a 'boolean' DataModel

getMaxPreference

public float getMaxPreference()
Specified by:
getMaxPreference in interface DataModel
Returns:
the maximum preference value that is possible in the current problem domain being evaluated. For example, if the domain is movie ratings on a scale of 1 to 5, this should be 5. While a Recommender may estimate a preference value above 5.0, it isn't "fair" to consider that the system is actually suggesting an impossible rating of, say, 5.4 stars. In practice the application would cap this estimate to 5.0. Since evaluators evaluate the difference between estimated and actual value, this at least prevents this effect from unfairly penalizing a Recommender

getMinPreference

public float getMinPreference()
Specified by:
getMinPreference in interface DataModel
See Also:
DataModel.getMaxPreference()


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.