コード例 #1
0
ファイル: TestRecommender.java プロジェクト: OliverBi/YueYun
  public static void main(String[] args) throws Exception {
    /*DataModel model = new FileDataModel(new File("data.csv"));

    UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

    UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model);

    Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);

    List<RecommendedItem> recommendations = recommender.recommend(1, 1);

    for(RecommendedItem recommendation : recommendations){
    	System.out.println(recommendation);
    }*/

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setServerName("localhost");
    dataSource.setPort(3306);
    dataSource.setDatabaseName("YueYun");
    dataSource.setUser("root");
    dataSource.setPassword("root");

    JDBCDataModel dataModel =
        new MySQLJDBCDataModel(
            dataSource, "tb_track_nopreference", "userId", "trackId", "nopreference", "");

    /*UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);

    UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, dataModel);

    Recommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, similarity);

          List<RecommendedItem> recommendations = recommender.recommend(1, 1);

          for(RecommendedItem recommendation : recommendations){
              System.out.println(recommendation);
          }*/

    DataModel model =
        new GenericBooleanPrefDataModel(GenericBooleanPrefDataModel.toDataMap(dataModel));
    UserSimilarity similarity = new LogLikelihoodSimilarity(model);

    UserNeighborhood neighborhood = new NearestNUserNeighborhood(4, similarity, model);

    Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);

    List<RecommendedItem> recommendations = recommender.recommend(1, 3);

    for (RecommendedItem recommendation : recommendations) {
      System.out.println(recommendation);
    }
  }
コード例 #2
0
  public static void main(String[] args)
      throws TasteException, UnknownHostException, MongoException {
    model =
        new MongoDBDataModel(
            "127.0.0.1",
            27017,
            "right-channel",
            "ratings",
            false,
            false,
            null,
            "user_id",
            "movie_id",
            "rating",
            MongoDBDataModel.DEFAULT_MONGO_MAP_COLLECTION);

    ItemSimilarity similarity = new PearsonCorrelationSimilarity(model);
    Recommender recommender = new BiasedItemBasedRecommender(model, similarity);
    IDRescorer rescorer = new OverallRescorer();
    List<RecommendedItem> recommendations =
        recommender.recommend(
            Long.parseLong(
                model.fromIdToLong(
                    new ObjectId("518ce9df4597b5adead4b61d").toStringMongod(), false)),
            1000,
            rescorer);

    Mongo mongoDB = new Mongo("127.0.0.1", 27017);
    DB db = mongoDB.getDB("right-channel");
    DBCollection recommendationCollection = db.getCollection("recommendations");

    BasicDBObject document = new BasicDBObject();
    document.put("user_id", new ObjectId("518ce9df4597b5adead4b61d"));
    recommendationCollection.remove(document);

    for (RecommendedItem recommendation : recommendations) {
      long movieIdLong = recommendation.getItemID();
      String movieIdString = model.fromLongToId(movieIdLong);
      ObjectId movieIdObject = new ObjectId(movieIdString);
      double rating = recommendation.getValue();

      BasicDBObject prediction = new BasicDBObject();
      Object userIdObject = new ObjectId("518ce9df4597b5adead4b61d");
      prediction.put("user_id", userIdObject);
      prediction.put("movie_id", movieIdObject);
      prediction.put("rating", rating);
      recommendationCollection.insert(prediction);

      System.out.println(rating);
    }
  }
コード例 #3
0
ファイル: UserBaseCF.java プロジェクト: rockiee281/JavaUtils
  public static void main(String[] args) throws IOException, Exception {
    DataModel dataModel =
        new FileDataModel(
            new File(UserBaseCF.class.getClassLoader().getResource("mydata.txt").getFile()));
    UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(dataModel);
    userSimilarity.setPreferenceInferrer(new AveragingPreferenceInferrer(dataModel));

    UserNeighborhood neighborhood = new NearestNUserNeighborhood(3, userSimilarity, dataModel);

    Recommender recommender =
        new GenericUserBasedRecommender(dataModel, neighborhood, userSimilarity);
    Recommender cachingRecommender = new CachingRecommender(recommender);
    List<RecommendedItem> recommendations = cachingRecommender.recommend(5, 5);
    for (RecommendedItem recommendedItem : recommendations) {
      System.out.println(recommendedItem);
    }
  }
