@Override protected ResultList recommendWithDetails(long user, int n, @Nullable LongSet candidates, @Nullable LongSet exclude) { return recommend(user, n, candidates, exclude) .stream() .map(i -> Results.create(i, statistics.getInteractionCount(i))) .collect(Results.listCollector()); } }
@Override public List<Long> recommendRelatedItems(long reference) { return recommendRelatedItems(reference, -1); }
@Override public List<Long> recommendRelatedItems(long reference, int n) { return recommendWithPredicate(n, i -> i != reference); }
@Override public LongList recommendRelatedItems(Set<Long> basket, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { return recommendWithSets(n, candidates, exclude); }
@Test public void testRecommendRelatedWithDetails() { ResultList results = recommender.recommendRelatedItemsWithDetails(ImmutableSet.of(), -1, null, null); assertThat(results.idList(), contains(1L, 2L)); }
@Test public void testRecommendForUserWithDetails() { ResultList results = recommender.recommendWithDetails(17, -1, null, null); assertThat(results.idList(), contains(1L, 2L)); }
@Before public void setUp() { List<Rating> ratings = ImmutableList.of(Rating.create(42, 1, 3.2), Rating.create(39, 1, 2.4), Rating.create(42, 2, 2.5)); StaticDataSource source = StaticDataSource.fromList(ratings); dao = source.get(); statistics = InteractionStatistics.create(dao); recommender = new PopularItemRecommender(statistics, dao); }
@Override protected LongList recommend(long user, int n, @Nullable LongSet candidates, @Nullable LongSet exclude) { if (exclude == null) { exclude = data.query(statistics.getEntityType()) .withAttribute(CommonAttributes.USER_ID, user) .valueSet(CommonAttributes.ITEM_ID); } return recommendWithSets(n, candidates, exclude); }
@Override public List<Long> recommendRelatedItems(Set<Long> basket) { return recommendRelatedItems(basket, -1); }
@Test public void testRecommendWithCandidates() { List<Long> results = recommender.recommend(10, -1, ImmutableSet.of(1L), null); assertThat(results, contains(1L)); }
@Override public List<Long> recommendRelatedItems(Set<Long> basket, int n) { LongSet lbk = LongUtils.asLongSet(basket); return recommendWithPredicate(n, i -> !lbk.contains(i)); }
@Override public LongList recommendRelatedItems(Set<Long> basket, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { return recommendWithSets(n, candidates, exclude); }
@Override public ResultList recommendRelatedItemsWithDetails(Set<Long> basket, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { return recommendRelatedItems(basket, n, candidates, exclude) .stream() .map(i -> Results.create(i, statistics.getInteractionCount(i))) .collect(Results.listCollector()); }
@Test public void testRecommendLimit() { List<Long> results = recommender.recommend(100, 1); assertThat(results, contains(1L)); } }
private LongList recommendWithSets(int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { LongSet cs = LongUtils.asLongSet(candidates); LongSet es = LongUtils.asLongSet(exclude); LongPredicate f; if (cs != null) { if (es != null) { f = i -> cs.contains(i) && !es.contains(i); } else { f = cs::contains; } } else if (es != null) { f = i -> !es.contains(i); } else { f = null; } return recommendWithPredicate(n, f); }
@Override protected LongList recommend(long user, int n, @Nullable LongSet candidates, @Nullable LongSet exclude) { if (exclude == null) { exclude = data.query(statistics.getEntityType()) .withAttribute(CommonAttributes.USER_ID, user) .valueSet(CommonAttributes.ITEM_ID); } return recommendWithSets(n, candidates, exclude); }
@Test public void testRecommendRelatedBasket() { List<Long> results = recommender.recommendRelatedItems(ImmutableSet.of(1L, 10L)); assertThat(results, contains(2L)); }
@Test public void testRecommendUnratedItems() { List<Long> results = recommender.recommend(39); assertThat(results, contains(2L)); }
@Override public List<Long> recommendRelatedItems(long reference, int n) { return recommendWithPredicate(n, i -> i != reference); }
@Test public void testRecommendRelatedWithCandidates() { List<Long> results = recommender.recommendRelatedItems(ImmutableSet.of(), -1, ImmutableSet.of(1L), null); assertThat(results, contains(1L)); }