/** * Make a fresh rating object with no timestamp. * * @see #make(long, long, double, long) * @deprecated Use {@link Rating#create(long, long, double)}. */ @Deprecated public static Rating make(long uid, long iid, double value) { return new RatingBuilder().setUserId(uid) .setItemId(iid) .setRating(value) .build(); }
@Test public void testSetUserId() { RatingBuilder rb = new RatingBuilder(); rb.setUserId(42); assertThat(rb.getUserId(), equalTo(42L)); }
@Test public void testSetRating() { RatingBuilder rb = new RatingBuilder(); rb.setRating(3.5); assertThat(rb.getRating(), equalTo(3.5)); }
@Test public void testSetItemId() { RatingBuilder rb = new RatingBuilder(); rb.setItemId(42); assertThat(rb.getItemId(), equalTo(42L)); }
@Test public void testSetTimestamp() { RatingBuilder rb = new RatingBuilder(); rb.setTimestamp(235909); assertThat(rb.getTimestamp(), equalTo(235909L)); }
@Before public void initialize() throws IOException { predictOutputFile = folder.newFile("predictions.csv"); List<Rating> ratings = new ArrayList<>(); Generator<Rating> rgen = LenskitGenerators.ratings(); Set<Pair<Long,Long>> used = new HashSet<>(); while (ratings.size() < RATING_COUNT) { Rating r = rgen.next(); long uid = r.getUserId() % 5; Pair<Long,Long> ui = ImmutablePair.of(uid, r.getItemId()); if (used.contains(ui)) { continue; } used.add(ui); Rating r2 = r.copyBuilder() .setUserId(r.getUserId() % 5) .build(); ratings.add(r2); } assumeThat(ratings, hasSize(RATING_COUNT)); dao = StaticDataSource.fromList(ratings).get(); LenskitConfiguration config = new LenskitConfiguration(); config.bind(ItemScorer.class).to(UserMeanItemScorer.class); config.bind(UserMeanBaseline.class, ItemScorer.class).to(ItemMeanRatingItemScorer.class); tempEval.setRebuildPeriod(1L); tempEval.setDataSource(dao); tempEval.setAlgorithm("UserMeanBaseline", config); tempEval.setOutputFile(predictOutputFile); }
@Test public void testInitialState() { RatingBuilder rb = new RatingBuilder(); try { rb.build(); fail("building a rating should fail"); } catch (IllegalStateException e) { /* expected */ } }
@EntityAttributeClearer("timestamp") public RatingBuilder clearTimestamp() { return setTimestamp(-1); }
/** * Create a new rating builder. * @return A new rating builder. */ public static RatingBuilder newBuilder() { return new RatingBuilder(); }
@EntityAttributeClearer("timestamp") public RatingBuilder clearTimestamp() { return setTimestamp(-1); }
/** * Create a new rating builder. * @return A new rating builder. */ public static RatingBuilder newBuilder() { return new RatingBuilder(); }
/** * Make a fresh rating event. * * @deprecated Use {@link Rating#create(long, long, double, long)} */ @Deprecated public static Rating make(long uid, long iid, double value, long ts) { return new RatingBuilder().setUserId(uid) .setItemId(iid) .setRating(value) .setTimestamp(ts) .build(); } }
/** * Test method for {@link Ratings#itemRatingVector(java.util.Collection)}. */ @Test public void testItemRatingVector() { Collection<Rating> ratings = new ArrayList<>(); ratings.add(Rating.create(7, 5, 3.5)); RatingBuilder rb = new RatingBuilder(); ratings.add(Rating.create(3, 5, 1.5)); ratings.add(Rating.create(8, 5, 2)); Long2DoubleMap v = Ratings.itemRatingVector(ratings); assertEquals(3, v.size()); assertEquals(7, Vectors.sum(v), EPSILON); long[] keys = {3, 7, 8}; double[] values = {1.5, 3.5, 2}; Long2DoubleSortedArrayMap sv = Long2DoubleSortedArrayMap.wrap(SortedKeyIndex.create(keys), values); assertEquals(sv, v); } }
/** * Create a new rating builder that will build a copy of this rating. * @return A rating builder initialized with the contents of this rating. */ public RatingBuilder copyBuilder() { RatingBuilder rb = new RatingBuilder(); rb.setUserId(user) .setItemId(item) .setTimestamp(getTimestamp()); double v = getValue(); if (!Double.isNaN(v)) { rb.setRating(v); } return rb; }
@Override public Rating next() { long id = ids.next(); while (usedIds.contains(id)) { id = ids.next(); } usedIds.add(id); RatingBuilder rb = Rating.newBuilder() .setId(id) .setUserId(userIds.next()) .setItemId(itemIds.next()) .setRating(values.next()); if (timestamps != null) { rb.setTimestamp(timestamps.next()); } return rb.build(); } }
@SuppressWarnings("deprecation") @Test /* * Test method that tests unrated items for a user in the data set, * it shouldn't return any value. * */ @Ignore("unrates are going away") public void testPredictForUnratedItems() { RatingBuilder rb = new RatingBuilder().setUserId(420); rs.add(rb.setItemId(840).setRating(3.5).setTimestamp(10).build()); rs.add(rb.setItemId(390).setRating(4.5).setTimestamp(20).build()); rs.add(rb.setItemId(840).clearRating().setTimestamp(30).build()); dao = StaticDataSource.fromList(rs).get(); KnownRatingRatingPredictor pred = new KnownRatingRatingPredictor(dao); Map<Long,Double> results = pred.predict(420, LongUtils.packedSet(840, 390)); assertThat(results, hasEntry(390L, 4.5)); assertThat(results.keySet(), not(Matchers.hasItem(840L))); } }
@Test public void testBuildRating() { Rating r = new RatingBuilder() .setUserId(692) .setItemId(483) .setRating(3.5) .setTimestamp(349702) .build(); assertThat(r, notNullValue()); assertThat(r.getUserId(), equalTo(692L)); assertThat(r.getItemId(), equalTo(483L)); assertThat(r.getValue(), equalTo(3.5)); assertThat(r.getTimestamp(), equalTo(349702L)); } }