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:
Refreshable, DataModel

public final class PlusAnonymousUserDataModel
extends java.lang.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, 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);
 }
 


Field Summary
static long TEMP_USER_ID
           
 
Constructor Summary
PlusAnonymousUserDataModel(DataModel delegate)
           
 
Method Summary
 LongPrimitiveIterator getItemIDs()
           
 FastIDSet getItemIDsFromUser(long userID)
           
 int getNumItems()
           
 int getNumUsers()
           
 int getNumUsersWithPreferenceFor(long... itemIDs)
           
 PreferenceArray getPreferencesForItem(long itemID)
           
 PreferenceArray getPreferencesFromUser(long userID)
           
 java.lang.Float getPreferenceValue(long userID, long itemID)
          Retrieves the preference value for a single user and item.
 LongPrimitiveIterator getUserIDs()
           
 boolean hasPreferenceValues()
           
 void refresh(java.util.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

setTempPrefs

public void setTempPrefs(PreferenceArray prefs)

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 List 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 java.lang.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

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... itemIDs)
                                 throws TasteException
Specified by:
getNumUsersWithPreferenceFor in interface DataModel
Parameters:
itemIDs - item IDs to check for
Returns:
the number of users who have expressed a preference for all of the items
Throws:
TasteException - if an error occurs while accessing the data
NoSuchItemException - if an item does not exist

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(java.util.Collection<Refreshable> alreadyRefreshed)
Description copied from interface: Refreshable

Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any 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 - s that are known to have already been refreshed as a result of an initial call to a 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


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.