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

org.ranksys.formats.preference.SimpleRatingPreferencesReader Maven / Gradle / Ivy

Go to download

RankSys module, providing utilities for reading and writing into different formats of preference data, recommendations, etc.

The newest version!
/*
 * Copyright (C) 2016 RankSys http://ranksys.org
 *
 * 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 org.ranksys.formats.preference;

import static es.uam.eps.ir.ranksys.core.util.FastStringSplitter.split;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static java.lang.Double.parseDouble;
import java.util.stream.Stream;
import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple3;
import org.ranksys.formats.parsing.Parser;

/**
 * Reads a file of tab-separated user-item-rating triples, one per line.
 *
 * @author Saúl Vargas ([email protected])
 */
public class SimpleRatingPreferencesReader implements PreferencesReader {

    /**
     * Returns and instance of this class.
     *
     * @param  user type
     * @param  item type
     * @return an instance of SimpleRatingPreferencesReader
     */
    public static  SimpleRatingPreferencesReader get() {
        return new SimpleRatingPreferencesReader();
    }

    private SimpleRatingPreferencesReader() {
    }

    @Override
    public  Stream> read(InputStream in, Parser up, Parser ip) throws IOException {
        return new BufferedReader(new InputStreamReader(in)).lines().map(line -> {
            CharSequence[] tokens = split(line, '\t', 4);
            U user = up.parse(tokens[0]);
            I item = ip.parse(tokens[1]);
            double value = parseDouble(tokens[2].toString());

            return Tuple.tuple(user, item, value);
        });
    }

}