@Test public void preferencesFetchedOnlyOnce() throws Exception { DataModel dataModel = EasyMock.createMock(DataModel.class); ItemSimilarity itemSimilarity = EasyMock.createMock(ItemSimilarity.class); CandidateItemsStrategy candidateItemsStrategy = EasyMock.createMock(CandidateItemsStrategy.class); MostSimilarItemsCandidateItemsStrategy mostSimilarItemsCandidateItemsStrategy = EasyMock.createMock(MostSimilarItemsCandidateItemsStrategy.class); PreferenceArray preferencesFromUser = new GenericUserPreferenceArray( Arrays.asList(new GenericPreference(1L, 1L, 5.0f), new GenericPreference(1L, 2L, 4.0f))); EasyMock.expect(dataModel.getMinPreference()).andReturn(Float.NaN); EasyMock.expect(dataModel.getMaxPreference()).andReturn(Float.NaN); EasyMock.expect(dataModel.getPreferencesFromUser(1L)).andReturn(preferencesFromUser); EasyMock.expect(candidateItemsStrategy.getCandidateItems(1L, preferencesFromUser, dataModel, false)) .andReturn(new FastIDSet(new long[] { 3L, 4L })); EasyMock.expect(itemSimilarity.itemSimilarities(3L, preferencesFromUser.getIDs())) .andReturn(new double[] { 0.5, 0.3 }); EasyMock.expect(itemSimilarity.itemSimilarities(4L, preferencesFromUser.getIDs())) .andReturn(new double[] { 0.4, 0.1 }); EasyMock.replay(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); Recommender recommender = new GenericItemBasedRecommender(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); recommender.recommend(1L, 3); EasyMock.verify(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); }
@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); if (preferencesFromUser.length() == 0) { return Collections.emptyList(); } FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems); TopItems.Estimator<Long> estimator = new Estimator(userID, preferencesFromUser); List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator); log.debug("Recommendations are: {}", topItems); return topItems; }
GenericItemBasedRecommender extends AbstractRecommender implements ItemBasedRecommender { @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); if (preferencesFromUser.length() == 0) { return Collections.emptyList(); } FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems); TopItems.Estimator<Long> estimator = new Estimator(userID, preferencesFromUser); List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator); log.debug("Recommendations are: {}", topItems); return topItems; } }
GenericItemBasedRecommender extends AbstractRecommender implements ItemBasedRecommender { @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); if (preferencesFromUser.length() == 0) { return Collections.emptyList(); } FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems); TopItems.Estimator<Long> estimator = new Estimator(userID, preferencesFromUser); List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator); log.debug("Recommendations are: {}", topItems); return topItems; } GenericItemBasedRecommender(DataModel dataModel,
                                     ItemSimilarity similarity,
                                     CandidateItemsStrategy candidateItemsStrategy,
                                     MostSimilarItemsCandidateItemsStrategy mostSimilarItemsCandidateItemsStrategy);  GenericItemBasedRecommender(DataModel dataModel, ItemSimilarity similarity); }
GenericItemBasedRecommender extends AbstractRecommender implements ItemBasedRecommender { @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); if (preferencesFromUser.length() == 0) { return Collections.emptyList(); } FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems); TopItems.Estimator<Long> estimator = new Estimator(userID, preferencesFromUser); List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator); log.debug("Recommendations are: {}", topItems); return topItems; } GenericItemBasedRecommender(DataModel dataModel,
                                     ItemSimilarity similarity,
                                     CandidateItemsStrategy candidateItemsStrategy,
                                     MostSimilarItemsCandidateItemsStrategy mostSimilarItemsCandidateItemsStrategy);  GenericItemBasedRecommender(DataModel dataModel, ItemSimilarity similarity); ItemSimilarity getSimilarity(); @Override List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer, boolean includeKnownItems); @Override float estimatePreference(long userID, long itemID); @Override List<RecommendedItem> mostSimilarItems(long itemID, int howMany); @Override List<RecommendedItem> mostSimilarItems(long itemID, int howMany,
                                                Rescorer<LongPair> rescorer); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany,
                                                Rescorer<LongPair> rescorer); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                                int howMany,
                                                boolean excludeItemIfNotSimilarToAll); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany,
                                                Rescorer<LongPair> rescorer,
                                                boolean excludeItemIfNotSimilarToAll); @Override List<RecommendedItem> recommendedBecause(long userID, long itemID, int howMany); @Override void refresh(Collection<Refreshable> alreadyRefreshed); @Override String toString(); }
GenericItemBasedRecommender extends AbstractRecommender implements ItemBasedRecommender { @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); if (preferencesFromUser.length() == 0) { return Collections.emptyList(); } FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems); TopItems.Estimator<Long> estimator = new Estimator(userID, preferencesFromUser); List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator); log.debug("Recommendations are: {}", topItems); return topItems; } GenericItemBasedRecommender(DataModel dataModel,
                                     ItemSimilarity similarity,
                                     CandidateItemsStrategy candidateItemsStrategy,
                                     MostSimilarItemsCandidateItemsStrategy mostSimilarItemsCandidateItemsStrategy);  GenericItemBasedRecommender(DataModel dataModel, ItemSimilarity similarity); ItemSimilarity getSimilarity(); @Override List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer, boolean includeKnownItems); @Override float estimatePreference(long userID, long itemID); @Override List<RecommendedItem> mostSimilarItems(long itemID, int howMany); @Override List<RecommendedItem> mostSimilarItems(long itemID, int howMany,
                                                Rescorer<LongPair> rescorer); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany,
                                                Rescorer<LongPair> rescorer); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                                int howMany,
                                                boolean excludeItemIfNotSimilarToAll); @Override List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany,
                                                Rescorer<LongPair> rescorer,
                                                boolean excludeItemIfNotSimilarToAll); @Override List<RecommendedItem> recommendedBecause(long userID, long itemID, int howMany); @Override void refresh(Collection<Refreshable> alreadyRefreshed); @Override String toString();  }