コード例 #4
0
  public static void main(String[] args) throws IOException, TasteException {
    String file = "datafile/item.csv";
    DataModel model = new FileDataModel(new File(file));
    UserSimilarity user = new EuclideanDistanceSimilarity(model);
    NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, user, model);
    Recommender r = new GenericUserBasedRecommender(model, neighbor, user);
    LongPrimitiveIterator iter = model.getUserIDs();

    while (iter.hasNext()) {
      long uid = iter.nextLong();
      List<RecommendedItem> list = r.recommend(uid, RECOMMENDER_NUM);
      System.out.printf("uid:%s", uid);
      for (RecommendedItem ritem : list) {
        System.out.printf("(%s,%f)", ritem.getItemID(), ritem.getValue());
      }
      System.out.println();
    }
  }
コード例 #5
0
  public static String userbasecrec(String files)
      throws FileNotFoundException, TasteException, IOException, OptionException {

    // DataModel model = new FileDataModel(new File("datasets/movieRatings.dat"));

    File ratingsFile = new File(files);
    DataModel model = new FileDataModel(ratingsFile);

    UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
    UserNeighborhood neighborhood = new NearestNUserNeighborhood(3, similarity, model);
    Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
    List<RecommendedItem> recommendations = recommender.recommend(1, 1);

    String ls = "";
    for (RecommendedItem recommendation : recommendations) {
      System.out.println(recommendation);
      ls = recommendation + "";
    }
    return ls;
  }
コード例 #6
0
  public static void main(String[] args) throws TasteException, MongoException, IOException {
    String ml10mFolder = "/home/yapianyu/Desktop/movielens/ml-10M100K/refinement/";
    String ratingFilePath = ml10mFolder + "ratings.dat";
    String movieFilePath = ml10mFolder + "movies.dat";

    for (String line : new FileLineIterable(new File(movieFilePath), false)) {
      String[] fields = line.split("::");
      long id = Long.parseLong(fields[0]);
      long year =
          Long.parseLong(fields[1].substring(fields[1].length() - 5, fields[1].length() - 1));
      String title = fields[3];
      try {
        double rating = Double.parseDouble(fields[4]);
        mid2rating.put(id, rating);
      } catch (Exception e) {
        mid2rating.put(id, Double.NaN);
      }
      mid2year.put(id, year);
      mid2title.put(id, title);
    }

    DataModel model = new FileDataModel(new File(ratingFilePath));
    ItemSimilarity similarity = new PearsonCorrelationSimilarity(model);
    Recommender recommender = new BiasedItemBasedRecommender(model, similarity);
    IDRescorer rescorer = new OverallRescorer();
    List<RecommendedItem> recommendations = recommender.recommend(72000, 1000, rescorer);
    for (RecommendedItem recommendation : recommendations) {
      long mid = recommendation.getItemID();
      System.out.println(
          String.format(
              "%.3f\t%d\t%.1f\t%s(%d)",
              recommendation.getValue(),
              mid2year.get(mid),
              mid2rating.get(mid),
              mid2title.get(mid),
              mid));
    }
  }
コード例 #7
0
  public static void main(String[] args)
      throws FileNotFoundException, TasteException, IOException, OptionException {
    DataModel model;
    model = new FileDataModel(new File("datasets/ratingsForMahout.dat"));

    File movieMapFile = new File("datasets/moviesForMahout.dat");
    HashMap<Long, String> movieMap = new HashMap<Long, String>();
    Scanner scan = new Scanner(movieMapFile);
    while (scan.hasNextLine()) {
      String[] line = scan.nextLine().split("\\|");
      movieMap.put(Long.parseLong(line[0]), line[1]);
    }
    scan.close();

    UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(model);
    UserNeighborhood neighborhood = new NearestNUserNeighborhood(3, userSimilarity, model);
    Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, userSimilarity);
    Recommender cachingRecommender = new CachingRecommender(recommender);

    for (LongPrimitiveIterator it = model.getUserIDs(); it.hasNext(); ) {
      long userId = it.nextLong();
      List<RecommendedItem> recommendations = cachingRecommender.recommend(userId, 10);
      if (recommendations.size() == 0) {
        System.out.println("User " + userId + ": no recommendations");
      }
      for (RecommendedItem recommendedItem : recommendations) {
        System.out.println(
            "User "
                + userId
                + ": "
                + movieMap.get(recommendedItem.getItemID())
                + "; value="
                + recommendedItem.getValue());
      }
    }
  }
