001    package es.uam.eps.irg.rs.trec.eval.test;
002    
003    import es.uam.eps.irg.rs.trec.eval.EvalUtils;
004    import es.uam.eps.irg.rs.trec.eval.TrecRecommenderEvaluator;
005    import es.uam.eps.irg.rs.trec.eval.TrecRecommenderEvaluatorFactory;
006    import es.uam.eps.irg.rs.trec.eval.file.persistent.RecommenderResultFromFileInMemory;
007    import java.io.File;
008    import java.io.IOException;
009    import java.util.HashMap;
010    import java.util.Map;
011    import org.apache.mahout.cf.taste.common.TasteException;
012    import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
013    import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
014    import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
015    import org.apache.mahout.cf.taste.impl.recommender.ItemUserAverageRecommender;
016    import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
017    import org.apache.mahout.cf.taste.model.DataModel;
018    import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
019    import org.apache.mahout.cf.taste.recommender.Recommender;
020    import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
021    import org.apache.mahout.cf.taste.similarity.UserSimilarity;
022    
023    /**
024     *
025     * Main class with examples of how to invoke the different evaluators
026     *
027     * @author Alejandro
028     */
029    public class Main {
030    
031        private Recommender[] recommenders;
032        private String[] names;
033        private Map<Long, Map<Long, Float>> trainMap;
034        private Map<Long, Map<Long, Float>> testMap;
035    
036        private void evaluateFromMahout() throws IOException, TasteException {
037            recommenders = new Recommender[2];
038            names = new String[2];
039            trainMap = new HashMap<Long, Map<Long, Float>>();
040            testMap = new HashMap<Long, Map<Long, Float>>();
041            initForEvaluateFromMahout(recommenders, names, trainMap, testMap);
042            evaluate();
043        }
044    
045        private void initForEvaluateFromMahout(Recommender[] recs, String[] names,
046                Map<Long, Map<Long, Float>> trainMap, Map<Long, Map<Long, Float>> testMap) throws IOException, TasteException {
047            // open the files
048            DataModel train = new FileDataModel(new File("res/train.txt"));
049            DataModel test = new FileDataModel(new File("res/test.txt"));
050            // choose a recommender
051            ItemUserAverageRecommender iuAvg = new ItemUserAverageRecommender(train);
052            UserSimilarity similarity = new PearsonCorrelationSimilarity(train);
053            UserNeighborhood neighborhood = new NearestNUserNeighborhood(50, similarity, train);
054            UserBasedRecommender ub = new GenericUserBasedRecommender(train, neighborhood, similarity);
055            recs[0] = iuAvg;
056            recs[1] = ub;
057            names[0] = "iuAvg";
058            names[1] = "ub";
059            // generate different evaluation files
060            trainMap.putAll(EvalUtils.fromDatamodelToMap(train));
061            testMap.putAll(EvalUtils.fromDatamodelToMap(test));
062        }
063    
064        private void evaluateFromMyMediaLite(String recFile) throws IOException, TasteException {
065            recommenders = new Recommender[1];
066            names = new String[1];
067            trainMap = new HashMap<Long, Map<Long, Float>>();
068            testMap = new HashMap<Long, Map<Long, Float>>();
069            initForEvaluateFromMyMediaLite(recFile, recommenders, names, trainMap, testMap);
070            evaluate();
071        }
072    
073        private static void initForEvaluateFromMyMediaLite(String recFile,
074                Recommender[] recs, String[] names,
075                Map<Long, Map<Long, Float>> trainMap, Map<Long, Map<Long, Float>> testMap) throws IOException, TasteException {
076            // open the files
077            DataModel train = new FileDataModel(new File("res/train.txt"));
078            DataModel test = new FileDataModel(new File("res/test.txt"));
079            // load the recommender from file
080            Recommender recommender = EvalUtils.getRecommenderFromResult(new RecommenderResultFromFileInMemory(recFile));
081            recs[0] = recommender;
082            names[0] = new File(recFile).getName();
083            // generate different evaluation files
084            trainMap.putAll(EvalUtils.fromDatamodelToMap(train));
085            testMap.putAll(EvalUtils.fromDatamodelToMap(test));
086        }
087    
088        private void evaluateFromGeneratedFiles(String recFile) throws IOException, TasteException {
089            recommenders = new Recommender[1];
090            names = new String[1];
091            trainMap = new HashMap<Long, Map<Long, Float>>();
092            testMap = new HashMap<Long, Map<Long, Float>>();
093            initForEvaluateFromGeneratedFiles(recFile, recommenders, names, trainMap, testMap);
094            evaluate();
095        }
096    
097        private static void initForEvaluateFromGeneratedFiles(String recFile,
098                Recommender[] recs, String[] names,
099                Map<Long, Map<Long, Float>> trainMap, Map<Long, Map<Long, Float>> testMap) throws IOException, TasteException {
100            // open the files
101            DataModel train = new FileDataModel(new File("res/train.txt"));
102            DataModel test = new FileDataModel(new File("res/test.txt"));
103            // load the recommender from file
104            Recommender recommender = EvalUtils.getRecommenderFromResult(new RecommenderResultFromFileInMemory(recFile, 0, 2, 4));
105            recs[0] = recommender;
106            names[0] = new File(recFile).getName();
107            // generate different evaluation files
108            trainMap.putAll(EvalUtils.fromDatamodelToMap(train));
109            testMap.putAll(EvalUtils.fromDatamodelToMap(test));
110        }
111    
112        private void evaluate() {
113            TrecRecommenderEvaluator eval = null;
114            long time = 0;
115    
116            eval = TrecRecommenderEvaluatorFactory.getTrecRecommenderEvaluator(TrecRecommenderEvaluatorFactory.TREC_REC_EVALUATOR_TYPE.USING_ALL_ITEMS);
117            TrecRecommenderEvaluatorFactory.initEvaluator(eval, trainMap, testMap, "res/urel_" + eval + ".txt", null, recommenders, names, "res/");
118            time = System.currentTimeMillis();
119            eval.evaluate();
120            System.out.println(eval + " --> " + (System.currentTimeMillis() - time) + " ms");
121            eval = TrecRecommenderEvaluatorFactory.getTrecRecommenderEvaluator(TrecRecommenderEvaluatorFactory.TREC_REC_EVALUATOR_TYPE.USING_TEST_ITEMS);
122            TrecRecommenderEvaluatorFactory.initEvaluator(eval, trainMap, testMap, "res/urel_" + eval + ".txt", null, recommenders, names, "res/");
123            time = System.currentTimeMillis();
124            eval.evaluate();
125            System.out.println(eval + " --> " + (System.currentTimeMillis() - time) + " ms");
126            eval = TrecRecommenderEvaluatorFactory.getTrecRecommenderEvaluator(TrecRecommenderEvaluatorFactory.TREC_REC_EVALUATOR_TYPE.USING_TRAIN_ITEMS);
127            TrecRecommenderEvaluatorFactory.initEvaluator(eval, trainMap, testMap, "res/urel_" + eval + ".txt", null, recommenders, names, "res/");
128            time = System.currentTimeMillis();
129            eval.evaluate();
130            System.out.println(eval + " --> " + (System.currentTimeMillis() - time) + " ms");
131    
132            eval = TrecRecommenderEvaluatorFactory.getTrecRecommenderEvaluator(TrecRecommenderEvaluatorFactory.TREC_REC_EVALUATOR_TYPE.USING_TEST_RATINGS, 4.0f, 500, null);
133            TrecRecommenderEvaluatorFactory.initEvaluator(eval, trainMap, testMap, "res/urel_" + eval + ".txt", null, recommenders, names, "res/");
134            time = System.currentTimeMillis();
135            eval.evaluate();
136            System.out.println(eval + " --> " + (System.currentTimeMillis() - time) + " ms");
137            eval = TrecRecommenderEvaluatorFactory.getTrecRecommenderEvaluator(TrecRecommenderEvaluatorFactory.TREC_REC_EVALUATOR_TYPE.USING_ONE_AND_N_ITEMS, 4.0f, 500, null);
138            TrecRecommenderEvaluatorFactory.initEvaluator(eval, trainMap, testMap, "res/urel_" + eval + ".txt", null, recommenders, names, "res/");
139            time = System.currentTimeMillis();
140            eval.evaluate();
141            System.out.println(eval + " --> " + (System.currentTimeMillis() - time) + " ms");
142            eval = TrecRecommenderEvaluatorFactory.getTrecRecommenderEvaluator(TrecRecommenderEvaluatorFactory.TREC_REC_EVALUATOR_TYPE.USING_TEST_AND_N_ITEMS, 4.0f, 500, null);
143            TrecRecommenderEvaluatorFactory.initEvaluator(eval, trainMap, testMap, "res/urel_" + eval + ".txt", null, recommenders, names, "res/");
144            time = System.currentTimeMillis();
145            eval.evaluate();
146            System.out.println(eval + " --> " + (System.currentTimeMillis() - time) + " ms");
147        }
148    
149        public static void main(String[] args) throws Exception {
150            new Main().evaluateFromMahout();
151            new Main().evaluateFromMyMediaLite(args[0]);
152            new Main().evaluateFromGeneratedFiles(args[1]);
153        }
154    }