eu.crydee.alignment.aligner.ae.EvaluatorC.java Source code

Java tutorial

Introduction

Here is the source code for eu.crydee.alignment.aligner.ae.EvaluatorC.java

Source

/*
 * Copyright 2014 Hugo m09? Mougard.
 *
 * 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 eu.crydee.alignment.aligner.ae;

import com.google.common.collect.Sets;
import eu.crydee.alignment.aligner.resources.ConfusionMatrix;
import eu.crydee.alignment.aligner.ts.Sentence;
import java.util.HashSet;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CASException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.descriptor.ExternalResource;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

/**
 *
 * @author Hugo m09? Mougard
 */
public class EvaluatorC extends JCasAnnotator_ImplBase {

    private static final Logger logger = LogManager.getLogger(EvaluatorC.class);

    public static final String RES_CONFUSION_MATRIX = "R1";
    @ExternalResource(key = RES_CONFUSION_MATRIX)
    private ConfusionMatrix cm;

    public static final String PARAM_VIEW_ELEMENTARY = "P1";
    @ConfigurationParameter(name = PARAM_VIEW_ELEMENTARY, mandatory = true)
    private String nameEle;

    public static final String PARAM_VIEW_REGULAR = "P2";
    @ConfigurationParameter(name = PARAM_VIEW_REGULAR, mandatory = true)
    private String nameReg;

    @Override
    public void process(JCas jcas) throws AnalysisEngineProcessException {
        JCas jcasEle;
        try {
            jcasEle = jcas.getView(nameEle);
        } catch (CASException ex) {
            throw new AnalysisEngineProcessException(ex);
        }
        int tp = 0, fp = 0, fn = 0;
        for (Sentence s : JCasUtil.select(jcasEle, Sentence.class)) {
            Set<Sentence> gold = new HashSet<>(), found = new HashSet<>();
            if (s.getGoldSimilarities() == null) {
                continue;
            }
            for (int i = 0, l = s.getGoldSimilarities().size(); i < l; ++i) {
                gold.add(s.getGoldSimilarities(i));
            }
            for (int i = 0, l = s.getFoundSimilarities().size(); i < l; ++i) {
                found.add(s.getFoundSimilarities(i));
            }
            int goldSize = gold.size(), foundSize = found.size(),
                    commonSize = Sets.intersection(gold, found).size();
            cm.addTruePositives(commonSize);
            cm.addFalsePositive(foundSize - commonSize);
            cm.addFalseNegatives(goldSize - commonSize);
        }
    }

    @Override
    public void collectionProcessComplete() throws AnalysisEngineProcessException {
        int tp = cm.getTruePositives(), fp = cm.getFalsePositives(), fn = cm.getFalseNegatives();
        logger.info(tp);
        logger.info(fp);
        logger.info(fn);
        logger.info(String.format("Prcision: %.2f", ((float) tp) / (tp + fp)));
        logger.info(String.format("Rappel:    %.2f", ((float) tp) / (tp + fn)));
    }
}