<commit_before>package org.insightcentre.richcontext;

import net.recommenders.rival.core.DataModel;
import net.recommenders.rival.core.DataModelFactory;
import net.recommenders.rival.core.DataModelIF;
import net.recommenders.rival.core.DataModelUtils;
import net.recommenders.rival.evaluation.metric.error.MAE;
import net.recommenders.rival.evaluation.metric.error.RMSE;
import net.recommenders.rival.evaluation.metric.ranking.NDCG;
import net.recommenders.rival.evaluation.metric.ranking.Precision;
import net.recommenders.rival.evaluation.metric.ranking.Recall;
import net.recommenders.rival.evaluation.strategy.EvaluationStrategy;
import net.recommenders.rival.evaluation.strategy.RelPlusN;
import net.recommenders.rival.evaluation.strategy.TestItems;
import net.recommenders.rival.evaluation.strategy.UserTest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * Created by fpena on 05/09/2017.
 */
public class RichContextResultsProcessor {


    private int numFolds;
    private int at;
    private int additionalItems;
    private int numTopics;
    private double relevanceThreshold;
    private long seed;
    private Strategy strategy;
    private ContextFormat contextFormat;
    private Dataset dataset;
    private String outputFile;
    private boolean coldStart;


    private String ratingsFolderPath;

    private final String[] headers;

    private static final String RATING = "rating";
    private static final String RANKING = "ranking";

    public enum EvaluationSet {
        TRAIN_USERS,
        TEST_USERS,
        TEST_ONLY_USERS,
        TRAIN_ONLY_USERS,
    }

    private EvaluationSet evaluationSet;

    public enum Strategy {
        ALL_ITEMS(RATING),
        REL_PLUS_N(RANKING),
        TEST_ITEMS(RATING),
        TRAIN_ITEMS(RATING),
        USER_TEST(RATING);

        private final String predictionType;

        Strategy(String predictionType) {
            this.predictionType = predictionType;
        }

        public String getPredictionType() {
            return predictionType;
        }
    }

    public enum ContextFormat {
        NO_CONTEXT,
        CONTEXT_TOPIC_WEIGHTS,
        TOP_WORDS,
        PREDEFINED_CONTEXT,
        TOPIC_PREDEFINED_CONTEXT
    }

    public enum Dataset {
        YELP_HOTEL,
        YELP_RESTAURANT,
        FOURCITY_HOTEL,
    }

    public enum ProcessingTask {
        PREPARE_LIBFM,
        PROCESS_LIBFM_RESULTS,
        EVALUATE_LIBFM_RESULTS
    }


