예제 #1
0
  @Test
  public void testTopItemItem() throws Exception {
    List<GenericItemSimilarity.ItemItemSimilarity> sims = Lists.newArrayList();
    for (int i = 0; i < 99; i++) {
      sims.add(new GenericItemSimilarity.ItemItemSimilarity(i, i + 1, i / 99.0));
    }

    List<GenericItemSimilarity.ItemItemSimilarity> res =
        TopItems.getTopItemItemSimilarities(10, sims.iterator());
    int gold = 99;
    for (GenericItemSimilarity.ItemItemSimilarity re : res) {
      assertEquals(gold--, re.getItemID2()); // the second id should be equal to 99 to start
    }
  }
예제 #2
0
  @Test
  public void testTopUserUserAlt() throws Exception {
    List<GenericUserSimilarity.UserUserSimilarity> sims = Lists.newArrayList();
    for (int i = 0; i < 99; i++) {
      sims.add(new GenericUserSimilarity.UserUserSimilarity(i, i + 1, 1 - (i / 99.0)));
    }

    List<GenericUserSimilarity.UserUserSimilarity> res =
        TopItems.getTopUserUserSimilarities(10, sims.iterator());
    int gold = 0;
    for (GenericUserSimilarity.UserUserSimilarity re : res) {
      assertEquals(gold++, re.getUserID1()); // the first id should be equal to 0 to start
    }
  }
  @Override
  public List<RecommendedItem> recommend(
      long userID, int howMany, IDRescorer rescorer, boolean includeKnownItems)
      throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    PreferenceArray preferencesFromUser = getDataModel().getPreferencesFromUser(userID);
    FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems);

    TopItems.Estimator<Long> estimator = new Estimator(userID);

    List<RecommendedItem> topItems =
        TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, estimator);

    log.debug("Recommendations are: {}", topItems);
    return topItems;
  }
예제 #4
0
 @Test
 public void testTopUsers() throws Exception {
   long[] ids = new long[100];
   for (int i = 0; i < 100; i++) {
     ids[i] = i;
   }
   LongPrimitiveIterator possibleItemIds = new LongPrimitiveArrayIterator(ids);
   TopItems.Estimator<Long> estimator =
       new TopItems.Estimator<Long>() {
         @Override
         public double estimate(Long thing) {
           return thing;
         }
       };
   long[] topItems = TopItems.getTopUsers(10, possibleItemIds, null, estimator);
   int gold = 99;
   for (long topItem : topItems) {
     assertEquals(gold--, topItem);
   }
 }
예제 #5
0
 @Test
 public void testTopItems() throws Exception {
   long[] ids = new long[100];
   for (int i = 0; i < 100; i++) {
     ids[i] = i;
   }
   LongPrimitiveIterator possibleItemIds = new LongPrimitiveArrayIterator(ids);
   TopItems.Estimator<Long> estimator =
       new TopItems.Estimator<Long>() {
         @Override
         public double estimate(Long thing) {
           return thing;
         }
       };
   List<RecommendedItem> topItems = TopItems.getTopItems(10, possibleItemIds, null, estimator);
   int gold = 99;
   for (RecommendedItem topItem : topItems) {
     assertEquals(gold, topItem.getItemID());
     assertEquals(gold--, topItem.getValue(), 0.01);
   }
 }
예제 #6
0
 @Test
 public void testTopItemsRandom() throws Exception {
   long[] ids = new long[100];
   for (int i = 0; i < 100; i++) {
     ids[i] = i;
   }
   LongPrimitiveIterator possibleItemIds = new LongPrimitiveArrayIterator(ids);
   final Random random = RandomUtils.getRandom();
   TopItems.Estimator<Long> estimator =
       new TopItems.Estimator<Long>() {
         @Override
         public double estimate(Long thing) {
           return random.nextDouble();
         }
       };
   List<RecommendedItem> topItems = TopItems.getTopItems(10, possibleItemIds, null, estimator);
   assertEquals(10, topItems.size());
   double last = 2.0;
   for (RecommendedItem topItem : topItems) {
     assertTrue(topItem.getValue() <= last);
     last = topItem.getItemID();
   }
 }