private Map<Long, Double> interactionCache = Maps.newHashMap();
private FeatureEncoder encoder = new FeatureEncoder();
public List<ScoredItem> topItems(User u, int limit) {
Vector userVector = new RandomAccessSparseVector(model.numFeatures());
encoder.addUserFeatures(u, userVector);
double userScore = model.classifyScalarNoLink(userVector);
PriorityQueue<ScoredItem> r = new PriorityQueue<ScoredItem>();
for (Item item : items) {
Double itemScore = itemCache.get(item);
if (itemScore == null) {
Vector v = new RandomAccessSparseVector(model.numFeatures());
encoder.addItemFeatures(item, v);
itemScore = model.classifyScalarNoLink(v);
itemCache.put(item, itemScore);
}
long code = encoder.interactionHash(u, item);
Double interactionScore = interactionCache.get(code);
if (interactionScore == null) {
Vector v = new RandomAccessSparseVector(model.numFeatures());
encoder.addInteractions(u, item, v);
interactionScore = model.classifyScalarNoLink(v);
interactionCache.put(code, interactionScore);
}
double score = userScore + itemScore + interactionScore;