/** * Get the IDs of the candidate neighbors for a user. * @param user The user. * @param userItems The user's rated items. * @param targetItems The set of target items. * @return The set of IDs of candidate neighbors. */ private LongSet findCandidateNeighbors(long user, LongSet userItems, LongCollection targetItems) { LongSet users = new LongOpenHashSet(100); LongIterator items; if (userItems.size() < targetItems.size()) { items = userItems.iterator(); } else { items = targetItems.iterator(); } while (items.hasNext()) { LongSet iusers = dao.query(CommonTypes.RATING) .withAttribute(CommonAttributes.ITEM_ID, items.nextLong()) .valueSet(CommonAttributes.USER_ID); if (iusers != null) { users.addAll(iusers); } } users.remove(user); return users; }
@Override public long[] toLongArray() { synchronized (sync) { return collection.toLongArray(); } } @Override
/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
@Override public boolean retainAll(LongCollection c) { if (c instanceof LongSortedSet) { return retainAll(c.iterator()); } else { long[] longs = c.toLongArray(); Arrays.sort(longs); return retainAll(LongArrayList.wrap(longs).iterator()); } }
/** * Initialize the training estimator. * @param snap The getEntry snapshot. * @param baseline The baseline predictor. * @param dom The getEntry domain (for clamping). */ TrainingEstimator(RatingMatrix snap, BiasModel baseline, PreferenceDomain dom) { ratings = snap.getRatings(); domain = dom; estimates = new double[ratings.size()]; final LongCollection userIds = snap.getUserIds(); LongIterator userIter = userIds.iterator(); double global = baseline.getIntercept(); for (RatingMatrixEntry r: snap.getRatings()) { double userBias = baseline.getUserBias(r.getUserId()); double itemBias = baseline.getItemBias(r.getItemId()); estimates[r.getIndex()] = global + userBias + itemBias; } }
@Override public boolean removeAll(LongCollection c) { if (c instanceof LongSortedSet) { return removeAll(c.iterator()); } else { long[] longs = c.toLongArray(); Arrays.sort(longs); return removeAll(LongArrayList.wrap(longs).iterator()); } }
@Override public boolean contains(final long o) { return collection.contains(o); } @Override
@Override public Iterable<Neighbor> getCandidateNeighbors(final long user, LongSet items) { Long2DoubleMap urs = rvDAO.userRatingVector(user); if (urs.isEmpty()) { return Collections.emptyList(); } final Long2DoubleMap normed = similarityNormalizer.makeTransformation(user, urs) .apply(urs); assert normed != null; LongCollection qset = items; if (normed.size() < qset.size()) { qset = normed.keySet(); } final LongSet candidates = new LongOpenHashSet(); for (LongIterator iter = qset.iterator(); iter.hasNext();) { final long item = iter.nextLong(); LongSet users = snapshot.getItemUsers(item); if (users != null) { candidates.addAll(users); } } candidates.remove(user); logger.debug("Found {} candidate neighbors for user {}", candidates.size(), user); return new Iterable<Neighbor>() { @Override public Iterator<Neighbor> iterator() { return new NeighborIterator(user, normed, candidates); } }; }
@Override public LongIterator iterator() { return collection.iterator(); } @Override
@Override public int size() { synchronized (sync) { return collection.size(); } } @Override
@Override public long[] toLongArray() { return collection.toLongArray(); } /*
@Override public boolean contains(final long k) { synchronized (sync) { return collection.contains(k); } } @Override
@Override public LongIterator iterator() { return LongIterators.unmodifiable(collection.iterator()); } @Override
@Override public int size() { return collection.size(); } @Override
/** * Creates a queue using the elements in a type-specific collection using a * given comparator. * * <p> * This constructor is more efficient than enqueing the elements of * {@code collection} one by one. * * @param collection * a collection; its elements will be used to initialize the queue. * @param c * the comparator used in this queue, or {@code null} for the natural * order. */ public LongHeapPriorityQueue(final LongCollection collection, final LongComparator c) { this(collection.toLongArray(), c); } /**