private LongSet getEffectiveCandidates(long user, LongSet candidates, LongSet exclude) { if (candidates == null) { candidates = getPredictableItems(user); } if (exclude == null) { exclude = getDefaultExcludes(user); } logger.debug("computing effective candidates for user {} from {} candidates and {} excluded items", user, candidates.size(), exclude.size()); if (!exclude.isEmpty()) { candidates = LongUtils.setDifference(candidates, exclude); } return candidates; }
/** * Implement recommendation by calling {@link ItemScorer#scoreWithDetails(long, Collection)} and sorting * the results. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected ResultList recommendWithDetails(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); ResultMap scores = scorer.scoreWithDetails(user, candidates); return getTopNResults(n, scores); }
/** * Implement recommendation by calling {@link ItemScorer#score(long, Collection)} and sorting * the results by score. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected List<Long> recommend(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); Map<Long, Double> scores = scorer.score(user, candidates); Long2DoubleAccumulator accum; if (n >= 0) { accum = new TopNLong2DoubleAccumulator(n); } else { accum = new UnlimitedLong2DoubleAccumulator(); } Long2DoubleMap map = LongUtils.asLong2DoubleMap(scores); for (Long2DoubleMap.Entry e: Vectors.fastEntries(map)) { accum.put(e.getLongKey(), e.getDoubleValue()); } return accum.finishList(); }
@Test public void testNoScores() { StaticDataSource source = new StaticDataSource(); DataAccessObject dao = source.get(); ItemScorer scorer = PrecomputedItemScorer.newBuilder() .build(); ItemRecommender rec = new TopNItemRecommender(dao, scorer); List<Long> recs = rec.recommend(42); assertThat(recs, hasSize(0)); ResultList details = rec.recommendWithDetails(42, -1, null, null); assertThat(details, hasSize(0)); }
/** * Implement recommendation by calling {@link ItemScorer#score(long, Collection)} and sorting * the results by score. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected List<Long> recommend(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); Map<Long, Double> scores = scorer.score(user, candidates); Long2DoubleAccumulator accum; if (n >= 0) { accum = new TopNLong2DoubleAccumulator(n); } else { accum = new UnlimitedLong2DoubleAccumulator(); } Long2DoubleMap map = LongUtils.asLong2DoubleMap(scores); for (Long2DoubleMap.Entry e: Vectors.fastEntries(map)) { accum.put(e.getLongKey(), e.getDoubleValue()); } return accum.finishList(); }
@Test public void testGetScoreOnly() { StaticDataSource source = new StaticDataSource(); source.addSource(ImmutableList.of(Entities.create(CommonTypes.ITEM, 3))); DataAccessObject dao = source.get(); ItemScorer scorer = PrecomputedItemScorer.newBuilder() .addScore(42, 3, 3.5) .build(); ItemRecommender rec = new TopNItemRecommender(dao, scorer); List<Long> recs = rec.recommend(42); assertThat(recs, contains(3L)); ResultList details = rec.recommendWithDetails(42, -1, null, null); assertThat(details, hasSize(1)); assertThat(Results.basicCopy(details.get(0)), equalTo(Results.create(3, 3.5))); }
private LongSet getEffectiveCandidates(long user, LongSet candidates, LongSet exclude) { if (candidates == null) { candidates = getPredictableItems(user); } if (exclude == null) { exclude = getDefaultExcludes(user); } logger.debug("computing effective candidates for user {} from {} candidates and {} excluded items", user, candidates.size(), exclude.size()); if (!exclude.isEmpty()) { candidates = LongUtils.setDifference(candidates, exclude); } return candidates; }
/** * Implement recommendation by calling {@link ItemScorer#scoreWithDetails(long, Collection)} and sorting * the results. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected ResultList recommendWithDetails(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); ResultMap scores = scorer.scoreWithDetails(user, candidates); return getTopNResults(n, scores); }
@Test public void testExcludeScore() { StaticDataSource source = new StaticDataSource(); source.addSource(ImmutableList.of(Entities.create(CommonTypes.ITEM, 3))); DataAccessObject dao = source.get(); ItemScorer scorer = PrecomputedItemScorer.newBuilder() .addScore(42, 3, 3.5) .build(); ItemRecommender rec = new TopNItemRecommender(dao, scorer); List<Long> recs = rec.recommend(42, -1, null, LongSets.singleton(3L)); assertThat(recs, hasSize(0)); ResultList details = rec.recommendWithDetails(42, -1, null, LongSets.singleton(3L)); assertThat(details, hasSize(0)); }
@Test public void testFindSomeItems() { StaticDataSource source = new StaticDataSource(); source.addSource(ImmutableList.of(Entities.create(CommonTypes.ITEM, 3), Entities.create(CommonTypes.ITEM, 2), Entities.create(CommonTypes.ITEM, 7))); DataAccessObject dao = source.get(); ItemScorer scorer = PrecomputedItemScorer.newBuilder() .addScore(42, 2, 3.0) .addScore(42, 7, 1.0) .addScore(42, 3, 3.5) .build(); ItemRecommender rec = new TopNItemRecommender(dao, scorer); List<Long> recs = rec.recommend(42, 2, null, null); assertThat(recs, hasSize(2)); assertThat(recs, contains(3L, 2L)); ResultList details = rec.recommendWithDetails(42, 2, null, null); assertThat(details, hasSize(2)); assertThat(details.idList(), contains(3L, 2L)); } }