Example usage for org.apache.mahout.cf.taste.hadoop RecommendedItemsWritable RecommendedItemsWritable

List of usage examples for org.apache.mahout.cf.taste.hadoop RecommendedItemsWritable RecommendedItemsWritable

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.hadoop RecommendedItemsWritable RecommendedItemsWritable.

Prototype

public RecommendedItemsWritable(List<RecommendedItem> recommended) 

Source Link

Usage

From source file:nl.gridline.zieook.inx.movielens.AggregateAndRecommendReducer.java

License:Apache License

/**
 * find the top entries in recommendationVector, map them to the real itemIDs and write back the result
 *//*from w ww  . j av a  2 s  . c  om*/
private void writeRecommendedItems(VarLongWritable userID, Vector recommendationVector, Context context)
        throws IOException, InterruptedException {
    Queue<RecommendedItem> topItems = new PriorityQueue<RecommendedItem>(recommendationsPerUser + 1,
            Collections.reverseOrder(ByValueRecommendedItemComparator.getInstance()));

    Iterator<Vector.Element> recommendationVectorIterator = recommendationVector.iterateNonZero();
    while (recommendationVectorIterator.hasNext()) {
        Vector.Element element = recommendationVectorIterator.next();
        int index = element.index();

        long itemID = indexItemIDMap.get(index);
        if (itemsToRecommendFor == null || itemsToRecommendFor.contains(itemID)) {
            float value = (float) element.get();
            if (!Float.isNaN(value)) {
                if (topItems.size() < recommendationsPerUser) {
                    topItems.add(new GenericRecommendedItem(itemID, value));
                } else if (value > topItems.peek().getValue()) {
                    topItems.add(new GenericRecommendedItem(itemID, value));
                    topItems.poll();
                }
            }
        }
    }

    if (!topItems.isEmpty()) {
        List<RecommendedItem> recommendations = new ArrayList<RecommendedItem>(topItems.size());
        recommendations.addAll(topItems);
        Collections.sort(recommendations, ByValueRecommendedItemComparator.getInstance());
        context.write(userID, new RecommendedItemsWritable(recommendations));
    }
}