Esempio n. 1
0
  /**
   * Gets the most recently added item not rated yet (new item in the platform).
   *
   * @return The most recently added item not rated yet.
   */
  public Long getLastItemAddedNotRated() {
    Long lastItemAdded = null;
    Date threshold = null;

    for (Long itemId : idao.getItemIds()) {
      List<Rating> ratings = iedao.getEventsForItem(itemId, Rating.class);
      Date firstTimestamp = new Date(ratings.get(0).getTimestamp() * 1000);
      if (threshold == null) {
        threshold = firstTimestamp;
        lastItemAdded = itemId;
      }

      for (Rating r : ratings) {
        Date timestamp = new Date(r.getTimestamp() * 1000);
        if (timestamp.before(firstTimestamp)) firstTimestamp = timestamp;
      }

      if (firstTimestamp.after(threshold)) {
        threshold = firstTimestamp;
        lastItemAdded = itemId;
      }
    }

    return lastItemAdded;
  }
  /**
   * Build the SVD model.
   *
   * @return A singular value decomposition recommender model.
   */
  @Override
  public SVDModel get() {
    // Create index mappings of user and item IDs.
    // You can use these to find row and columns in the matrix based on user/item IDs.
    IdIndexMapping userMapping = IdIndexMapping.create(userDAO.getUserIds());
    logger.debug("indexed {} users", userMapping.size());
    IdIndexMapping itemMapping = IdIndexMapping.create(itemDAO.getItemIds());
    logger.debug("indexed {} items", itemMapping.size());

    // We have to do 2 things:
    // First, prepare a matrix containing the rating data.
    RealMatrix matrix = createRatingMatrix(userMapping, itemMapping);

    // Second, compute its factorization
    // All the work is done in the constructor
    SingularValueDecomposition svd = new SingularValueDecomposition(matrix);

    // Third, truncate the decomposed matrix
    // TODO Truncate the matrices and construct the SVD model
    RealMatrix userMatrix = svd.getU();
    RealMatrix weights = svd.getS();
    RealMatrix itemMatrix = svd.getV();

    userMatrix = userMatrix.getSubMatrix(0, userMatrix.getRowDimension() - 1, 0, featureCount - 1);
    weights = weights.getSubMatrix(0, featureCount - 1, 0, featureCount - 1);
    itemMatrix = itemMatrix.getSubMatrix(0, itemMatrix.getRowDimension() - 1, 0, featureCount - 1);

    return new SVDModel(userMapping, itemMapping, userMatrix, itemMatrix, weights);
  }
Esempio n. 3
0
  /**
   * Gets the most popular item, i.e. the item with the greatest number of ratings, in a certain
   * period of time (last week/month/year/ever).
   *
   * @param period Period of time to consider.
   * @return The most popular item.
   */
  @SuppressWarnings("deprecation")
  public Long getMostPopularItem(Period period) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(getLastTimestamp());

    Date thresholdDate = null;
    switch (period) {
      case LAST_WEEK:
        cal.add(Calendar.DATE, -7);
        thresholdDate = cal.getTime();
        break;
      case LAST_MONTH:
        cal.add(Calendar.DATE, -30);
        thresholdDate = cal.getTime();
        break;
      case LAST_YEAR:
        cal.add(Calendar.DATE, -365);
        thresholdDate = cal.getTime();
        break;
      case EVER:
        break;
    }

    Long idItemMostPopular = null;
    int max = 0;
    for (Long itemId : idao.getItemIds()) {
      List<Event> events = iedao.getEventsForItem(itemId);
      int count = 0;
      if (thresholdDate == null) count = events.size();
      else {
        thresholdDate.setHours(0);
        thresholdDate.setMinutes(0);
        thresholdDate.setSeconds(0);
        for (Event ev : events) {
          Date rateDate = new Date(ev.getTimestamp() * 1000);
          if (rateDate.after(thresholdDate)) count++;
        }
      }

      if (count > max) {
        idItemMostPopular = itemId;
        max = count;
      }
    }

    return idItemMostPopular;
  }
Esempio n. 4
0
  /**
   * Gets the last positively rated item (the last item added in the platform rated in a positive
   * way). An item is "positively rated" if its rate is equals or greater than the global mean of
   * all ratings.
   *
   * @return The last positively rated item.
   */
  public Long getLastPositivelyRatedItem() {
    Long lastPositivelyRatedItem = null;
    Date recentDate = getFirstTimestamp();

    for (Long itemId : idao.getItemIds()) {
      List<Rating> ratings = iedao.getEventsForItem(itemId, Rating.class);
      int threshold = getPositiveRatingThreshold(ratings);
      Date date = getFirstTimestamp();

      for (Rating rating : ratings) {
        Date dateR = new Date(rating.getTimestamp() * 1000);
        if (rating.getValue() >= threshold && dateR.after(date)) date = dateR;
      }

      if (date.after(recentDate)) recentDate = date;
      lastPositivelyRatedItem = itemId;
    }

    return lastPositivelyRatedItem;
  }