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

ml.classifiers.utils.ClassificationVoter Maven / Gradle / Ivy

package ml.classifiers.utils;

import utils.Pair;
import utils.PairBuilder;

import java.util.*;

public class ClassificationVoter implements IVoter> > {


    public ClassificationVoter(){
        this.values = new ArrayList>();
    }

    @Override
    public void addItem(Integer item, Double criterionType){
        this.values.add(PairBuilder.makePair(item, criterionType));
    }

    /**
     * Allow any Object cast to double to be added
     * @param item
     * @param criterionType
     */
    public void addItem(Integer item, Object criterionType){
        this.addItem(item, (Double) criterionType);
    }

    /**
     * Add a list of items T, U should be castable to Integer and Double respectively
     */
    public  void addItems(List> items){

        for (int i = 0; i < items.size(); i++) {
            this.values.add((Pair)items.get(i));
        }
    }

    @Override
    public List> getResult(int max){

        //get the values
        Object[] valuesArr = (Object[]) values.toArray();

        Arrays.sort(valuesArr, new Comparator() {
            @Override
            public int compare(Object t, Object t1) {

                if(((Pair)t).second < ((Pair)t1).second ){
                    return -1;
                }
                else if(((Pair)t).second > ((Pair)t1).second){
                    return 1;
                }

                return 0;
            }
        });

        List> subList = new ArrayList>();

        for(int i=0; i) valuesArr[i]);
        }


        return subList;
    }


    public void clear(){

        this.values = new ArrayList>();
    }

    private List> values;
}