Skip to content

Instantly share code, notes, and snippets.

@noff
Created November 24, 2013 12:33
Show Gist options
  • Save noff/7626790 to your computer and use it in GitHub Desktop.
Save noff/7626790 to your computer and use it in GitHub Desktop.
@Override
public SimpleItemItemModel get() {
// Get the transposed rating matrix
// This gives us a map of item IDs to those items' rating vectors
Map<Long, ImmutableSparseVector> itemVectors = getItemVectors();
// Get all items - you might find this useful
LongSortedSet items = LongUtils.packedSet(itemVectors.keySet());
// Map items to vectors of item similarities
// Map<Long,MutableSparseVector> itemSimilarities = new HashMap<Long, MutableSparseVector>();
Map<Long,List<ScoredId>> neighbours = new HashMap<Long, List<ScoredId>>();
// TODO FIXED Compute the similarities between each pair of items
CosineVectorSimilarity similarityObject = new CosineVectorSimilarity();
for(long itemId: items) {
TopNScoredItemAccumulator accumulator = new TopNScoredItemAccumulator(items.size() - 1);
for(long otherItemId: items) {
if(itemId != otherItemId) {
double sim = similarityObject.similarity(itemVectors.get(itemId), itemVectors.get(otherItemId));
if(sim >= 0) {
accumulator.put(otherItemId, sim);
}
}
}
List<ScoredId> similarities = accumulator.finish();
neighbours.put(itemId, similarities);
}
// It will need to be in a map of longs to lists of Scored IDs to store in the model
// return new SimpleItemItemModel(Collections.EMPTY_MAP);
return new SimpleItemItemModel(neighbours);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment