/** Tests {@code recommend(long, SparseVector, int, Set, Set)}. */
  @Test
  public void testItemItemRecommender4() {
    List<ScoredId> recs = recommender.recommend(5, -1, null, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 7L, 9L));

    LongOpenHashSet candidates = new LongOpenHashSet();
    candidates.add(6);
    candidates.add(7);
    candidates.add(8);
    candidates.add(9);
    recs = recommender.recommend(5, -1, candidates, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 7L, 9L));

    candidates.remove(6);
    recs = recommender.recommend(5, -1, candidates, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(7L, 9L));

    candidates.remove(7);
    recs = recommender.recommend(5, -1, candidates, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(9L));

    candidates.remove(9);
    recs = recommender.recommend(5, -1, candidates, null);
    assertThat(recs, hasSize(0));

    candidates.add(9);
    candidates.add(7);
    recs = recommender.recommend(5, 1, candidates, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), anyOf(contains(9L), contains(7L)));

    LongOpenHashSet exclude = new LongOpenHashSet();
    exclude.add(7);
    recs = recommender.recommend(5, 2, candidates, exclude);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(9L));

    recs = recommender.recommend(5, 0, candidates, null);
    assertThat(recs, hasSize(0));

    candidates.clear();
    candidates.add(7);
    candidates.add(9);
    recs = recommender.recommend(5, -1, candidates, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(7L, 9L));

    candidates.add(6);
    exclude.clear();
    exclude.add(9);
    recs = recommender.recommend(5, -1, candidates, exclude);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 7L));

    exclude.add(7);
    recs = recommender.recommend(5, -1, candidates, exclude);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L));

    exclude.add(6);
    recs = recommender.recommend(5, -1, candidates, exclude);
    assertThat(recs, hasSize(0));
  }
  /** Tests {@code recommend(long, SparseVector, int)}. */
  @Test
  public void testItemItemRecommender2() {
    List<ScoredId> recs = recommender.recommend(2, 1);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), contains(9L));

    recs = recommender.recommend(2, 0);
    assertThat(recs, hasSize(0));

    recs = recommender.recommend(3, 1);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), anyOf(contains(6L), contains(9L)));

    recs = recommender.recommend(4, 0);
    assertThat(recs, hasSize(0));
  }
  /** Tests {@code recommend(long, SparseVector, Set)}. */
  @Test
  public void testItemItemRecommender3() {
    List<ScoredId> recs = recommender.recommend(1, null);
    assertTrue(recs.isEmpty());

    LongOpenHashSet candidates = new LongOpenHashSet();
    candidates.add(6);
    candidates.add(7);
    candidates.add(8);
    candidates.add(9);
    recs = recommender.recommend(1, candidates);
    assertThat(recs, hasSize(0));

    recs = recommender.recommend(2, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), contains(9L));

    candidates.clear();
    candidates.add(7);
    candidates.add(8);
    candidates.add(9);
    recs = recommender.recommend(2, candidates);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), contains(9L));

    candidates.add(6);
    candidates.remove(9);
    recs = recommender.recommend(2, candidates);
    assertThat(recs, hasSize(0));

    recs = recommender.recommend(5, null);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(9L, 7L, 6L));

    candidates.clear();
    candidates.add(6);
    candidates.add(7);
    recs = recommender.recommend(5, candidates);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 7L));

    candidates.clear();
    candidates.add(6);
    candidates.add(9);
    recs = recommender.recommend(5, candidates);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 9L));
  }
  /** Tests {@code recommend(long, SparseVector)}. */
  @Test
  public void testItemItemRecommender1() {
    List<ScoredId> recs = recommender.recommend(1);
    assertThat(recs, hasSize(0));

    recs = recommender.recommend(2);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), contains(9L));

    recs = recommender.recommend(3);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), contains(6L));

    recs = recommender.recommend(4);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 9L));
    assertEquals(2, recs.size());

    recs = recommender.recommend(5);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 7L, 9L));

    recs = recommender.recommend(6);
    assertThat(Lists.transform(recs, ScoredIds.idFunction()), containsInAnyOrder(6L, 7L, 9L));
  }
Ejemplo n.º 5
0
 @Before
 public void createBuilder() {
   builder = ScoredIds.newListBuilder();
 }