SHARE
TWEET

Untitled

a guest May 26th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package zad2;
  2.  
  3. import org.apache.mahout.cf.taste.common.NoSuchUserException;
  4. import org.apache.mahout.cf.taste.common.TasteException;
  5. import org.apache.mahout.cf.taste.eval.IRStatistics;
  6. import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
  7. import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
  8. import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
  9. import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
  10. import org.apache.mahout.cf.taste.impl.eval.RMSRecommenderEvaluator;
  11. import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
  12. import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
  13. import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
  14. import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
  15. import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
  16. import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
  17. import org.apache.mahout.cf.taste.model.DataModel;
  18. import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
  19. import org.apache.mahout.cf.taste.recommender.RecommendedItem;
  20. import org.apache.mahout.cf.taste.recommender.Recommender;
  21. import org.apache.mahout.cf.taste.similarity.UserSimilarity;
  22. import org.apache.mahout.common.RandomUtils;
  23.  
  24. import java.io.BufferedWriter;
  25. import java.io.File;
  26. import java.io.FileWriter;
  27. import java.util.List;
  28.  
  29. public class UserRecommender {
  30.  
  31.     public static final double TRAINING_PERCENTAGE = 0.35;
  32.     public static final double EVALUATION_PERCENTAGE = 0.65;
  33.     public static final String JESTER_RATINGS = "/home/marin/Dropbox/fax/4. godina/rovkp/dz3/jester_ratings.dat";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.  
  37.         RandomUtils.useTestSeed();
  38.  
  39.         //inicijaliziraj model učitavanjem podataka o korisnicima
  40.         DataModel model = new FileDataModel(new File(JESTER_RATINGS), "\t+");
  41.  
  42.         RecommenderBuilder paersonCorrelationBuilder = new RecommenderBuilder() {
  43.             @Override
  44.             public Recommender buildRecommender(DataModel model) throws TasteException {
  45.                 UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
  46.                 UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
  47.                 return new GenericUserBasedRecommender(model, neighborhood, similarity);
  48.             }
  49.         };
  50.  
  51.  
  52.         //nađi preporuke
  53.         List<RecommendedItem> recommendedItems = paersonCorrelationBuilder.buildRecommender(model).recommend(22, 10);
  54.         for (RecommendedItem recommendedItem : recommendedItems) {
  55.             System.out.println(recommendedItem);
  56.         }
  57.  
  58.         //evaluiraj procjenu za paerson correlation buildera
  59.         RecommenderEvaluator recEvaluator = new RMSRecommenderEvaluator();
  60.         double score = recEvaluator.evaluate(paersonCorrelationBuilder, null, model, TRAINING_PERCENTAGE, EVALUATION_PERCENTAGE);
  61.         System.out.println("Paerson correlation score = " + score);
  62.  
  63.  
  64.         //ispiši 10 preporuka prvih 100 korisnika na disk
  65.  
  66.         BufferedWriter writer = new BufferedWriter(new FileWriter("result.txt"));
  67.         BufferedWriter usersWriter = new BufferedWriter(new FileWriter("users.txt"));
  68.         for (int i = 0,userId=1; i < 100; userId++) {
  69.             try {
  70.                 String line = userId + "\t { ";
  71.                 List<RecommendedItem> recommendations = paersonCorrelationBuilder.buildRecommender(model).recommend(userId, 10);
  72.                 for (RecommendedItem recommendedItem : recommendations) {
  73.                     line += recommendedItem.getItemID() + " : " + recommendedItem.getValue() + ", ";
  74.                 }
  75.                 line += "}";
  76.                 writer.write(line);
  77.                 writer.newLine();
  78.  
  79.                 usersWriter.write(String.valueOf(userId));
  80.                 usersWriter.newLine();
  81.                 i++;
  82.             } catch (NoSuchUserException e) {
  83.                 System.err.println("No such user with id " + userId);
  84.             }
  85.  
  86.         }
  87.         writer.close();
  88.         usersWriter.close();
  89.  
  90.  
  91.         //log likelihood builder
  92.         RecommenderBuilder logLikelihoodBuilder = new RecommenderBuilder() {
  93.             @Override
  94.             public Recommender buildRecommender(DataModel model) throws TasteException {
  95.                 UserSimilarity similarity = new LogLikelihoodSimilarity(model);
  96.                 UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
  97.                 return new GenericUserBasedRecommender(model, neighborhood, similarity);
  98.             }
  99.         };
  100.  
  101.         //evaluiraj procjenu za log likelihood buildera
  102.         RecommenderEvaluator recEvaluator2 = new RMSRecommenderEvaluator();
  103.         double score2 = recEvaluator.evaluate(logLikelihoodBuilder, null, model, TRAINING_PERCENTAGE, EVALUATION_PERCENTAGE);
  104.         System.out.println("Log Likelihood score = " + score2);
  105.     }
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top