コード例 #8
0
  @Test
  public void testRecommender() throws Exception {
    MutableInt recommendCount = new MutableInt();
    Recommender mockRecommender = new MockRecommender(recommendCount);

    Recommender cachingRecommender = new CachingRecommender(mockRecommender);
    cachingRecommender.recommend(1, 1);
    assertEquals(1, recommendCount.intValue());
    cachingRecommender.recommend(2, 1);
    assertEquals(2, recommendCount.intValue());
    cachingRecommender.recommend(1, 1);
    assertEquals(2, recommendCount.intValue());
    cachingRecommender.recommend(2, 1);
    assertEquals(2, recommendCount.intValue());
    cachingRecommender.refresh(null);
    cachingRecommender.recommend(1, 1);
    assertEquals(3, recommendCount.intValue());
    cachingRecommender.recommend(2, 1);
    assertEquals(4, recommendCount.intValue());
    cachingRecommender.recommend(3, 1);
    assertEquals(5, recommendCount.intValue());

    // Results from this recommend() method can be cached...
    IDRescorer rescorer = NullRescorer.getItemInstance();
    cachingRecommender.refresh(null);
    cachingRecommender.recommend(1, 1, rescorer);
    assertEquals(6, recommendCount.intValue());
    cachingRecommender.recommend(2, 1, rescorer);
    assertEquals(7, recommendCount.intValue());
    cachingRecommender.recommend(1, 1, rescorer);
    assertEquals(7, recommendCount.intValue());
    cachingRecommender.recommend(2, 1, rescorer);
    assertEquals(7, recommendCount.intValue());

    // until you switch Rescorers
    cachingRecommender.recommend(1, 1, null);
    assertEquals(8, recommendCount.intValue());
    cachingRecommender.recommend(2, 1, null);
    assertEquals(9, recommendCount.intValue());

    cachingRecommender.refresh(null);
    cachingRecommender.estimatePreference(1, 1);
    assertEquals(10, recommendCount.intValue());
    cachingRecommender.estimatePreference(1, 2);
    assertEquals(11, recommendCount.intValue());
    cachingRecommender.estimatePreference(1, 2);
    assertEquals(11, recommendCount.intValue());
  }
  @Override
  public List<RecommendedItem> recommend(
      long userID, int howMany, IDRescorer rescorer, boolean includeKnownItems)
      throws TasteException {

    Map<Long, Pair<Long, Float>> recommendedItemOccurrence = new HashMap<Long, Pair<Long, Float>>();
    if (criteriaList != null && recommenderMap != null) {
      for (RecommenderCriteriumEntity criterium : criteriaList) {
        Recommender recommender = recommenderMap.get(criterium);
        if (recommender != null) {
          List<RecommendedItem> recommendedItems =
              recommender.recommend(userID, howMany, rescorer, includeKnownItems);

          if (recommendedItems != null) {
            for (RecommendedItem recommendedItem : recommendedItems) {
              Pair<Long, Float> pair = recommendedItemOccurrence.get(recommendedItem.getItemID());
              if (pair == null) {
                pair = new ImmutablePair<Long, Float>(1L, recommendedItem.getValue());
              } else {
                pair =
                    new ImmutablePair<Long, Float>(
                        pair.getLeft() + 1, (pair.getRight() + recommendedItem.getValue()) / 2);
              }

              recommendedItemOccurrence.put(recommendedItem.getItemID(), pair);
            }
          }
        }
      }
    }

    PriorityQueue<ImmutablePair<RecommendedItem, Long>> queue =
        new PriorityQueue<ImmutablePair<RecommendedItem, Long>>(
            10,
            new Comparator<ImmutablePair<RecommendedItem, Long>>() {

              @Override
              public int compare(
                  ImmutablePair<RecommendedItem, Long> o1,
                  ImmutablePair<RecommendedItem, Long> o2) {
                int result = -o1.getRight().compareTo(o2.getRight());

                if (result == 0) {
                  Float v1 = o1.getLeft().getValue();
                  Float v2 = o2.getLeft().getValue();

                  result = v1.compareTo(v2);
                }

                return result;
              }
            });

    for (Map.Entry<Long, Pair<Long, Float>> entry : recommendedItemOccurrence.entrySet()) {
      RecommendedItem recommendedItem =
          new GenericRecommendedItem(entry.getKey(), entry.getValue().getRight());

      ImmutablePair<RecommendedItem, Long> pair =
          new ImmutablePair<RecommendedItem, Long>(recommendedItem, entry.getValue().getLeft());
      queue.add(pair);
    }

    List<RecommendedItem> recommendedItems = new ArrayList<RecommendedItem>();
    do {
      if (queue.peek() != null && queue.peek().getLeft() != null) {
        recommendedItems.add(queue.poll().getLeft());
      }
    } while (recommendedItems.size() < howMany && !queue.isEmpty());

    return recommendedItems;
  }