List of usage examples for org.apache.mahout.cf.taste.impl.recommender ByValueRecommendedItemComparator getInstance
public static Comparator<RecommendedItem> getInstance()
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 w w . j a v a 2s . 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)); } }
From source file:org.plista.kornakapi.core.recommender.ArrayTopItems.java
License:Apache License
public static List<RecommendedItem> getTopItems(int howMany, long[] possibleItemIDs, int fromIndex, int toIndex, IDRescorer rescorer, TopItems.Estimator<Long> estimator) throws TasteException { Preconditions.checkArgument(possibleItemIDs != null, "possibleItemIDs is null"); Preconditions.checkArgument(estimator != null, "estimator is null"); Queue<RecommendedItem> topItems = new PriorityQueue<RecommendedItem>(howMany + 1, Collections.reverseOrder(ByValueRecommendedItemComparator.getInstance())); boolean full = false; double lowestTopValue = Double.NEGATIVE_INFINITY; for (int index = fromIndex; index < toIndex; index++) { long itemID = possibleItemIDs[index]; if (rescorer == null || !rescorer.isFiltered(itemID)) { double preference; try { preference = estimator.estimate(itemID); } catch (NoSuchItemException nsie) { continue; } catch (NoSuchUserException nsue) { continue; }/*from www . j ava 2 s. co m*/ double rescoredPref = rescorer == null ? preference : rescorer.rescore(itemID, preference); if (!Double.isNaN(rescoredPref) && (!full || rescoredPref > lowestTopValue)) { topItems.add(new GenericRecommendedItem(itemID, (float) rescoredPref)); if (full) { topItems.poll(); } else if (topItems.size() > howMany) { full = true; topItems.poll(); } lowestTopValue = topItems.peek().getValue(); } } } int size = topItems.size(); if (size == 0) { return Collections.emptyList(); } List<RecommendedItem> result = Lists.newArrayListWithCapacity(size); result.addAll(topItems); Collections.sort(result, ByValueRecommendedItemComparator.getInstance()); return result; }
From source file:org.plista.kornakapi.core.recommender.ParallelTopItems.java
License:Apache License
public static List<RecommendedItem> getTopItems(int howMany, int numThreads, FastIDSet possibleItemIDs, final IDRescorer rescorer, final TopItems.Estimator<Long> estimator) throws TasteException { Preconditions.checkArgument(numThreads > 1); Preconditions.checkNotNull(possibleItemIDs); Preconditions.checkNotNull(estimator); //long s = System.currentTimeMillis(); long[] itemIDsToEstimate = possibleItemIDs.toArray(); //long d = System.currentTimeMillis() - s; //System.out.println("Preparation " + d); //s = System.currentTimeMillis(); ExecutorService queue = Executors.newFixedThreadPool(numThreads); int fromIndex = 0; List<EstimationWorker> estimatorWorkers = Lists.newArrayListWithCapacity(numThreads); for (int n = 0; n < numThreads; n++) { int toIndex = Math.min(fromIndex + itemIDsToEstimate.length / numThreads, itemIDsToEstimate.length); EstimationWorker worker = new EstimationWorker(howMany, itemIDsToEstimate, fromIndex, toIndex, rescorer, estimator);/*from ww w.j ava 2 s.c o m*/ estimatorWorkers.add(worker); queue.execute(worker); fromIndex = toIndex; } queue.shutdown(); try { queue.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } //d = System.currentTimeMillis() - s; //System.out.println("Estimation " + d); List<RecommendedItem> topItems = Lists.newArrayList(); for (int n = 0; n < numThreads; n++) { topItems.addAll(estimatorWorkers.get(n).topItems); } if (topItems.isEmpty()) { return Collections.EMPTY_LIST; } if (topItems.size() < howMany) { howMany = topItems.size(); } Collections.sort(topItems, ByValueRecommendedItemComparator.getInstance()); List<RecommendedItem> recommendedItems = topItems.subList(0, howMany); return recommendedItems; }