@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));
 }
 @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));
 }
  /**
   * 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
  }
Пример #5
0
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    } else if (o instanceof SparseVector) {
      SparseVector vo = (SparseVector) o;

      int sz = size();
      int osz = vo.size();
      if (sz != osz) {
        return false;
      } else {
        if (!this.keySet().equals(vo.keySet())) {
          return false; // same keys
        }
        // we know that sparse vector values are always in key order. so just compare them.
        return this.values().equals(vo.values());
      }
    } else {
      return false;
    }
  }
Пример #6
0
 @Override
 public int size() {
   return vector.size();
 }
 @Test
 public void testSummarizeNoLike() {
   SparseVector vec = sum.summarize(History.forUser(42, Rating.create(42, 39, 2.5)));
   assertThat(vec.size(), equalTo(0));
 }
 @Test
 public void testSummarizeEmpty() {
   SparseVector vec = sum.summarize(History.forUser(42));
   assertThat(vec.size(), equalTo(0));
 }