package de.unidue.ltl.evaluation.measure.agreement;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

public class ConfusionMatrix
{

    private List<Integer> ratingsA;
    private List<Integer> ratingsB;
    
    private Integer[] categories;
    
    private Map<Integer, Map<Integer, Integer>> confusionMatrix;
    
    public ConfusionMatrix(Integer[] ratingsA, Integer[] ratingsB, Integer... categories) {
        this(Arrays.asList(ratingsA), Arrays.asList(ratingsB), categories);
    }

    public ConfusionMatrix(List<Integer> ratingsA, List<Integer> ratingsB, Integer... categories)
    {
        if (ratingsA.size() != ratingsB.size()) {
            throw new IllegalArgumentException("Rating vectors need to be of equal size.");
        }
        
        this.ratingsA = ratingsA;
        this.ratingsB = ratingsB;
        
        this.categories = categories;
        
        confusionMatrix = new TreeMap<Integer, Map<Integer, Integer>>();
        
        for (Integer category : categories) {
            confusionMatrix.put(category, new TreeMap<Integer,Integer>());
            for (Integer innerCategory : categories) {
                confusionMatrix.get(category).put(innerCategory, 0);
            }
        }

        for (int i=0; i<ratingsA.size(); i++) {
            int ratingA = ratingsA.get(i);
            int ratingB = ratingsB.get(i);
            
            if (!confusionMatrix.get(ratingA).containsKey(ratingB)) {
                confusionMatrix.get(ratingA).put(ratingB, 1);
            }
            else {
                confusionMatrix.get(ratingA).put(ratingB, confusionMatrix.get(ratingA).get(ratingB) + 1);
            }
        }
    }

    public String getConfusionMatrixSerialization() {

        List<Integer> values = new ArrayList<Integer>();
        for (int outerCategory : categories) {
            for (int innerCategory : categories) {
                values.add(confusionMatrix.get(outerCategory).get(innerCategory));
            }
        }
        
        return StringUtils.join(values, ",");
    }

    public void printConfusionMatrix() {
        System.out.print("\t");
        for (int category : categories) {
            System.out.print(category + "\t");
        }
        System.out.println();

        for (int outerCategory : categories) {
            System.out.print(outerCategory + "\t");
            for (int innerCategory : categories) {
                System.out.print(confusionMatrix.get(outerCategory).get(innerCategory) + "\t");
            }
            System.out.println();
        }
        System.out.println();
    }
    
    public Integer getElement(int categoryA, int categoryB) {
        return confusionMatrix.get(categoryA).get(categoryB);
    }
    
    public List<Integer> getRatingsA()
    {
        return ratingsA;
    }

    public List<Integer> getRatingsB()
    {
        return ratingsB;
    }
}
@@ -44,16 +44,811 @@
 reement;
+%0A/*******************************************************************************%0A * Copyright 2017%0A * Language Technology Lab%0A * University of Duisburg-Essen%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A ******************************************************************************/
 %0A%0Aimport

Add copyright