/**
   * Build a rating matrix from the rating data. Each user's ratings are first normalized by
   * subtracting a baseline score (usually a mean).
   *
   * @param userMapping The index mapping of user IDs to column numbers.
   * @param itemMapping The index mapping of item IDs to row numbers.
   * @return A matrix storing the <i>normalized</i> user ratings.
   */
  private RealMatrix createRatingMatrix(IdIndexMapping userMapping, IdIndexMapping itemMapping) {
    final int nusers = userMapping.size();
    final int nitems = itemMapping.size();

    // Create a matrix with users on rows and items on columns
    logger.info("creating {} by {} rating matrix", nusers, nitems);
    RealMatrix matrix = MatrixUtils.createRealMatrix(nusers, nitems);

    // populate it with data
    Cursor<UserHistory<Event>> users = userEventDAO.streamEventsByUser();
    try {
      for (UserHistory<Event> user : users) {
        // Get the row number for this user
        int u = userMapping.getIndex(user.getUserId());
        MutableSparseVector ratings = Ratings.userRatingVector(user.filter(Rating.class));
        MutableSparseVector baselines = MutableSparseVector.create(ratings.keySet());
        baselineScorer.score(user.getUserId(), baselines);
        // TODO Populate this user's row with their ratings, minus the baseline scores
        for (VectorEntry entry : ratings.fast(State.SET)) {
          long itemid = entry.getKey();
          int i = itemMapping.getIndex(itemid);
          double rating = entry.getValue();
          double baseline = baselines.get(itemid);
          matrix.setEntry(u, i, rating - baseline);
        }
      }
    } finally {
      users.close();
    }

    return matrix;
  }
  /**
   * Get a user's ratings.
   *
   * @param user The user ID.
   * @return The ratings to retrieve.
   */
  private SparseVector getUserRatingVector(long user) {
    UserHistory<Rating> history = userEvents.getEventsForUser(user, Rating.class);
    if (history == null) {
      history = History.forUser(user);
    }

    return RatingVectorUserHistorySummarizer.makeRatingVector(history);
  }
  /**
   * It is used to generate rating list from UserEventDAO.
   *
   * @param uid The user ID.
   * @param dao The UserEventDAO.
   * @return The VectorEntry list of rating.
   */
  private SparseVector makeUserVector(long uid, UserEventDAO dao) {
    UserHistory<Rating> history = dao.getEventsForUser(uid, Rating.class);
    SparseVector vector = null;
    if (history != null) {
      vector = RatingVectorUserHistorySummarizer.makeRatingVector(history);
    }

    return vector;
  }
Exemple #4
0
 @Override
 protected TestUser getUserResults(long uid) {
   Preconditions.checkState(recommender != null, "recommender not built");
   UserHistory<Event> userData = userEvents.getEventsForUser(uid);
   return new LenskitTestUser(recommender, userData);
 }