MFRecommender extends AbstractFastRecommender<U, I> { @Override public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) { DoubleMatrix1D pu; pu = factorization.getUserVector(uidx2user(uidx)); if (pu == null) { return new FastRecommendation(uidx, new ArrayList<>()); } IntDoubleTopN topN = new IntDoubleTopN(min(maxLength, factorization.numItems())); DoubleMatrix1D r = factorization.getItemMatrix().zMult(pu, null); for (int iidx = 0; iidx < r.size(); iidx++) { if (filter.test(iidx)) { topN.add(iidx, r.getQuick(iidx)); } } topN.sort(); List<Tuple2id> items = topN.reverseStream() .collect(toList()); return new FastRecommendation(uidx, items); } MFRecommender(FastUserIndex<U> uIndex, FastItemIndex<I> iIndex, Factorization<U, I> factorization); @Override FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter); @Override FastRecommendation getRecommendation(int uidx, IntStream candidates);  }
@Test public void testFilter() { int maxLength = 2; IntPredicate filter = i -> i % 2 == 0; List<Tuple2id> result = recommender.getRecommendation(0, maxLength, filter).getIidxs(); List<Tuple2id> expected = Arrays.asList( tuple(0, 6.1), tuple(2, 4.1) ); assertEquals(expected, result); }
@Test public void testCandidates() { IntStream candidates = IntStream.of(1, 3, 5); List<Tuple2id> result = recommender.getRecommendation(0, candidates).getIidxs(); List<Tuple2id> expected = Arrays.asList( tuple(1, 5.1), tuple(3, 3.1), tuple(5, 1.1) ); assertEquals(expected, result); }