Exemplo n.º 1
0
  @Override
  public void execute() throws IOException, RecommenderBuildException {
    LenskitConfiguration dataConfig = input.getConfiguration();
    LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder();
    for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
      builder.addConfiguration(config);
    }
    builder.addConfiguration(dataConfig, ModelDisposition.EXCLUDED);

    Stopwatch timer = Stopwatch.createStarted();
    LenskitRecommenderEngine engine = builder.build();
    timer.stop();
    logger.info("built model in {}", timer);
    File output = getOutputFile();
    CompressionMode comp = CompressionMode.autodetect(output);
    logger.info("writing model to {}", output);
    Closer closer = Closer.create();
    try {
      OutputStream stream = closer.register(new FileOutputStream(output));
      stream = closer.register(comp.wrapOutput(stream));
      engine.write(stream);
    } catch (Throwable th) { // NOSONAR using a closer
      throw closer.rethrow(th);
    } finally {
      closer.close();
    }
  }
 @SuppressWarnings("deprecation")
 @Before
 public void setup() throws RecommenderBuildException {
   List<Rating> rs = new ArrayList<Rating>();
   rs.add(Rating.create(1, 6, 4));
   rs.add(Rating.create(2, 6, 2));
   rs.add(Rating.create(1, 7, 3));
   rs.add(Rating.create(2, 7, 2));
   rs.add(Rating.create(3, 7, 5));
   rs.add(Rating.create(4, 7, 2));
   rs.add(Rating.create(1, 8, 3));
   rs.add(Rating.create(2, 8, 4));
   rs.add(Rating.create(3, 8, 3));
   rs.add(Rating.create(4, 8, 2));
   rs.add(Rating.create(5, 8, 3));
   rs.add(Rating.create(6, 8, 2));
   rs.add(Rating.create(1, 9, 3));
   rs.add(Rating.create(3, 9, 4));
   EventCollectionDAO dao = new EventCollectionDAO(rs);
   LenskitConfiguration config = new LenskitConfiguration();
   config.bind(EventDAO.class).to(dao);
   config.bind(ItemScorer.class).to(ItemItemScorer.class);
   // this is the default
   config.bind(UserVectorNormalizer.class).to(DefaultUserVectorNormalizer.class);
   config.bind(VectorNormalizer.class).to(IdentityVectorNormalizer.class);
   LenskitRecommenderEngine engine = LenskitRecommenderEngine.build(config);
   session = engine.createRecommender();
   recommender = session.getItemRecommender();
 }
  @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())));
  }
Exemplo n.º 4
0
  @Override
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void execute() throws IOException, RecommenderBuildException {
    LenskitRecommenderEngine engine = loadEngine();

    long user = options.getLong("user");
    List<Long> items = options.get("items");

    LenskitRecommender rec = engine.createRecommender();
    RatingPredictor pred = rec.getRatingPredictor();
    if (pred == null) {
      logger.error("recommender has no rating predictor");
      throw new UnsupportedOperationException("no rating predictor");
    }

    logger.info("predicting {} items", items.size());
    Symbol pchan = getPrintChannel();
    Stopwatch timer = Stopwatch.createStarted();
    SparseVector preds = pred.predict(user, items);
    Long2ObjectMap channel = null;
    if (pchan != null) {
      for (TypedSymbol sym : preds.getChannelSymbols()) {
        if (sym.getRawSymbol().equals(pchan)) {
          channel = preds.getChannel(sym);
        }
      }
    }
    for (VectorEntry e : preds) {
      System.out.format("  %d: %.3f", e.getKey(), e.getValue());
      if (channel != null) {
        System.out.format(" (%s)", channel.get(e.getKey()));
      }
      System.out.println();
    }
    timer.stop();
    logger.info("predicted for {} items in {}", items.size(), timer);
  }
Exemplo n.º 5
0
 private LenskitRecommenderEngine loadEngine() throws RecommenderBuildException, IOException {
   File modelFile = options.get("model_file");
   if (modelFile == null) {
     logger.info("creating fresh recommender");
     LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder();
     for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
       builder.addConfiguration(config);
     }
     builder.addConfiguration(input.getConfiguration());
     Stopwatch timer = Stopwatch.createStarted();
     LenskitRecommenderEngine engine = builder.build();
     timer.stop();
     logger.info("built recommender in {}", timer);
     return engine;
   } else {
     logger.info("loading recommender from {}", modelFile);
     LenskitRecommenderEngineLoader loader = LenskitRecommenderEngine.newLoader();
     for (LenskitConfiguration config : environment.loadConfigurations(getConfigFiles())) {
       loader.addConfiguration(config);
     }
     loader.addConfiguration(input.getConfiguration());
     Stopwatch timer = Stopwatch.createStarted();
     LenskitRecommenderEngine engine;
     CompressionMode comp = CompressionMode.autodetect(modelFile);
     InputStream input = new FileInputStream(modelFile);
     try {
       input = comp.wrapInput(input);
       engine = loader.load(input);
     } finally {
       input.close();
     }
     timer.stop();
     logger.info("loaded recommender in {}", timer);
     return engine;
   }
 }
  @SuppressWarnings("deprecation")
  @Before
  public void setup() 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 = new EventCollectionDAO(rs);

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

    engine = LenskitRecommenderEngine.build(config);
  }