com.planetj.taste.impl.correlation
Class GenericItemCorrelation

java.lang.Object
  extended by com.planetj.taste.impl.correlation.GenericItemCorrelation
All Implemented Interfaces:
Refreshable, ItemCorrelation

public final class GenericItemCorrelation
extends java.lang.Object
implements ItemCorrelation

A "generic" GenericItemCorrelation.ItemItemCorrelation which takes a static list of precomputed Item correlations 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 GenericItemCorrelation.ItemItemCorrelation 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.

Author:
Sean Owen, tsmorton

Nested Class Summary
static class GenericItemCorrelation.ItemItemCorrelation
          Encapsulates a correlation between two items.
 
Constructor Summary
GenericItemCorrelation(ItemCorrelation otherCorrelation, DataModel dataModel)
          Builds a list of item-item correlations given an GenericItemCorrelation.ItemItemCorrelation implementation and a DataModel, rather than a list of GenericItemCorrelation.ItemItemCorrelations.
GenericItemCorrelation(ItemCorrelation otherCorrelation, DataModel dataModel, int maxToKeep)
          Like GenericItemCorrelation(ItemCorrelation, DataModel) )}, but will only keep the specified number of correlations from the given DataModel.
GenericItemCorrelation(java.lang.Iterable<GenericItemCorrelation.ItemItemCorrelation> correlations)
          Creates a GenericItemCorrelation from a precomputed list of GenericItemCorrelation.ItemItemCorrelations.
GenericItemCorrelation(java.lang.Iterable<GenericItemCorrelation.ItemItemCorrelation> correlations, int maxToKeep)
          Like GenericItemCorrelation(Iterable), but will only keep the specified number of correlations from the given Iterable of correlations.
 
Method Summary
 double itemCorrelation(Item item1, Item item2)
          Returns the correlation between two items.
 void refresh()
          

Triggers "refresh" -- whatever that means -- of the implementation.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericItemCorrelation

public GenericItemCorrelation(java.lang.Iterable<GenericItemCorrelation.ItemItemCorrelation> correlations)

Creates a GenericItemCorrelation from a precomputed list of GenericItemCorrelation.ItemItemCorrelations. Each represents the correlation between two distinct items. Since correlation is assumed to be symmetric, it is not necessary to specify correlation between item1 and item2, and item2 and item1. Both are the same. It is also not necessary to specify a correlation between any item and itself; these are assumed to be 1.0.

Note that specifying a correlation between two items twice is not an error, but, the later value will win.

Parameters:
correlations - set of GenericItemCorrelation.ItemItemCorrelations on which to base this instance

GenericItemCorrelation

public GenericItemCorrelation(java.lang.Iterable<GenericItemCorrelation.ItemItemCorrelation> correlations,
                              int maxToKeep)

Like GenericItemCorrelation(Iterable), but will only keep the specified number of correlations from the given Iterable of correlations. It will keep those with the highest correlation -- those that are therefore most important.

Thanks to tsmorton for suggesting this and providing part of the implementation.

Parameters:
correlations - set of GenericItemCorrelation.ItemItemCorrelations on which to base this instance
maxToKeep - maximum number of correlations to keep

GenericItemCorrelation

public GenericItemCorrelation(ItemCorrelation otherCorrelation,
                              DataModel dataModel)
                       throws TasteException

Builds a list of item-item correlations given an GenericItemCorrelation.ItemItemCorrelation implementation and a DataModel, rather than a list of GenericItemCorrelation.ItemItemCorrelations.

It's valid to build a GenericItemCorrelation this way, but perhaps missing some of the point of an item-based recommender. Item-based recommenders use the assumption that item-item correlations are relatively fixed, and might be known already independent of user preferences. Hence it is useful to inject that information, using GenericItemCorrelation(Iterable).

Parameters:
otherCorrelation - other GenericItemCorrelation.ItemItemCorrelation to get correlations from
dataModel - data model to get Items from
Throws:
TasteException - if an error occurs while accessing the DataModel items

GenericItemCorrelation

public GenericItemCorrelation(ItemCorrelation otherCorrelation,
                              DataModel dataModel,
                              int maxToKeep)
                       throws TasteException

Like GenericItemCorrelation(ItemCorrelation, DataModel) )}, but will only keep the specified number of correlations from the given DataModel. It will keep those with the highest correlation -- those that are therefore most important.

Thanks to tsmorton for suggesting this and providing part of the implementation.

Parameters:
otherCorrelation - other GenericItemCorrelation.ItemItemCorrelation to get correlations from
dataModel - data model to get Items from
maxToKeep - maximum number of correlations to keep
Throws:
TasteException - if an error occurs while accessing the DataModel items
Method Detail

itemCorrelation

public double itemCorrelation(Item item1,
                              Item item2)

Returns the correlation between two items. Note that correlation is assumed to be symmetric, that itemCorrelation(item1, item2) == itemCorrelation(item2, item1), and that itemCorrelation(item1, item1) == 1.0 for all items.

Specified by:
itemCorrelation in interface ItemCorrelation
Parameters:
item1 - first item
item2 - second item
Returns:
correlation between the two

refresh

public void refresh()

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