/** * Construct a new interaction statistics object that counts ratings. * @param dao The DAO. * @return The rating statistics. */ public static InteractionStatistics create(DataAccessObject dao) { return create(dao, CommonTypes.RATING); }
@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); }
@Inject public PopularityRankItemScorer(final InteractionStatistics stats) { statistics = stats; long[] items = stats.getKnownItems().toLongArray(); LongArrays.quickSort(items, (l1, l2) -> Integer.compare(stats.getInteractionCount(l2), stats.getInteractionCount(l1))); Long2IntMap ranks = LongUtils.itemRanks(LongArrayList.wrap(items)); SortedKeyIndex keys = SortedKeyIndex.fromCollection(ranks.keySet()); int n = keys.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { values[i] = 1.0 - ranks.get(keys.getKey(i)) / ((double) n); } rankScores = Long2DoubleSortedArrayMap.wrap(keys, values); }
@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()); }
@Override public InteractionStatistics get() { Long2IntOpenHashMap counts = new Long2IntOpenHashMap(); try (ObjectStream<Entity> stream = dao.query(entityType).stream()) { for (Entity e : stream) { long item = e.getLong(CommonAttributes.ITEM_ID); counts.addTo(item, 1); } } return new InteractionStatistics(entityType, counts); } }
private LongList recommendWithPredicate(int n, LongPredicate filter) { LongList items = statistics.getItemsByPopularity(); LongList list = new LongArrayList(items.size()); LongStream str = IntStream.range(0, items.size()).mapToLong(items::getLong); if (filter != null) { str = str.filter(filter); } if (n > 0) { str = str.limit(n); } str.forEachOrdered(list::add); return list; }
@Inject public PopularityRankItemScorer(final InteractionStatistics stats) { statistics = stats; long[] items = stats.getKnownItems().toLongArray(); LongArrays.quickSort(items, new AbstractLongComparator() { @Override public int compare(long l1, long l2) { return Integer.compare(stats.getInteractionCount(l2), stats.getInteractionCount(l1)); } }); Long2IntMap ranks = LongUtils.itemRanks(LongArrayList.wrap(items)); SortedKeyIndex keys = SortedKeyIndex.fromCollection(ranks.keySet()); int n = keys.size(); double[] values = new double[n]; for (int i = 0; i < n; i++) { values[i] = 1.0 - ranks.get(keys.getKey(i)) / ((double) n); } rankScores = Long2DoubleSortedArrayMap.wrap(keys, values); }
@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 InteractionStatistics get() { Long2IntOpenHashMap counts = new Long2IntOpenHashMap(); try (ObjectStream<Entity> stream = dao.query(entityType).stream()) { for (Entity e : stream) { long item = e.getLong(CommonAttributes.ITEM_ID); counts.addTo(item, e.getInteger(CommonAttributes.COUNT)); } } return new InteractionStatistics(entityType, counts); } }
private LongList recommendWithPredicate(int n, LongPredicate filter) { LongList items = statistics.getItemsByPopularity(); LongList list = new LongArrayList(items.size()); LongStream str = IntStream.range(0, items.size()).mapToLong(items::getLong); if (filter != null) { str = str.filter(filter); } if (n > 0) { str = str.limit(n); } str.forEachOrdered(list::add); return list; }
/** * Construct a new interaction statistics object that counts ratings. * @param dao The DAO. * @return The rating statistics. */ public static InteractionStatistics create(DataAccessObject dao) { return create(dao, CommonTypes.RATING); }
@Override public int compare(long l1, long l2) { return Integer.compare(stats.getInteractionCount(l2), stats.getInteractionCount(l1)); } });
@Override public InteractionStatistics get() { Long2IntOpenHashMap counts = new Long2IntOpenHashMap(); try (ObjectStream<Entity> stream = dao.query(entityType).stream()) { for (Entity e : stream) { long item = e.getLong(CommonAttributes.ITEM_ID); counts.addTo(item, 1); } } return new InteractionStatistics(entityType, counts); } }
@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); }
@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 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()); }
@Override public InteractionStatistics get() { Long2IntOpenHashMap counts = new Long2IntOpenHashMap(); try (ObjectStream<Entity> stream = dao.query(entityType).stream()) { for (Entity e : stream) { long item = e.getLong(CommonAttributes.ITEM_ID); counts.addTo(item, e.getInteger(CommonAttributes.COUNT)); } } return new InteractionStatistics(entityType, counts); } }
@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 PopularityRankItemScorer(statistics); }
@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()); } }