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