@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") @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(); }
@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); }