public final class GenericItemSimilarity extends Object implements ItemSimilarity
A "generic" ItemSimilarity
which takes a static list of precomputed item similarities and bases its
responses on that alone. The values may have been precomputed offline by another process, stored in a file,
and then read and fed into an instance of this class.
This is perhaps the best ItemSimilarity
to use with
GenericItemBasedRecommender
, for now, since the point
of item-based recommenders is that they can take advantage of the fact that item similarity is relatively
static, can be precomputed, and then used in computation to gain a significant performance advantage.
Modifier and Type | Class and Description |
---|---|
static class |
GenericItemSimilarity.ItemItemSimilarity
Encapsulates a similarity between two items.
|
Constructor and Description |
---|
GenericItemSimilarity(ItemSimilarity otherSimilarity,
DataModel dataModel)
Builds a list of item-item similarities given an
ItemSimilarity implementation and a
DataModel , rather than a list of GenericItemSimilarity.ItemItemSimilarity s. |
GenericItemSimilarity(ItemSimilarity otherSimilarity,
DataModel dataModel,
int maxToKeep)
Like
GenericItemSimilarity(ItemSimilarity, DataModel) )}, but will only keep the specified
number of similarities from the given DataModel . |
GenericItemSimilarity(Iterable<GenericItemSimilarity.ItemItemSimilarity> similarities)
Creates a
GenericItemSimilarity from a precomputed list of GenericItemSimilarity.ItemItemSimilarity s. |
GenericItemSimilarity(Iterable<GenericItemSimilarity.ItemItemSimilarity> similarities,
int maxToKeep)
Like
GenericItemSimilarity(Iterable) , but will only keep the specified number of similarities
from the given Iterable of similarities. |
Modifier and Type | Method and Description |
---|---|
long[] |
allSimilarItemIDs(long itemID) |
double[] |
itemSimilarities(long itemID1,
long[] itemID2s)
A bulk-get version of
ItemSimilarity.itemSimilarity(long, long) . |
double |
itemSimilarity(long itemID1,
long itemID2)
Returns the similarity between two items.
|
void |
refresh(Collection<Refreshable> alreadyRefreshed)
Triggers "refresh" -- whatever that means -- of the implementation.
|
public GenericItemSimilarity(Iterable<GenericItemSimilarity.ItemItemSimilarity> similarities)
Creates a GenericItemSimilarity
from a precomputed list of GenericItemSimilarity.ItemItemSimilarity
s. Each
represents the similarity between two distinct items. Since similarity is assumed to be symmetric, it is
not necessary to specify similarity between item1 and item2, and item2 and item1. Both are the same. It
is also not necessary to specify a similarity between any item and itself; these are assumed to be 1.0.
Note that specifying a similarity between two items twice is not an error, but, the later value will win.
similarities
- set of GenericItemSimilarity.ItemItemSimilarity
s on which to base this instancepublic GenericItemSimilarity(Iterable<GenericItemSimilarity.ItemItemSimilarity> similarities, int maxToKeep)
Like GenericItemSimilarity(Iterable)
, but will only keep the specified number of similarities
from the given Iterable
of similarities. It will keep those with the highest similarity -- those
that are therefore most important.
Thanks to tsmorton for suggesting this and providing part of the implementation.
similarities
- set of GenericItemSimilarity.ItemItemSimilarity
s on which to base this instancemaxToKeep
- maximum number of similarities to keeppublic GenericItemSimilarity(ItemSimilarity otherSimilarity, DataModel dataModel) throws TasteException
Builds a list of item-item similarities given an ItemSimilarity
implementation and a
DataModel
, rather than a list of GenericItemSimilarity.ItemItemSimilarity
s.
It's valid to build a GenericItemSimilarity
this way, but perhaps missing some of the point of an
item-based recommender. Item-based recommenders use the assumption that item-item similarities are
relatively fixed, and might be known already independent of user preferences. Hence it is useful to
inject that information, using GenericItemSimilarity(Iterable)
.
otherSimilarity
- other ItemSimilarity
to get similarities fromdataModel
- data model to get items fromTasteException
- if an error occurs while accessing the DataModel
itemspublic GenericItemSimilarity(ItemSimilarity otherSimilarity, DataModel dataModel, int maxToKeep) throws TasteException
Like GenericItemSimilarity(ItemSimilarity, DataModel)
)}, but will only keep the specified
number of similarities from the given DataModel
. It will keep those with the highest similarity
-- those that are therefore most important.
Thanks to tsmorton for suggesting this and providing part of the implementation.
otherSimilarity
- other ItemSimilarity
to get similarities fromdataModel
- data model to get items frommaxToKeep
- maximum number of similarities to keepTasteException
- if an error occurs while accessing the DataModel
itemspublic double itemSimilarity(long itemID1, long itemID2)
Returns the similarity between two items. Note that similarity is assumed to be symmetric, that
itemSimilarity(item1, item2) == itemSimilarity(item2, item1)
, and that
itemSimilarity(item1,item1) == 1.0
for all items.
itemSimilarity
in interface ItemSimilarity
itemID1
- first itemitemID2
- second itempublic double[] itemSimilarities(long itemID1, long[] itemID2s)
ItemSimilarity
A bulk-get version of ItemSimilarity.itemSimilarity(long, long)
.
itemSimilarities
in interface ItemSimilarity
itemID1
- first item IDitemID2s
- second item IDs to compute similarity withpublic long[] allSimilarItemIDs(long itemID)
allSimilarItemIDs
in interface ItemSimilarity
public 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.Copyright © 2008–2017 The Apache Software Foundation. All rights reserved.