package de.ustu.creta.segmentation.agreement.impl;

import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

import de.nilsreiter.pipeline.segmentation.type.SegmentBoundary;
import de.nilsreiter.pipeline.segmentation.type.SegmentationUnit;
import de.ustu.creta.segmentation.agreement.CohensKappa;
import de.ustu.creta.segmentation.evaluation.Metric;

public class CohensKappa_impl implements CohensKappa {

	Metric observedAgreementMetric;

	@Override
	public double agr(JCas... jcas) {
		double obs = getObservedAgreement(jcas);
		double chc = getChanceAgreement(jcas);
		return (obs - chc) / (1 - chc);
	}

	@Override
	public double getObservedAgreement(JCas... jcas) {
		int mass = JCasUtil.select(jcas[0], SegmentationUnit.class).size();
		int z = 0;
		int n = 0;
		for (int i = 0; i < jcas.length; i++) {
			for (int j = i + 1; j < jcas.length; j++) {
				double score =
						observedAgreementMetric.scores(jcas[i], jcas[j]).get(
								observedAgreementMetric.getClass()
										.getSimpleName());
				z += mass * score;
				n += mass;
			}
		}

		return z / (double) n;
	}

	@Override
	public double getChanceAgreement(JCas... jcas) {
		int mass = JCasUtil.select(jcas[0], SegmentationUnit.class).size();

		double cha0 =
				JCasUtil.select(jcas[0], SegmentBoundary.class).size()
						/ (double) (mass - 1);
		double cha1 =
				JCasUtil.select(jcas[1], SegmentBoundary.class).size()
						/ (double) (mass - 1);
		return cha0 * cha1;
	}

	@Override
	public void setObservedAgreementMetric(Metric metric) {
		observedAgreementMetric = metric;

	}

	@Override
	public Metric getObservedAgreementMetric() {
		return observedAgreementMetric;
	}
}

@@ -757,19 +757,22 @@
 ze();%0A%09%09
-int
+double
  z = 0;%0A
@@ -773,19 +773,22 @@
  = 0;%0A%09%09
-int
+double
  n = 0;%0A
@@ -998,18 +998,16 @@
 Class()%0A
-%09%09
 %09%09%09%09%09%09%09%09
@@ -1025,16 +1025,16 @@
 ame());%0A
+
 %09%09%09%09z +=
@@ -1085,25 +1085,16 @@
 turn z /
- (double)
  n;%0A%09%7D%0A%0A
@@ -1292,34 +1292,32 @@
 y.class).size()%0A
-%09%09
 %09%09%09%09/ (double) (
@@ -1327,16 +1327,16 @@
 s - 1);%0A
+
 %09%09double
@@ -1402,18 +1402,16 @@
 .size()%0A
-%09%09
 %09%09%09%09/ (d

int->double