// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IAnnotationUnit.java
// public interface IAnnotationUnit
//     extends Serializable
// {
//     /**
//      * Returns the index of the rater who coded this unit (in case of a coding study) or defined the
//      * boundaries of this unit (in case of a unitizing study). The first rater has index 0.
//      */
//     public int getRaterIdx();
// 
//     /**
//      * Returns the category assigned to this unit by one of the raters. The category might be null
//      * if, and only if, the unit represents a missing value (in case of a coding study) or a gap (in
//      * case of a unitizing study).
//      */
//     public Object getCategory();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/coding/ICodingAnnotationItem.java
// public interface ICodingAnnotationItem
//     extends IAnnotationItem
// {
//     /**
//      * Returns the annotation unit of the rater with the specified index. That is, the object
//      * holding the category assigned to the item by the specified rater.
//      */
//     public IAnnotationUnit getUnit(int raterIdx);
// 
//     /**
//      * Returns all coding units for this annotation item (i.e., the categories assigned by the
//      * individual raters).
//      */
//     public Iterable<IAnnotationUnit> getUnits();
// 
//     /**
//      * Returns the number of raters who annotated this item with a non-null category.
//      */
//     public int getRaterCount();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/coding/ICodingAnnotationStudy.java
// public interface ICodingAnnotationStudy
//     extends IAnnotationStudy
// {
// 
//     // -- Categories --
// 
//     /**
//      * Returns true if, and only if, the annotation study contains at least one item with a missing
//      * value (i.e., an annotation item containing an annotation unit with category null).
//      */
//     public boolean hasMissingValues();
// 
//     // -- Items --
// 
//     /**
//      * Returns the annotation item with the given index. The first item has index 0.
//      */
//     public ICodingAnnotationItem getItem(int index);
// 
//     /** Allows iterating all annotation items of this study. */
//     public Iterable<ICodingAnnotationItem> getItems();
// 
//     /** Returns the number of annotation items defined by the study. */
//     public int getItemCount();
// 
//     // -- Units --
// 
//     /**
//      * Returns the number of annotation units defined by the study. That is, the number of
//      * annotations coded by the raters. If there are no missing values, the unit count equals the
//      * item count multiplied with the number of raters.
//      */
//     @Override
//     public int getUnitCount();
// 
// }


import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dkpro.statistics.agreement.IAnnotationUnit;
import org.dkpro.statistics.agreement.coding.ICodingAnnotationItem;
import org.dkpro.statistics.agreement.coding.ICodingAnnotationStudy;
/*
 * Copyright 2014
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universität Darmstadt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dkpro.statistics.agreement.visualization;



/**
 * Plain-text visualization for a reliability matrix as defined by 
 * Krippendorff (1980: p. 136). That is, a table showing one annotation
 * item per column and the categories assigned to them by a certain
 * rater. In addition to that, the reliability matrix sums the number 
 * of units with identical categories. For the example by Krippendorff 
 * (1980: p. 139), the implementation displays:
 * <pre>
 *    1  2  3  4  5  6  7  8  9   Σ
 * 1  1  1  2  4  1  2  1  3  2 
 * 2  1  2  2  4  4  2  2  3  2 
 * 3  1  2  2  4  4  2  3  3  2 
 *                              
 * 1  3  1        1     1         6
 * 2     2  3        3  1     3  12
 * 3                    1  3      4
 * 4           3  2               5
 * </pre><br>
 * References:<ul>
 * <li>Krippendorff, K.: Content Analysis: An Introduction to Its Methodology.
 *   Beverly Hills, CA: Sage Publications, 1980.</li></ul>
 * @see ICodingAnnotationStudy
 * @author Christian M. Meyer
 */
public class ReliabilityMatrixPrinter
{

