@Override public boolean applySV(long value) { return _matchingValues.contains(value); } }
@Override public boolean applySV(long value) { return !_nonMatchingValues.contains(value); } }
private static long[] filterLongs(LongSet longSet, long[] source) { LongList longList = new LongArrayList(); for (long value : source) { if (longSet.contains(value)) { longList.add(value); } } if (longList.size() == source.length) { return source; } else { return longList.toLongArray(); } }
@Override public boolean containsKey(long k) { return keySet.contains(k); }
if (!set.contains(value)) { set.add(value); distinctCount++;
@Override public double getOrDefault(long key, double defaultValue) { if (keySet.contains(key)) { return valueFunc.applyAsDouble(key); } else { return defaultValue; } }
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 public List<Long> recommendRelatedItems(Set<Long> basket, int n) { LongSet lbk = LongUtils.asLongSet(basket); return recommendWithPredicate(n, i -> !lbk.contains(i)); }
@Nonnull @Override public MetricResult measureUserRecList(Recommender rec, TestUser user, int targetLength, List<Long> recs, Context context) { int tp = 0; LongSet items = goodItems.selectItems(context.universe, rec, user); for (long item: recs) { if(items.contains(item)) { tp += 1; } } if (items.size() > 0 && recs.size() > 0) { // if both the items set and recommendations are non-empty (no division by 0). double precision = (double) tp / recs.size(); double recall = (double) tp / items.size(); context.addUser(precision, recall); return new PresRecResult(precision, recall).withSuffix(suffix); } else { context.addUser(0, 0); return new PresRecResult(0, 0).withSuffix(suffix); } }
@Nonnull @Override public MetricResult measureUserRecList(Recommender rec, TestUser user, int targetLength, List<Long> recommendations, Context context) { LongSet good = goodItems.selectItems(context.universe, rec, user); if (good.isEmpty()) { logger.warn("no good items for user {}", user.getUserId()); } Integer rank = null; int i = 0; for (long item: recommendations) { i++; if (good.contains(item)) { rank = i; break; } } UserResult result = new UserResult(rank); context.addUser(result); return result.withSuffix(suffix); }
/** * Compute the set difference of two sets. * * @param items The initial set * @param exclude The items to remove * @return The elements of <var>items</var> that are not in <var>exclude</var>. */ public static LongSortedSet setDifference(LongSet items, LongSet exclude) { long[] data = new long[items.size()]; final LongIterator iter = items.iterator(); int i = 0; while (iter.hasNext()) { final long x = iter.nextLong(); if (!exclude.contains(x)) { data[i++] = x; } } if (!(items instanceof LongSortedSet)) { Arrays.sort(data, 0, i); } // trim the array if (data.length * 2 > i * 3) { data = Arrays.copyOf(data, i); } return SortedKeyIndex.wrap(data, i).keySet(); }
/** * Compute the size of the intersection of two sets. * @param a The first set. * @param b The second set. * @return The size of the intersection of the two sets. */ public static int intersectSize(LongSet a, LongSet b) { if (a instanceof LongSortedSet && b instanceof LongSortedSet) { return intersectSize((LongSortedSet) a, (LongSortedSet) b); } else { int n = 0; LongIterator iter = a.iterator(); while (iter.hasNext()) { long x = iter.nextLong(); if (b.contains(x)) { n += 1; } } return n; } }
@Nonnull @Override public MetricResult measureUserRecList(Recommender rec, TestUser user, int targetLength, List<Long> recs, Context context) { LongSet good = goodItems.selectItems(context.universe, rec, user); if (good.isEmpty()) { logger.warn("no good items for user {}", user.getUserId()); return new UserResult(0); } if (recs == null || recs.isEmpty()) { return MetricResult.empty(); } int n = 0; double ngood = 0; double sum = 0; for (long id: recs) { n += 1; if (good.contains(id)) { // it is good ngood += 1; // add to MAP sum sum += ngood / n; } } double aveP = ngood > 0 ? sum / ngood : 0; UserResult result = new UserResult(aveP); context.addUser(result); return result.withSuffix(suffix); }
/** * Compute the intersection of two sets. * * @param a The first set. * @param b The second set. * @return The elements present in both sets. */ public static LongSortedSet setIntersect(LongSet a, LongSet b) { if (a instanceof LongSortedSet && b instanceof LongSortedSet) { return setIntersect((LongSortedSet) a, (LongSortedSet) b); } else if (a.size() <= b.size()) { LongArrayList longs = new LongArrayList(Math.min(a.size(), b.size())); LongIterator iter = a.iterator(); while (iter.hasNext()) { long key = iter.nextLong(); if (b.contains(key)) { longs.add(key); } } return LongUtils.packedSet(longs); } else { return setIntersect(b, a); } }
if (!seen.contains(nextItem)) {
while (iter.hasNext()) { final long item = iter.nextLong(); if (exclude.contains(item)) { continue;
@Override public Rating next() { long id = ids.next(); while (usedIds.contains(id)) { id = ids.next(); } usedIds.add(id); RatingBuilder rb = Rating.newBuilder() .setId(id) .setUserId(userIds.next()) .setItemId(itemIds.next()) .setRating(values.next()); if (timestamps != null) { rb.setTimestamp(timestamps.next()); } return rb.build(); } }
if (ids.contains(id)) { if (replace) { throw new UnsupportedOperationException("packed builder cannot replace entities");