@Override public List<RecommendedItem> recommend( long userID, int howMany, IDRescorer rescorer, boolean includeKnownItems) throws TasteException { Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1"); log.debug("Recommending items for user ID '{}'", userID); PreferenceArray preferencesFromUser = getDataModel().getPreferencesFromUser(userID); FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems); TopItems.Estimator<Long> estimator = new Estimator(userID); List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator); log.debug("Recommendations are: {}", topItems); return topItems; }
@Test public void testTopItems() throws Exception { long[] ids = new long[100]; for (int i = 0; i < 100; i++) { ids[i] = i; } LongPrimitiveIterator possibleItemIds = new LongPrimitiveArrayIterator(ids); TopItems.Estimator<Long> estimator = new TopItems.Estimator<Long>() { @Override public double estimate(Long thing) { return thing; } }; List<RecommendedItem> topItems = TopItems.getTopItems(10, possibleItemIds, null, estimator); int gold = 99; for (RecommendedItem topItem : topItems) { assertEquals(gold, topItem.getItemID()); assertEquals(gold--, topItem.getValue(), 0.01); } }
@Test public void testTopItemsRandom() throws Exception { long[] ids = new long[100]; for (int i = 0; i < 100; i++) { ids[i] = i; } LongPrimitiveIterator possibleItemIds = new LongPrimitiveArrayIterator(ids); final Random random = RandomUtils.getRandom(); TopItems.Estimator<Long> estimator = new TopItems.Estimator<Long>() { @Override public double estimate(Long thing) { return random.nextDouble(); } }; List<RecommendedItem> topItems = TopItems.getTopItems(10, possibleItemIds, null, estimator); assertEquals(10, topItems.size()); double last = 2.0; for (RecommendedItem topItem : topItems) { assertTrue(topItem.getValue() <= last); last = topItem.getItemID(); } }