/**
  * 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
  }
Beispiel #3
0
  /**
   * Get the keys of this vector sorted by the value of the items stored for each key.
   *
   * @param decreasing If {@code true}, sort in decreasing order.
   * @return The sorted list of keys of this vector.
   */
  public LongArrayList keysByValue(boolean decreasing) {
    long[] skeys = keySet().toLongArray();

    LongComparator cmp;
    // Set up the comparator. We use the key as a secondary comparison to get
    // a reproducible sort irrespective of sorting algorithm.
    if (decreasing) {
      cmp =
          new AbstractLongComparator() {
            @Override
            public int compare(long k1, long k2) {
              int c = Double.compare(get(k2), get(k1));
              if (c != 0) {
                return c;
              } else {
                return Longs.compare(k1, k2);
              }
            }
          };
    } else {
      cmp =
          new AbstractLongComparator() {
            @Override
            public int compare(long k1, long k2) {
              int c = Double.compare(get(k1), get(k2));
              if (c != 0) {
                return c;
              } else {
                return Longs.compare(k1, k2);
              }
            }
          };
    }

    LongArrays.quickSort(skeys, cmp);
    return LongArrayList.wrap(skeys);
  }