@Test public void testTopItemItemAlt() throws Exception { List<GenericItemSimilarity.ItemItemSimilarity> sims = Lists.newArrayList(); for (int i = 0; i < 99; i++) { sims.add(new GenericItemSimilarity.ItemItemSimilarity(i, i + 1, 1 - (i / 99.0))); } List<GenericItemSimilarity.ItemItemSimilarity> res = TopItems.getTopItemItemSimilarities(10, sims.iterator()); int gold = 0; for (GenericItemSimilarity.ItemItemSimilarity re : res) { assertEquals(gold++, re.getItemID1()); } }
public static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities( int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { Queue<GenericItemSimilarity.ItemItemSimilarity> topSimilarities = new PriorityQueue<>(howMany + 1, Collections.reverseOrder()); boolean full = false; double lowestTopValue = Double.NEGATIVE_INFINITY; while (allSimilarities.hasNext()) { GenericItemSimilarity.ItemItemSimilarity similarity = allSimilarities.next(); double value = similarity.getValue(); if (!Double.isNaN(value) && (!full || value > lowestTopValue)) { topSimilarities.add(similarity); if (full) { topSimilarities.poll(); } else if (topSimilarities.size() > howMany) { full = true; topSimilarities.poll(); } lowestTopValue = topSimilarities.peek().getValue(); } } int size = topSimilarities.size(); if (size == 0) { return Collections.emptyList(); } List<GenericItemSimilarity.ItemItemSimilarity> result = new ArrayList<>(size); result.addAll(topSimilarities); Collections.sort(result); return result; }
TopItems { public static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities( int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { Queue<GenericItemSimilarity.ItemItemSimilarity> topSimilarities = new PriorityQueue<>(howMany + 1, Collections.reverseOrder()); boolean full = false; double lowestTopValue = Double.NEGATIVE_INFINITY; while (allSimilarities.hasNext()) { GenericItemSimilarity.ItemItemSimilarity similarity = allSimilarities.next(); double value = similarity.getValue(); if (!Double.isNaN(value) && (!full || value > lowestTopValue)) { topSimilarities.add(similarity); if (full) { topSimilarities.poll(); } else if (topSimilarities.size() > howMany) { full = true; topSimilarities.poll(); } lowestTopValue = topSimilarities.peek().getValue(); } } int size = topSimilarities.size(); if (size == 0) { return Collections.emptyList(); } List<GenericItemSimilarity.ItemItemSimilarity> result = new ArrayList<>(size); result.addAll(topSimilarities); Collections.sort(result); return result; } }
TopItems { public static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities( int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { Queue<GenericItemSimilarity.ItemItemSimilarity> topSimilarities = new PriorityQueue<>(howMany + 1, Collections.reverseOrder()); boolean full = false; double lowestTopValue = Double.NEGATIVE_INFINITY; while (allSimilarities.hasNext()) { GenericItemSimilarity.ItemItemSimilarity similarity = allSimilarities.next(); double value = similarity.getValue(); if (!Double.isNaN(value) && (!full || value > lowestTopValue)) { topSimilarities.add(similarity); if (full) { topSimilarities.poll(); } else if (topSimilarities.size() > howMany) { full = true; topSimilarities.poll(); } lowestTopValue = topSimilarities.peek().getValue(); } } int size = topSimilarities.size(); if (size == 0) { return Collections.emptyList(); } List<GenericItemSimilarity.ItemItemSimilarity> result = new ArrayList<>(size); result.addAll(topSimilarities); Collections.sort(result); return result; } private  TopItems(); }
TopItems { public static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities( int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { Queue<GenericItemSimilarity.ItemItemSimilarity> topSimilarities = new PriorityQueue<>(howMany + 1, Collections.reverseOrder()); boolean full = false; double lowestTopValue = Double.NEGATIVE_INFINITY; while (allSimilarities.hasNext()) { GenericItemSimilarity.ItemItemSimilarity similarity = allSimilarities.next(); double value = similarity.getValue(); if (!Double.isNaN(value) && (!full || value > lowestTopValue)) { topSimilarities.add(similarity); if (full) { topSimilarities.poll(); } else if (topSimilarities.size() > howMany) { full = true; topSimilarities.poll(); } lowestTopValue = topSimilarities.peek().getValue(); } } int size = topSimilarities.size(); if (size == 0) { return Collections.emptyList(); } List<GenericItemSimilarity.ItemItemSimilarity> result = new ArrayList<>(size); result.addAll(topSimilarities); Collections.sort(result); return result; } private  TopItems(); static List<RecommendedItem> getTopItems(int howMany,
                                                  LongPrimitiveIterator possibleItemIDs,
                                                  IDRescorer rescorer,
                                                  Estimator<Long> estimator); static long[] getTopUsers(int howMany,
                                   LongPrimitiveIterator allUserIDs,
                                   IDRescorer rescorer,
                                   Estimator<Long> estimator); static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities(
    int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities); static List<GenericUserSimilarity.UserUserSimilarity> getTopUserUserSimilarities(
    int howMany, Iterator<GenericUserSimilarity.UserUserSimilarity> allSimilarities); }
TopItems { public static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities( int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities) { Queue<GenericItemSimilarity.ItemItemSimilarity> topSimilarities = new PriorityQueue<>(howMany + 1, Collections.reverseOrder()); boolean full = false; double lowestTopValue = Double.NEGATIVE_INFINITY; while (allSimilarities.hasNext()) { GenericItemSimilarity.ItemItemSimilarity similarity = allSimilarities.next(); double value = similarity.getValue(); if (!Double.isNaN(value) && (!full || value > lowestTopValue)) { topSimilarities.add(similarity); if (full) { topSimilarities.poll(); } else if (topSimilarities.size() > howMany) { full = true; topSimilarities.poll(); } lowestTopValue = topSimilarities.peek().getValue(); } } int size = topSimilarities.size(); if (size == 0) { return Collections.emptyList(); } List<GenericItemSimilarity.ItemItemSimilarity> result = new ArrayList<>(size); result.addAll(topSimilarities); Collections.sort(result); return result; } private  TopItems(); static List<RecommendedItem> getTopItems(int howMany,
                                                  LongPrimitiveIterator possibleItemIDs,
                                                  IDRescorer rescorer,
                                                  Estimator<Long> estimator); static long[] getTopUsers(int howMany,
                                   LongPrimitiveIterator allUserIDs,
                                   IDRescorer rescorer,
                                   Estimator<Long> estimator); static List<GenericItemSimilarity.ItemItemSimilarity> getTopItemItemSimilarities(
    int howMany, Iterator<GenericItemSimilarity.ItemItemSimilarity> allSimilarities); static List<GenericUserSimilarity.UserUserSimilarity> getTopUserUserSimilarities(
    int howMany, Iterator<GenericUserSimilarity.UserUserSimilarity> allSimilarities);  }