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); }
Modifier and Type | Field and Description |
---|---|
static long |
TEMP_USER_ID |
Constructor and Description |
---|
PlusAnonymousUserDataModel(DataModel delegate) |
Modifier and Type | Method and Description |
---|---|
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) |
public static final long TEMP_USER_ID
public PlusAnonymousUserDataModel(DataModel delegate)
protected DataModel getDelegate()
public void setTempPrefs(PreferenceArray prefs)
public void clearTempPrefs()
public LongPrimitiveIterator getUserIDs() throws TasteException
getUserIDs
in interface DataModel
TasteException
- if an error occurs while accessing the datapublic PreferenceArray getPreferencesFromUser(long userID) throws TasteException
getPreferencesFromUser
in interface DataModel
userID
- ID of user to get prefs forNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic FastIDSet getItemIDsFromUser(long userID) throws TasteException
getItemIDsFromUser
in interface DataModel
userID
- ID of user to get prefs forNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic LongPrimitiveIterator getItemIDs() throws TasteException
getItemIDs
in interface DataModel
LongPrimitiveIterator
of all item IDs in the model, in orderTasteException
- if an error occurs while accessing the datapublic PreferenceArray getPreferencesForItem(long itemID) throws TasteException
getPreferencesForItem
in interface DataModel
itemID
- item IDPreference
s expressed for that item, ordered by user ID, as an arrayNoSuchItemException
- if the item does not existTasteException
- if an error occurs while accessing the datapublic Float getPreferenceValue(long userID, long itemID) throws TasteException
DataModel
getPreferenceValue
in interface DataModel
userID
- user ID to get pref value fromitemID
- item ID to get pref value forNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic Long getPreferenceTime(long userID, long itemID) throws TasteException
DataModel
getPreferenceTime
in interface DataModel
userID
- user ID for preference in questionitemID
- item ID for preference in questionNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic int getNumItems() throws TasteException
getNumItems
in interface DataModel
TasteException
- if an error occurs while accessing the datapublic int getNumUsers() throws TasteException
getNumUsers
in interface DataModel
TasteException
- if an error occurs while accessing the datapublic int getNumUsersWithPreferenceFor(long itemID) throws TasteException
getNumUsersWithPreferenceFor
in interface DataModel
itemID
- item ID to check forTasteException
- if an error occurs while accessing the datapublic int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
getNumUsersWithPreferenceFor
in interface DataModel
itemID1
- first item ID to check foritemID2
- second item ID to check forTasteException
- if an error occurs while accessing the datapublic void setPreference(long userID, long itemID, float value) throws TasteException
DataModel
Sets a particular preference (item plus rating) for a user.
setPreference
in interface DataModel
userID
- user to set preference foritemID
- item to set preference forvalue
- preference valueNoSuchItemException
- if the item does not existNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic void removePreference(long userID, long itemID) throws TasteException
DataModel
Removes a particular preference for a user.
removePreference
in interface DataModel
userID
- user from which to remove preferenceitemID
- item to remove preference forNoSuchItemException
- if the item does not existNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic void refresh(Collection<Refreshable> alreadyRefreshed)
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.
refresh
in interface Refreshable
alreadyRefreshed
- Refreshable
s that are known to have already been
refreshed as a result of an initial call to a {#refresh(Collection)} method on some
object. This ensure that objects in a refresh dependency graph aren't refreshed twice
needlessly.public boolean hasPreferenceValues()
hasPreferenceValues
in interface DataModel
public float getMaxPreference()
getMaxPreference
in interface DataModel
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
public float getMinPreference()
getMinPreference
in interface DataModel
DataModel.getMaxPreference()
Copyright © 2008–2017 The Apache Software Foundation. All rights reserved.