    /** Print the reliability matrix for the given coding study. */
    public void print(final PrintStream out, final ICodingAnnotationStudy study)
    {
        // TODO: measure length of cats. maybe cut them.
        Map<Object, Integer> categories = new LinkedHashMap<Object, Integer>();
        for (Object cat : study.getCategories()) {
            categories.put(cat, categories.size());
        }

        final String DIVIDER = "\t";

        for (int i = 0; i < study.getItemCount(); i++) {
            out.print(DIVIDER + (i + 1));
        }
        out.print(DIVIDER + "Σ");
        out.println();

        for (int r = 0; r < study.getRaterCount(); r++) {
            out.print(r + 1);
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IAnnotationUnit.java
// public interface IAnnotationUnit
//     extends Serializable
// {
//     /**
//      * Returns the index of the rater who coded this unit (in case of a coding study) or defined the
//      * boundaries of this unit (in case of a unitizing study). The first rater has index 0.
//      */
//     public int getRaterIdx();
// 
//     /**
//      * Returns the category assigned to this unit by one of the raters. The category might be null
//      * if, and only if, the unit represents a missing value (in case of a coding study) or a gap (in
//      * case of a unitizing study).
//      */
//     public Object getCategory();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/coding/ICodingAnnotationItem.java
// public interface ICodingAnnotationItem
//     extends IAnnotationItem
// {
//     /**
//      * Returns the annotation unit of the rater with the specified index. That is, the object
//      * holding the category assigned to the item by the specified rater.
//      */
//     public IAnnotationUnit getUnit(int raterIdx);
// 
//     /**
//      * Returns all coding units for this annotation item (i.e., the categories assigned by the
//      * individual raters).
//      */
//     public Iterable<IAnnotationUnit> getUnits();
// 
//     /**
//      * Returns the number of raters who annotated this item with a non-null category.
//      */
//     public int getRaterCount();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/coding/ICodingAnnotationStudy.java
// public interface ICodingAnnotationStudy
//     extends IAnnotationStudy
// {
// 
//     // -- Categories --
// 
//     /**
//      * Returns true if, and only if, the annotation study contains at least one item with a missing
//      * value (i.e., an annotation item containing an annotation unit with category null).
//      */
//     public boolean hasMissingValues();
// 
//     // -- Items --
// 
//     /**
//      * Returns the annotation item with the given index. The first item has index 0.
//      */
//     public ICodingAnnotationItem getItem(int index);
// 
//     /** Allows iterating all annotation items of this study. */
//     public Iterable<ICodingAnnotationItem> getItems();
// 
//     /** Returns the number of annotation items defined by the study. */
//     public int getItemCount();
// 
//     // -- Units --
// 
//     /**
//      * Returns the number of annotation units defined by the study. That is, the number of
//      * annotations coded by the raters. If there are no missing values, the unit count equals the
//      * item count multiplied with the number of raters.
//      */
//     @Override
//     public int getUnitCount();
// 
// }


// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/visualization/ReliabilityMatrixPrinter.java
import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dkpro.statistics.agreement.IAnnotationUnit;
import org.dkpro.statistics.agreement.coding.ICodingAnnotationItem;
import org.dkpro.statistics.agreement.coding.ICodingAnnotationStudy;

/*
 * Copyright 2014
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universität Darmstadt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dkpro.statistics.agreement.visualization;



/**
 * Plain-text visualization for a reliability matrix as defined by 
 * Krippendorff (1980: p. 136). That is, a table showing one annotation
 * item per column and the categories assigned to them by a certain
 * rater. In addition to that, the reliability matrix sums the number 
 * of units with identical categories. For the example by Krippendorff 
 * (1980: p. 139), the implementation displays:
 * <pre>
 *    1  2  3  4  5  6  7  8  9   Σ
 * 1  1  1  2  4  1  2  1  3  2 
 * 2  1  2  2  4  4  2  2  3  2 
 * 3  1  2  2  4  4  2  3  3  2 
 *                              
 * 1  3  1        1     1         6
 * 2     2  3        3  1     3  12
 * 3                    1  3      4
 * 4           3  2               5
 * </pre><br>
 * References:<ul>
 * <li>Krippendorff, K.: Content Analysis: An Introduction to Its Methodology.
 *   Beverly Hills, CA: Sage Publications, 1980.</li></ul>
 * @see ICodingAnnotationStudy
 * @author Christian M. Meyer
 */
public class ReliabilityMatrixPrinter
{

    /** Print the reliability matrix for the given coding study. */
    public void print(final PrintStream out, final ICodingAnnotationStudy study)
    {
        // TODO: measure length of cats. maybe cut them.
        Map<Object, Integer> categories = new LinkedHashMap<Object, Integer>();
        for (Object cat : study.getCategories()) {
            categories.put(cat, categories.size());
        }

        final String DIVIDER = "\t";

        for (int i = 0; i < study.getItemCount(); i++) {
            out.print(DIVIDER + (i + 1));
        }
        out.print(DIVIDER + "Σ");
        out.println();

        for (int r = 0; r < study.getRaterCount(); r++) {
            out.print(r + 1);
            for (ICodingAnnotationItem item : study.getItems()) {