// 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/ICategorySpecificAgreement.java
// public interface ICategorySpecificAgreement
// {
//     /**
//      * Calculates the inter-rater agreement for the given category.
//      * 
//      * @see ICategorySpecificAgreement
//      */
//     /*
//      * TODO @throws NullPointerException if the study is null or the given category is null.
//      * 
//      * @throws ArrayIndexOutOfBoundsException if the study does not contain the given category.
//      * 
//      * @throws ArithmeticException if the study does not contain annotations for the given category.
//      */
//     public double calculateCategoryAgreement(final Object category);
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IChanceCorrectedDisagreement.java
// public interface IChanceCorrectedDisagreement
//     extends IAgreementMeasure
// {
//     /**
//      * Returns the observed disagreement of an annotation study. The observed disagreement is
//      * basically the proportion of annotation units that the raters disagree on divided by the
//      * number of units in the given study.
//      */
//     public double calculateObservedDisagreement();
// 
//     /**
//      * Returns the expected disagreement of an annotation study. The expected disagreement is the
//      * proportion of disagreement that would be expected by chance alone. The expected disagreement
//      * should be equal to the observed disagreement if each rater makes a random decision for each
//      * unit.
//      */
//     public double calculateExpectedDisagreement();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/InsufficientDataException.java
// public class InsufficientDataException
//     extends RuntimeException
// {
//     private static final long serialVersionUID = -3081535421752493402L;
// 
//     public InsufficientDataException()
//     {
//         super();
//     }
// 
//     public InsufficientDataException(final String message, final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     public InsufficientDataException(final String message)
//     {
//         super(message);
//     }
// 
//     public InsufficientDataException(final Throwable cause)
//     {
//         super(cause);
//     }
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/distance/IDistanceFunction.java
// public interface IDistanceFunction
// {
// 
//     /**
//      * Returns a distance value for the given pair of categories used within the given annotation
//      * study. Normally, the measure should return a distance of 0 if, and only if, the two
//      * categories are equal and a positive number otherwise.
//      */
//     public double measureDistance(final IAnnotationStudy study, final Object category1,
//             final Object category2);
// 
// }


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.dkpro.statistics.agreement.IAnnotationUnit;
import org.dkpro.statistics.agreement.ICategorySpecificAgreement;
import org.dkpro.statistics.agreement.IChanceCorrectedDisagreement;
import org.dkpro.statistics.agreement.InsufficientDataException;
import org.dkpro.statistics.agreement.distance.IDistanceFunction;
            n += n_c;
        }

        /*
         * double D_E = 0.0; for (Entry<Object, Double> cat1 : marginals.entrySet()) for
         * (Entry<Object, Double> cat2 : marginals.entrySet()) D_E += cat1.getValue() *
         * cat2.getValue() distanceFunction.measureDistance(study, cat1.getKey(), cat2.getKey());
         * D_E /= n * (n - 1.0);
         */
        double D_E = calculateExpectedDisagreement();
        if (D_E == 0.0) {
            return 1.0;
        }
        else {
            return 1.0 - (D_O / D_E);
        }
    }

    @Override
    public double calculateCategoryAgreement(final Object category)
    {
        ensureDistanceFunction();

        final Object NULL_CATEGORY = new Object();
        double observedDisagreement = 0.0;
        int nKeepCategorySum = 0;
        int nNullCategorySum = 0;
        for (ICodingAnnotationItem item : study.getItems()) {
            int nKeepCategory = 0;
            int nNullCategory = 0;
// 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/ICategorySpecificAgreement.java
// public interface ICategorySpecificAgreement
// {
//     /**
//      * Calculates the inter-rater agreement for the given category.
//      * 
//      * @see ICategorySpecificAgreement
//      */
//     /*
//      * TODO @throws NullPointerException if the study is null or the given category is null.
//      * 
//      * @throws ArrayIndexOutOfBoundsException if the study does not contain the given category.
//      * 
//      * @throws ArithmeticException if the study does not contain annotations for the given category.
//      */
//     public double calculateCategoryAgreement(final Object category);
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/IChanceCorrectedDisagreement.java
// public interface IChanceCorrectedDisagreement
//     extends IAgreementMeasure
// {
//     /**
//      * Returns the observed disagreement of an annotation study. The observed disagreement is
//      * basically the proportion of annotation units that the raters disagree on divided by the
//      * number of units in the given study.
//      */
//     public double calculateObservedDisagreement();
// 
//     /**
//      * Returns the expected disagreement of an annotation study. The expected disagreement is the
//      * proportion of disagreement that would be expected by chance alone. The expected disagreement
//      * should be equal to the observed disagreement if each rater makes a random decision for each
//      * unit.
//      */
//     public double calculateExpectedDisagreement();
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/InsufficientDataException.java
// public class InsufficientDataException
//     extends RuntimeException
// {
//     private static final long serialVersionUID = -3081535421752493402L;
// 
//     public InsufficientDataException()
//     {
//         super();
//     }
// 
//     public InsufficientDataException(final String message, final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     public InsufficientDataException(final String message)
//     {
//         super(message);
//     }
// 
//     public InsufficientDataException(final Throwable cause)
//     {
//         super(cause);
//     }
// }
// 
// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/distance/IDistanceFunction.java
// public interface IDistanceFunction
// {
// 
//     /**
//      * Returns a distance value for the given pair of categories used within the given annotation
//      * study. Normally, the measure should return a distance of 0 if, and only if, the two
//      * categories are equal and a positive number otherwise.
//      */
//     public double measureDistance(final IAnnotationStudy study, final Object category1,
//             final Object category2);
// 
// }


// Path: dkpro-statistics-agreement/src/main/java/org/dkpro/statistics/agreement/coding/KrippendorffAlphaAgreement.java
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.dkpro.statistics.agreement.IAnnotationUnit;
import org.dkpro.statistics.agreement.ICategorySpecificAgreement;
import org.dkpro.statistics.agreement.IChanceCorrectedDisagreement;
import org.dkpro.statistics.agreement.InsufficientDataException;
import org.dkpro.statistics.agreement.distance.IDistanceFunction;

            n += n_c;
        }

        /*
         * double D_E = 0.0; for (Entry<Object, Double> cat1 : marginals.entrySet()) for
         * (Entry<Object, Double> cat2 : marginals.entrySet()) D_E += cat1.getValue() *
         * cat2.getValue() distanceFunction.measureDistance(study, cat1.getKey(), cat2.getKey());
         * D_E /= n * (n - 1.0);
         */
        double D_E = calculateExpectedDisagreement();
        if (D_E == 0.0) {
            return 1.0;
        }
        else {
            return 1.0 - (D_O / D_E);
        }
    }

    @Override
    public double calculateCategoryAgreement(final Object category)
    {
        ensureDistanceFunction();

        final Object NULL_CATEGORY = new Object();
        double observedDisagreement = 0.0;
        int nKeepCategorySum = 0;
        int nNullCategorySum = 0;
        for (ICodingAnnotationItem item : study.getItems()) {
            int nKeepCategory = 0;
            int nNullCategory = 0;
            for (IAnnotationUnit annotation : item.getUnits()) {