es.uam.eps.ir.ranksys.examples.RerankerExample.java Source code

Java tutorial

Introduction

Here is the source code for es.uam.eps.ir.ranksys.examples.RerankerExample.java

Source

/* 
 * Copyright (C) 2015 Information Retrieval Group at Universidad Autnoma
 * de Madrid, http://ir.ii.uam.es
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package es.uam.eps.ir.ranksys.examples;

import es.uam.eps.ir.ranksys.core.feature.FeatureData;
import es.uam.eps.ir.ranksys.core.feature.SimpleFeatureData;
import es.uam.eps.ir.ranksys.core.preference.PreferenceData;
import es.uam.eps.ir.ranksys.core.preference.SimplePreferenceData;
import es.uam.eps.ir.ranksys.diversity.binom.BinomialModel;
import es.uam.eps.ir.ranksys.diversity.distance.reranking.MMR;
import es.uam.eps.ir.ranksys.diversity.intentaware.*;
import es.uam.eps.ir.ranksys.diversity.intentaware.reranking.AlphaXQuAD;
import es.uam.eps.ir.ranksys.diversity.intentaware.reranking.XQuAD;
import es.uam.eps.ir.ranksys.novdiv.distance.ItemDistanceModel;
import es.uam.eps.ir.ranksys.novdiv.distance.JaccardFeatureItemDistanceModel;
import es.uam.eps.ir.ranksys.novdiv.reranking.Reranker;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.apache.commons.io.FilenameUtils;
import org.jooq.lambda.Unchecked;
import org.ranksys.diversity.prop.reranking.PM;
import org.ranksys.formats.feature.SimpleFeaturesReader;
import static org.ranksys.formats.parsing.Parsers.lp;
import static org.ranksys.formats.parsing.Parsers.sp;
import org.ranksys.formats.preference.SimpleRatingPreferencesReader;
import org.ranksys.formats.rec.RecommendationFormat;
import org.ranksys.formats.rec.SimpleRecommendationFormat;

/**
 * Example main of re-rankers.
 *
 * @author Sal Vargas (saul.vargas@uam.es)
 * @author Pablo Castells (pablo.castells@uam.es)
 */
public class RerankerExample {

    public static void main(String[] args) throws Exception {
        String trainDataPath = args[0];
        String featurePath = args[1];
        String recIn = args[2];

        int cutoff = 100;
        PreferenceData<Long, Long> trainData = SimplePreferenceData
                .load(SimpleRatingPreferencesReader.get().read(trainDataPath, lp, lp));
        FeatureData<Long, String, Double> featureData = SimpleFeatureData
                .load(SimpleFeaturesReader.get().read(featurePath, lp, sp));

        Map<String, Supplier<Reranker<Long, Long>>> rerankersMap = new HashMap<>();

        rerankersMap.put("MMR", () -> {
            double lambda = 0.5;
            ItemDistanceModel<Long> dist = new JaccardFeatureItemDistanceModel<>(featureData);
            return new MMR<>(lambda, cutoff, dist);
        });

        rerankersMap.put("xQuAD", () -> {
            double lambda = 0.5;
            IntentModel<Long, Long, String> intentModel = new FeatureIntentModel<>(trainData, featureData);
            AspectModel<Long, Long, String> aspectModel = new ScoresAspectModel<>(intentModel);
            return new XQuAD<>(aspectModel, lambda, cutoff, true);
        });

        rerankersMap.put("RxQuAD", () -> {
            double alpha = 0.5;
            double lambda = 0.5;
            IntentModel<Long, Long, String> intentModel = new FeatureIntentModel<>(trainData, featureData);
            AspectModel<Long, Long, String> aspectModel = new ScoresRelevanceAspectModel<>(intentModel);
            return new AlphaXQuAD<>(aspectModel, alpha, lambda, cutoff, true);
        });

        rerankersMap.put("PM", () -> {
            double alpha = 0.5;
            double lambda = 0.9;
            BinomialModel<Long, Long, String> binomialModel = new BinomialModel<>(false, Stream.empty(), trainData,
                    featureData, alpha);
            return new PM<>(featureData, binomialModel, lambda, cutoff);
        });

        RecommendationFormat<Long, Long> format = new SimpleRecommendationFormat<>(lp, lp);

        rerankersMap.forEach(Unchecked.biConsumer((name, rerankerSupplier) -> {
            String recOut = Paths.get(Paths.get(recIn).getParent().toString(),
                    String.format("%s-%s", name, FilenameUtils.getName(recIn))).toString();
            System.out.printf("running %s, output to %s\n", name, recOut);
            Reranker<Long, Long> reranker = rerankerSupplier.get();
            try (RecommendationFormat.Writer<Long, Long> writer = format.getWriter(recOut)) {
                format.getReader(recIn).readAll().map(rec -> reranker.rerankRecommendation(rec, cutoff))
                        .forEach(Unchecked.consumer(writer::write));
            }
        }));
    }
}