/**
* find the top entries in recommendationVector, map them to the real itemIDs and write back the result
*/
private void writeRecommendedItems(VarLongWritable userID, Vector recommendationVector, Context context)
throws IOException, InterruptedException {
TopItemsQueue topKItems = new TopItemsQueue(recommendationsPerUser);
FastIDSet itemsForUser = null;
if (idReader != null && idReader.isUserItemFilterSpecified()) {
itemsForUser = idReader.getItemsToRecommendForUser(userID.get());
}
for (Element element : recommendationVector.nonZeroes()) {
int index = element.index();
long itemID;
if (indexItemIDMap != null && !indexItemIDMap.isEmpty()) {
itemID = indexItemIDMap.get(index);
} else { // we don't have any mappings, so just use the original
itemID = index;
}
if (shouldIncludeItemIntoRecommendations(itemID, itemsToRecommendFor, itemsForUser)) {
float value = (float) element.get();
if (!Float.isNaN(value)) {
MutableRecommendedItem topItem = topKItems.top();
if (value > topItem.getValue()) {
topItem.set(itemID, value);
topKItems.updateTop();
}
}
}
}
List<RecommendedItem> topItems = topKItems.getTopItems();
if (!topItems.isEmpty()) {
recommendedItems.set(topItems);
context.write(userID, recommendedItems);
}
}