@Test public void testTwoLikes() { SparseVector vec = sum.summarize(History.forUser(42, Like.create(42, 39), Like.create(42, 67))); assertThat(vec.size(), equalTo(2)); assertThat(vec.get(39), equalTo(1.0)); assertThat(vec.get(67), equalTo(1.0)); }
@Override public Double get(long key) { if (vector.containsKey(key)) { return vector.get(key); } else { return defaultReturnValue(); } }
@Test public void testLikeBatch() { SparseVector vec = sum.summarize( History.forUser( 42, Like.create(42, 39), LikeBatch.create(42, 67, 402), Like.create(42, 39))); assertThat(vec.size(), equalTo(2)); assertThat(vec.get(39), equalTo(2.0)); assertThat(vec.get(67), equalTo(402.0)); }
/** * Check that we score items but do not provide scores for items the user has previously rated. * User 5 has rated only item 8 previously. */ @Test public void testItemScorerNoRating() { long[] items = {7, 8}; ItemItemScorer scorer = session.get(ItemItemScorer.class); assertThat(scorer, notNullValue()); SparseVector scores = scorer.score(5, LongArrayList.wrap(items)); assertThat(scores, notNullValue()); assertThat(scores.size(), equalTo(1)); assertThat(scores.get(7), not(notANumber())); assertThat(scores.containsKey(8), equalTo(false)); }
/** Compute the DCG of a list of items with respect to a value vector. */ static double computeDCG(LongList items, SparseVector values) { final double lg2 = log(2); double gain = 0; int rank = 0; LongIterator iit = items.iterator(); while (iit.hasNext()) { final long item = iit.nextLong(); final double v = values.get(item, 0); rank++; if (rank < 2) { gain += v; } else { gain += v * lg2 / log(rank); } } return gain; }
/** * Check that we score items but do not provide scores for items the user has previously rated. * User 5 has rated only item 8 previously. */ @Test public void testItemScorerChannels() { long[] items = {7, 8}; ItemItemScorer scorer = session.get(ItemItemScorer.class); assertThat(scorer, notNullValue()); SparseVector scores = scorer.score(5, LongArrayList.wrap(items)); assertThat(scores, notNullValue()); assertThat(scores.size(), equalTo(1)); assertThat(scores.get(7), not(notANumber())); assertThat( scores.getChannelVector(ItemItemScorer.NEIGHBORHOOD_SIZE_SYMBOL).get(7), closeTo(1.0, 1.0e-5)); assertThat(scores.containsKey(8), equalTo(false)); long[] items2 = {7, 8, 9}; scorer = session.get(ItemItemScorer.class); assertThat(scorer, notNullValue()); scores = scorer.score(2, LongArrayList.wrap(items2)); assertThat( scores.getChannelVector(ItemItemScorer.NEIGHBORHOOD_SIZE_SYMBOL).get(9), closeTo(3.0, 1.0e-5)); // 1, 7, 8 }
@Test public void testMeanBaseline() { ItemScorer pred = makeGlobalMean(); SparseVector pv = pred.score(10L, itemSet(2l)); assertEquals(RATINGS_DAT_MEAN, pv.get(2l), 0.00001); }
@Test public void testEmptyConstructor() { SparseVector v = new ImmutableSparseVector(); assertThat(v.isEmpty(), equalTo(true)); assertThat(v.get(15, Double.NaN), notANumber()); }