All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.uam.eps.ir.ranksys.metrics.rel.BackgroundBinaryRelevanceModel Maven / Gradle / Ivy

Go to download

RankSys module, providing interfaces and common components for defining metrics.

The newest version!
/* 
 * Copyright (C) 2015 Information Retrieval Group at Universidad Autónoma
 * 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.metrics.rel;

import es.uam.eps.ir.ranksys.core.preference.PreferenceData;
import es.uam.eps.ir.ranksys.metrics.rel.RelevanceModel.UserRelevanceModel;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;

/**
 * Background discount model: assumes the relevance of unseen items in a test
 * subset with a pre-fixed gain value.
 *
 * @author Saúl Vargas ([email protected])
 * 
 * @param  type of the users
 * @param  type of the items
 */
public class BackgroundBinaryRelevanceModel extends RelevanceModel {

    private final PreferenceData testData;
    private final double threshold;
    private final double background;

    /**
     * Constructor.
     *
     * @param caching are the user relevance models being cached?
     * @param testData test subset of preferences
     * @param threshold relevance threshold
     * @param background gain of unseen items in the test subset
     */
    public BackgroundBinaryRelevanceModel(boolean caching, PreferenceData testData, double threshold, double background) {
        super(caching, testData.getUsersWithPreferences());
        this.testData = testData;
        this.threshold = threshold;
        this.background = background;
    }

    @Override
    protected UserRelevanceModel get(U user) {
        return new UserSmo4RelevanceModel(user);
    }

    private class UserSmo4RelevanceModel implements UserRelevanceModel {

        private final Object2DoubleMap gainMap;

        public UserSmo4RelevanceModel(U user) {
            this.gainMap = new Object2DoubleOpenHashMap<>();
            this.gainMap.defaultReturnValue(background);

            testData.getUserPreferences(user).forEach(iv -> {
                if (iv.v2 >= threshold) {
                    gainMap.put(iv.v1, 1.0);
                } else {
                    gainMap.put(iv.v1, 0.0);
                }
            });
        }

        @Override
        public boolean isRelevant(I item) {
            return gain(item) > 0.0;
        }

        @Override
        public double gain(I item) {
            return gainMap.getDouble(item);
        }

    }
}