@Test
  public void testSnapshot() throws RecommenderBuildException {
    List<Rating> rs = new ArrayList<Rating>();
    rs.add(Ratings.make(1, 5, 2));
    rs.add(Ratings.make(1, 7, 4));
    rs.add(Ratings.make(8, 4, 5));
    rs.add(Ratings.make(8, 5, 4));

    EventDAO dao = EventCollectionDAO.create(rs);

    LenskitConfiguration config = new LenskitConfiguration();
    config.bind(EventDAO.class).to(dao);
    config.bind(ItemScorer.class).to(UserUserItemScorer.class);
    config.bind(NeighborFinder.class).to(SnapshotNeighborFinder.class);

    LenskitRecommenderEngine engine = LenskitRecommenderEngine.build(config);
    Recommender rec = engine.createRecommender();
    assertThat(rec.getItemScorer(), instanceOf(UserUserItemScorer.class));
    assertThat(rec.getItemRecommender(), instanceOf(TopNItemRecommender.class));
    RatingPredictor pred = rec.getRatingPredictor();
    assertThat(pred, instanceOf(SimpleRatingPredictor.class));

    Recommender rec2 = engine.createRecommender();
    assertThat(rec2.getItemScorer(), not(sameInstance(rec.getItemScorer())));
  }
  @SuppressWarnings("deprecation")
  @Test
  public void testUserUserRecommenderEngineCreate() {
    Recommender rec = engine.createRecommender();

    assertThat(rec.getItemScorer(), instanceOf(UserUserItemScorer.class));
    assertThat(rec.getItemRecommender(), instanceOf(TopNItemRecommender.class));
    RatingPredictor pred = rec.getRatingPredictor();
    assertThat(pred, instanceOf(SimpleRatingPredictor.class));
    assertThat(((SimpleRatingPredictor) pred).getScorer(), sameInstance(rec.getItemScorer()));
  }
示例#3
0
  public static void main(String[] args) throws RecommenderBuildException {
    LenskitConfiguration config = configureRecommender();

    logger.info("building recommender");
    Recommender rec = LenskitRecommender.build(config);

    if (args.length == 0) {
      logger.error("No users specified; provide user IDs as command line arguments");
    }

    // we automatically get a useful recommender since we have a scorer
    ItemRecommender irec = rec.getItemRecommender();
    assert irec != null;
    try {
      // Generate 5 recommendations for each user
      for (String user : args) {
        long uid;
        try {
          uid = Long.parseLong(user);
        } catch (NumberFormatException e) {
          logger.error("cannot parse user {}", user);
          continue;
        }
        logger.info("searching for recommendations for user {}", user);
        List<ScoredId> recs = irec.recommend(uid, 5);
        if (recs.isEmpty()) {
          logger.warn("no recommendations for user {}, do they exist?", uid);
        }
        System.out.format("recommendations for user %d:\n", uid);
        for (ScoredId id : recs) {
          System.out.format("  %d: %.4f\n", id.getId(), id.getScore());
        }
      }
    } catch (UnsupportedOperationException e) {
      if (e.getMessage().equals("stub implementation")) {
        System.out.println("Congratulations, the stub builds and runs!");
      }
    }
  }