/** * 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; } }
/** * 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; }
@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; }
@Test public void testEmptyURV() { List<Rating> ratings = Collections.emptyList(); Long2DoubleMap urv = Ratings.userRatingVector(ratings); assertThat(urv.isEmpty(), equalTo(true)); assertThat(urv.size(), equalTo(0)); }
@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)); }
@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)); }
/** * 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; } }
@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; }