@Override public LongSet keySet() { return LongUtils.asLongSet(map.keySet()); }
/** * {@inheritDoc} * * This implementation delegates to {@link #recommend(long, int, LongSet, LongSet)}. */ @Override public List<Long> recommend(long user, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { return recommend(user, n, LongUtils.asLongSet(candidates), LongUtils.asLongSet(exclude)); }
@Nonnull @Override public Map<Long, Double> score(long user, @Nonnull Collection<Long> items) { return rankScores.subMap(LongUtils.asLongSet(items)); }
/** * Pick a random subset of a set of items. * @param items The set of items to pick from. * @param n the number of items to select. * @return A random subset of `items` of size at most `n`. */ public LongSet pickRandom(Set<Long> items, int n) { return LongUtils.randomSubset(LongUtils.asLongSet(items), n, random); }
@Override public ResultList recommendRelatedItemsWithDetails(Set<Long> basket, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { if (candidates == null) { candidates = dao.getEntityIds(CommonTypes.ITEM); } if (exclude == null) { exclude = getDefaultExcludes(LongUtils.asLongSet(basket)); } if (!exclude.isEmpty()) { candidates = LongUtils.setDifference(LongUtils.asLongSet(candidates), LongUtils.asLongSet(exclude)); } ResultMap scores = scorer.scoreRelatedItemsWithDetails(basket, candidates); return recommend(n, scores); }
@Override public List<Long> recommendRelatedItems(Set<Long> basket, int n) { LongSet lbk = LongUtils.asLongSet(basket); return recommendWithPredicate(n, i -> !lbk.contains(i)); }
@SuppressWarnings("unchecked") @Override public synchronized LongSet selectItems(LongSet universe, Recommender recommender, TestUser user) { script.setup(universe, recommender, user); Set<Long> set = (Set<Long>) script.run(); return LongUtils.asLongSet(set); }
/** * Get a Fastutil {@link LongSet} from a {@link java.util.Collection} of longs. * * @param longs The set of longs. * @return {@code longs} as a fastutil {@link LongSet}. If {@code longs} is already * a LongSet, it is cast. */ public static LongSet asLongSet(@Nullable final Collection<Long> longs) { if (longs == null) { return null; } else if (longs instanceof Set) { return asLongSet((Set<Long>) longs); } else { return SortedKeyIndex.fromCollection(longs).keySet(); } }
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); }
@Nonnull @Override public ResultMap scoreWithDetails(long user, @Nonnull Collection<Long> items) { if(cachedId == user && cachedScores != null) { LongSet cachedItems = LongUtils.asLongSet(cachedScores.keySet()); if (!cachedItems.containsAll(LongUtils.asLongCollection(items))) { LongSet reqItems = LongUtils.packedSet(items); LongSortedSet diffItems = LongUtils.setDifference(reqItems, cachedItems); ResultMap newCache = scorer.scoreWithDetails(user, diffItems); cachedScores = Results.newResultMap(Iterables.concat(cachedScores, newCache)); } } else { cachedScores = scorer.scoreWithDetails(user, items); cachedId = user; } return cachedScores; }
@Nonnull @Override public MetricResult measureUser(TestUser user, ResultMap predictions, Context context) { int n = user.getTestRatings().size(); int good = predictions.size(); assert LongUtils.setDifference(LongUtils.asLongSet(predictions.keySet()), user.getTestRatings().keySet()) .isEmpty(); context.addUser(n, good); return new Coverage(n, good); }
@Nonnull @Override public MetricResult measureUser(TestUser user, ResultMap predictions, Mean context) { if (predictions == null || predictions.isEmpty()) { return MetricResult.empty(); } Long2DoubleMap ratings = user.getTestRatings(); long[] ideal = ratings.keySet().toLongArray(); LongArrays.quickSort(ideal, LongComparators.oppositeComparator(LongUtils.keyValueComparator(ratings))); long[] actual = LongUtils.asLongSet(predictions.keySet()).toLongArray(); LongArrays.quickSort(actual, LongComparators.oppositeComparator( LongUtils.keyValueComparator( LongUtils.asLong2DoubleMap(predictions.scoreMap())))); double idealGain = computeDCG(ideal, ratings); double gain = computeDCG(actual, ratings); logger.debug("user {} has gain of {} (ideal {})", user.getUserId(), gain, idealGain); double score = gain / idealGain; synchronized (context) { context.increment(score); } ImmutableMap.Builder<String,Double> results = ImmutableMap.builder(); return MetricResult.fromMap(results.put(columnName, score) .put(columnName + ".Raw", gain) .build()); }
@Override public LongSet keySet() { return LongUtils.asLongSet(map.keySet()); }
@Test public void testRandomMaps() { for (Map<Long,Double> map: someMaps(longs(), doubles())) { Long2DoubleSortedArrayMap vec = Long2DoubleSortedArrayMap.create(map); Set<Long> picked = sets(map.keySet()).next(); Set<Long> extra = sets(longs()).next(); LongSortedSet wanted = LongUtils.setUnion(LongUtils.asLongSet(picked), LongUtils.asLongSet(extra)); Long2DoubleSortedMap sv = vec.subMap(wanted); assertThat(sv.keySet(), everyItem(isIn(wanted))); assertThat(sv.keySet(), containsInAnyOrder(picked.toArray())); assertThat(sv.entrySet(), everyItem(isIn(map.entrySet()))); } }
/** * {@inheritDoc} * * This implementation delegates to {@link #recommend(long, int, LongSet, LongSet)}. */ @Override public List<Long> recommend(long user, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { return recommend(user, n, LongUtils.asLongSet(candidates), LongUtils.asLongSet(exclude)); }
@Nonnull @Override public Map<Long, Double> score(long user, @Nonnull Collection<Long> items) { return rankScores.subMap(LongUtils.asLongSet(items)); }
@Override public ResultList recommendRelatedItemsWithDetails(Set<Long> basket, int n, @Nullable Set<Long> candidates, @Nullable Set<Long> exclude) { if (candidates == null) { candidates = dao.getEntityIds(CommonTypes.ITEM); } if (exclude == null) { exclude = getDefaultExcludes(LongUtils.asLongSet(basket)); } if (!exclude.isEmpty()) { candidates = LongUtils.setDifference(LongUtils.asLongSet(candidates), LongUtils.asLongSet(exclude)); } ResultMap scores = scorer.scoreRelatedItemsWithDetails(basket, candidates); return recommend(n, scores); }
@Override public List<Long> recommendRelatedItems(Set<Long> basket, int n) { LongSet lbk = LongUtils.asLongSet(basket); return recommendWithPredicate(n, i -> !lbk.contains(i)); }