    public RichContextResultsProcessor(
            String cacheFolder, String outputFolder, String propertiesFile,
            EvaluationSet evaluationSet, Integer paramNumTopics,
            String itemType)
            throws IOException {


        Properties properties = Properties.loadProperties(propertiesFile);
        numFolds = properties.getCrossValidationNumFolds();
        at = properties.getTopN();
        relevanceThreshold = properties.getRelevanceThreshold();
        seed = properties.getSeed();
        strategy = Strategy.valueOf(
                (properties.getStrategy().toUpperCase(Locale.ENGLISH)));
        additionalItems = properties.getTopnNumItems();
        contextFormat = RichContextResultsProcessor.ContextFormat.valueOf(
                properties.getContextFormat().toUpperCase(Locale.ENGLISH));
        dataset = (itemType == null) ?
                RichContextResultsProcessor.Dataset.valueOf(
                        properties.getDataset().toUpperCase(Locale.ENGLISH)) :
                RichContextResultsProcessor.Dataset.valueOf(
                        itemType.toUpperCase(Locale.ENGLISH));
        numTopics = (paramNumTopics == null) ?
                properties.getNumTopics() :
                paramNumTopics;
        coldStart = properties.getEvaluateColdStart();
        outputFile = outputFolder +
                "rival_" + dataset.toString() + "_results_folds_4.csv";

        String jsonRatingsFile = cacheFolder + dataset.toString().toLowerCase() +
                "_recsys_formatted_context_records_ensemble_" +
                "numtopics-" + numTopics + "_iterations-100_passes-10_targetreview-specific_" +
                "normalized_contextformat-" + contextFormat.toString().toLowerCase()
                + "_lang-en_bow-NN_document_level-review_targettype-context_" +
                "min_item_reviews-10.json";
        this.evaluationSet = evaluationSet;

        this.ratingsFolderPath = Utils.getRatingsFolderPath(jsonRatingsFile);

        headers = new String[] {
                "Algorithm",
                "Num_Topics",
                "Strategy",
                "Context_Format",
                "Cold-start",
                "NDCG@" + at,
                "Precision@" + at,
                "Recall@" + at,
                "RMSE",
                "MAE",
                "fold_0_ndcg",
                "fold_1_ndcg",
                "fold_2_ndcg",
                "fold_3_ndcg",
                "fold_4_ndcg",
                "fold_0_precision",
                "fold_1_precision",
                "fold_2_precision",
                "fold_3_precision",<commit_msg>Added the dataset as part of the output file for the RichContextResultsProcessor class<commit_after>package org.insightcentre.richcontext;

import net.recommenders.rival.core.DataModel;
import net.recommenders.rival.core.DataModelFactory;
import net.recommenders.rival.core.DataModelIF;
import net.recommenders.rival.core.DataModelUtils;
import net.recommenders.rival.evaluation.metric.error.MAE;
import net.recommenders.rival.evaluation.metric.error.RMSE;
import net.recommenders.rival.evaluation.metric.ranking.NDCG;
import net.recommenders.rival.evaluation.metric.ranking.Precision;
import net.recommenders.rival.evaluation.metric.ranking.Recall;
import net.recommenders.rival.evaluation.strategy.EvaluationStrategy;
import net.recommenders.rival.evaluation.strategy.RelPlusN;
import net.recommenders.rival.evaluation.strategy.TestItems;
import net.recommenders.rival.evaluation.strategy.UserTest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * Created by fpena on 05/09/2017.
 */
public class RichContextResultsProcessor {


    private int numFolds;
    private int at;
    private int additionalItems;
    private int numTopics;
    private double relevanceThreshold;
    private long seed;
    private Strategy strategy;
    private ContextFormat contextFormat;
    private Dataset dataset;
    private String outputFile;
    private boolean coldStart;


    private String ratingsFolderPath;

    private final String[] headers;

    private static final String RATING = "rating";
    private static final String RANKING = "ranking";

    public enum EvaluationSet {
        TRAIN_USERS,
        TEST_USERS,
        TEST_ONLY_USERS,
        TRAIN_ONLY_USERS,
    }

    private EvaluationSet evaluationSet;

    public enum Strategy {
        ALL_ITEMS(RATING),
        REL_PLUS_N(RANKING),
        TEST_ITEMS(RATING),
        TRAIN_ITEMS(RATING),
        USER_TEST(RATING);

        private final String predictionType;

        Strategy(String predictionType) {
            this.predictionType = predictionType;
        }

        public String getPredictionType() {
            return predictionType;
        }
    }

    public enum ContextFormat {
        NO_CONTEXT,
        CONTEXT_TOPIC_WEIGHTS,
        TOP_WORDS,
        PREDEFINED_CONTEXT,
        TOPIC_PREDEFINED_CONTEXT
    }

    public enum Dataset {
        YELP_HOTEL,
        YELP_RESTAURANT,
        FOURCITY_HOTEL,
    }

    public enum ProcessingTask {
        PREPARE_LIBFM,
        PROCESS_LIBFM_RESULTS,
        EVALUATE_LIBFM_RESULTS
    }


    public RichContextResultsProcessor(
            String cacheFolder, String outputFolder, String propertiesFile,
            EvaluationSet evaluationSet, Integer paramNumTopics,
            String itemType)
            throws IOException {


        Properties properties = Properties.loadProperties(propertiesFile);
        numFolds = properties.getCrossValidationNumFolds();
        at = properties.getTopN();
        relevanceThreshold = properties.getRelevanceThreshold();
        seed = properties.getSeed();
        strategy = Strategy.valueOf(
                (properties.getStrategy().toUpperCase(Locale.ENGLISH)));
        additionalItems = properties.getTopnNumItems();
        contextFormat = RichContextResultsProcessor.ContextFormat.valueOf(
                properties.getContextFormat().toUpperCase(Locale.ENGLISH));
        dataset = (itemType == null) ?
                RichContextResultsProcessor.Dataset.valueOf(
                        properties.getDataset().toUpperCase(Locale.ENGLISH)) :
                RichContextResultsProcessor.Dataset.valueOf(
                        itemType.toUpperCase(Locale.ENGLISH));
        numTopics = (paramNumTopics == null) ?
                properties.getNumTopics() :
                paramNumTopics;
        coldStart = properties.getEvaluateColdStart();
        outputFile = outputFolder +
                "rival_" + dataset.toString() + "_results_folds_4.csv";

        String jsonRatingsFile = cacheFolder + dataset.toString().toLowerCase() +
                "_recsys_formatted_context_records_ensemble_" +
                "numtopics-" + numTopics + "_iterations-100_passes-10_targetreview-specific_" +
                "normalized_contextformat-" + contextFormat.toString().toLowerCase()
                + "_lang-en_bow-NN_document_level-review_targettype-context_" +
                "min_item_reviews-10.json";
        this.evaluationSet = evaluationSet;

        this.ratingsFolderPath = Utils.getRatingsFolderPath(jsonRatingsFile);

        headers = new String[] {
                "Algorithm",
                "Dataset",
                "Num_Topics",
                "Strategy",
                "Context_Format",
                "Cold-start",
                "NDCG@" + at,
                "Precision@" + at,
                "Recall@" + at,
                "RMSE",
                "MAE",
                "fold_0_ndcg",
                "fold_1_ndcg",
                "fold_2_ndcg",
                "fold_3_ndcg",
                "fold_4_ndcg",
                "fold_0_precision",
                "fold_1_precision",
                "fold_2_precision",
                "fold_3_precision",
{'fraction_non_alphanumeric': 0.0833019578313253, 'fraction_numerical': 0.00470632530120482, 'mean_word_length': 2.440738341968912, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3026483', 'n_tokens_mistral': 3130, 'n_tokens_neox': 2879, 'n_words': 516}