package com.tzld.piaoquan.recommend.server.service.score4recall.model4recall;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class Model4Recall2List extends AbstractModel {
    private static final Logger LOGGER = LoggerFactory.getLogger(Model4Recall2List.class);
    public List<Pair<Long, Double>> recallList;

    public Model4Recall2List() {
        //配置不同环境的hdfs conf
        this.recallList = new ArrayList<>();
    }


    @Override
    public boolean loadFromStream(InputStreamReader in) throws IOException {
        BufferedReader input = new BufferedReader(in);
        String line = null;
        while ((line = input.readLine()) != null) {
            String[] items = line.split("\t");
            if (items.length < 2) {
                continue;
            }
            for (String item : items) {
                String[] itemArr = item.split(",");
                if (itemArr.length != 2) {
                    continue;
                }
                String id = itemArr[0].trim();
                String score = itemArr[1].trim();
                try {
                    Pair<Long, Double> pair = Pair.of(Long.valueOf(id), Double.valueOf(score));
                    this.recallList.add(pair);
                } catch (Exception e) {
                    LOGGER.error(String.format("something is wrong with parse pair %s %s: ", id, score), e);
                }
            }
        }
        input.close();
        in.close();
        return true;
    }
}
