/** * Get the user's test ratings. * Summarizes the user's ratings from the history. * * @return The user's ratings for the test items. */ public Long2DoubleMap getTestRatings() { if (testRatings == null) { Predicate<Entity> predicate = Entities.typePredicate(CommonTypes.RATING); Function<Entity, Rating> targetViewClass = Entities.projection(Rating.class); ImmutableList<Rating> list = FluentIterable.from(testHistory).filter(predicate).transform(targetViewClass).toList(); testRatings = Ratings.userRatingVector(list); } return testRatings; } }
/** * Construct a rating vector that contains the ratings provided for each * item. If all ratings in <var>ratings</var> are by the same user, then * this will be a valid user rating vector. * * @param ratings A collection of ratings (should all be by the same user) * @return A sparse vector mapping item IDs to ratings */ public static Long2DoubleMap userRatingVector(@Nonnull Collection<Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.ITEM, ratings.size()); }
private static Matcher<Preference> samePreferenceAs(Preference p) { return equivalentTo(p, Ratings.preferenceEquivalence()); }
@Test public void testEmptyIRV() { List<Rating> ratings = Collections.emptyList(); Long2DoubleMap urv = Ratings.itemRatingVector(ratings); assertThat(urv.isEmpty(), equalTo(true)); assertThat(urv.size(), equalTo(0)); }
@Test @SuppressWarnings("deprecation") public void testDeprecatedFactories() { Rating rating = Ratings.make(1, 2, 3.0); Rating withTS = Ratings.make(1, 2, 3.0, 1030); assertThat(rating.getUserId(), equalTo(1L)); assertThat(rating.getItemId(), equalTo(2L)); assertThat(rating.getValue(), equalTo(3.0)); assertThat(withTS.getUserId(), equalTo(1L)); assertThat(withTS.getItemId(), equalTo(2L)); assertThat(withTS.getValue(), equalTo(3.0)); assertThat(withTS.getTimestamp(), equalTo(1030L)); }
@Test public void testIRVRatings() { List<Rating> ratings = new ArrayList<>(); ratings.add(Rating.create(1, 2, 3.0, 1)); ratings.add(Rating.create(2, 2, 2.3, 3)); ratings.add(Rating.create(3, 2, 4.5, 10)); Long2DoubleMap urv = Ratings.itemRatingVector(ratings); assertThat(urv.isEmpty(), equalTo(false)); assertThat(urv.size(), equalTo(3)); assertThat(urv.get(1), closeTo(3.0, 1.0e-6)); assertThat(urv.get(3), closeTo(4.5, 1.0e-6)); assertThat(urv.get(2), closeTo(2.3, 1.0e-6)); assertThat(urv.containsKey(5), equalTo(false)); }
/** * Extract a user vector from a data source. * * @param uid The user ID. * @param dao The DAO. * @return The user rating vector. */ private Long2DoubleMap makeUserVector(long uid, DataAccessObject dao) { List<Rating> history = dao.query(Rating.class) .withAttribute(CommonAttributes.USER_ID, uid) .get(); Long2DoubleMap vector = null; if (!history.isEmpty()) { vector = Ratings.userRatingVector(history); } return vector; }
/** * Construct a rating vector that contains the ratings provided by each user. * If all ratings in <var>ratings</var> are for the same item, then this * will be a valid item rating vector. If multiple ratings are by the same * user, the one with the highest timestamp is retained. If two ratings * by the same user have identical timestamps, then the one that occurs last * when the collection is iterated is retained. * * @param ratings Some ratings (they should all be for the same item) * @return A sparse vector mapping user IDs to ratings. */ public static Long2DoubleMap itemRatingVector(@Nonnull Collection<? extends Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.USER, ratings.size()); }
/** * 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); } }
@Test public void testGetUserRatings() { Collection<RatingMatrixEntry> ratings = snap.getUserRatings(1); Equivalence<Preference> eq = Ratings.preferenceEquivalence(); assertThat(ratings, hasSize(4)); assertThat(Iterables.transform(ratings, eq::wrap),
@Nonnull @Override public Long2DoubleMap userRatingVector(long user) { IdBox<Long2DoubleMap> cached = cachedValue; if (cached != null && cached.getId() == user) { return cached.getValue(); } Long2DoubleMap map; try (ObjectStream<Rating> stream = dao.query(Rating.class) .withAttribute(CommonAttributes.USER_ID, user) .stream()) { map = Ratings.userRatingVector(stream); } return map; }
/** * Extract a user rating vector from a rating stream. * * @param ratings The rating stream. * @return The user rating vector. * @see #userRatingVector(Collection) */ public static Long2DoubleMap userRatingVector(@WillClose ObjectStream<? extends Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.ITEM, -1); }
@Test public void testEmptyURV() { List<Rating> ratings = Collections.emptyList(); Long2DoubleMap urv = Ratings.userRatingVector(ratings); assertThat(urv.isEmpty(), equalTo(true)); assertThat(urv.size(), equalTo(0)); }
/** * Construct a rating vector that contains the ratings provided for each * item. If all ratings in <var>ratings</var> are by the same user, then * this will be a valid user rating vector. * * @param ratings A collection of ratings (should all be by the same user) * @return A sparse vector mapping item IDs to ratings */ public static Long2DoubleMap userRatingVector(@Nonnull Collection<Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.ITEM, ratings.size()); }
@Test public void testURVRatingsInOrder() { List<Rating> ratings = new ArrayList<>(); ratings.add(Rating.create(1, 2, 3.0, 3)); ratings.add(Rating.create(1, 3, 4.5, 7)); ratings.add(Rating.create(1, 5, 2.3, 10)); Long2DoubleMap urv = Ratings.userRatingVector(ratings); assertThat(urv.isEmpty(), equalTo(false)); assertThat(urv.size(), equalTo(3)); assertThat(urv.get(2), closeTo(3.0, 1.0e-6)); assertThat(urv.get(3), closeTo(4.5, 1.0e-6)); assertThat(urv.get(5), closeTo(2.3, 1.0e-6)); assertThat(urv.containsKey(1), equalTo(false)); }
/** * Construct a rating vector that contains the ratings provided by each user. * If all ratings in <var>ratings</var> are for the same item, then this * will be a valid item rating vector. If multiple ratings are by the same * user, the one with the highest timestamp is retained. If two ratings * by the same user have identical timestamps, then the one that occurs last * when the collection is iterated is retained. * * @param ratings Some ratings (they should all be for the same item) * @return A sparse vector mapping user IDs to ratings. */ public static Long2DoubleMap itemRatingVector(@Nonnull Collection<? extends Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.USER, ratings.size()); }
@Test public void testURVRatingsOutOfOrder() { List<Rating> ratings = new ArrayList<>(); ratings.add(Rating.create(1, 2, 3.0, 3)); ratings.add(Rating.create(1, 5, 2.3, 7)); ratings.add(Rating.create(1, 3, 4.5, 10)); Long2DoubleMap urv = Ratings.userRatingVector(ratings); assertThat(urv.isEmpty(), equalTo(false)); assertThat(urv.size(), equalTo(3)); assertThat(urv.get(2), closeTo(3.0, 1.0e-6)); assertThat(urv.get(3), closeTo(4.5, 1.0e-6)); assertThat(urv.get(5), closeTo(2.3, 1.0e-6)); assertThat(urv.containsKey(1), equalTo(false)); }
/** * Extract a user rating vector from a rating stream. * * @param ratings The rating stream. * @return The user rating vector. * @see #userRatingVector(Collection) */ public static Long2DoubleMap userRatingVector(@WillClose ObjectStream<? extends Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.ITEM, -1); }
/** * Test method for {@link Ratings#userRatingVector(java.util.Collection)}. */ @Test public void testUserRatingVector() { Collection<Rating> ratings = Lists.newArrayList( Rating.create(5, 7, 3.5), Rating.create(5, 3, 1.5), Rating.create(5, 8, 2) ); Long2DoubleMap v = Ratings.userRatingVector(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); } }
/** * Get the user's test ratings. * * Summarizes the user's ratings from the history. * * @return The user's ratings for the test items. */ public Long2DoubleMap getTestRatings() { if (testRatings == null) { testRatings = Ratings.userRatingVector(testHistory.filter(Rating.class)); } return testRatings; } }