de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.ReportTools.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.ReportTools.java

Source

/*
 * Copyright 2015
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universitt 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 de.tudarmstadt.ukp.dkpro.argumentation.sequence.report;

import de.tudarmstadt.ukp.dkpro.argumentation.preprocessing.annotation.ArgumentBIOAnnotator;
import de.tudarmstadt.ukp.dkpro.tc.svmhmm.util.ConfusionMatrix;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author Ivan Habernal
 */
public class ReportTools {
    /**
     * Returns a list of  {@code size} elements and if the {@code goldLabelSentenceLevel} is B-XX
     * tag, returns [B-XX, I-XX, I-XX, ...], otherwise [I-XX, I-XX, I-XX, ..]
     *
     * @param goldLabelSentenceLevel gold label for sentence
     * @param size                   length of sentence
     * @return list
     */
    public static List<String> recreateTokenLabels(String goldLabelSentenceLevel, int size) {
        List<String> result = new ArrayList<>();
        result.add(goldLabelSentenceLevel);

        for (int i = 1; i < size; i++) {

            if (goldLabelSentenceLevel.endsWith(ArgumentBIOAnnotator.B_SUFFIX)) {
                result.add(goldLabelSentenceLevel.replace(ArgumentBIOAnnotator.B_SUFFIX,
                        ArgumentBIOAnnotator.I_SUFFIX));
            } else {
                result.add(goldLabelSentenceLevel);
            }
        }

        //        System.out.println(goldLabelSentenceLevel + ": " + result);

        return result;
    }

    /**
     * Prints macro F1 and F1 for each label into file (each line: label + F1, tab delimited)
     *
     * @param confusionMatrix matrix
     * @param file            file
     * @throws IOException
     */
    public static void printFMeasuresToFile(ConfusionMatrix confusionMatrix, File file) throws IOException {
        // and write to the output
        PrintWriter pw = new PrintWriter(file);
        // print macro F1 first
        pw.printf(Locale.ENGLISH, "%s\t%.3f%n", "Macro-Fm", confusionMatrix.getMacroFMeasure());

        // then for all labels
        for (Map.Entry<String, Double> entry : confusionMatrix.getFMeasureForLabels().entrySet()) {
            pw.printf(Locale.ENGLISH, "%s\t%.3f%n", entry.getKey(), entry.getValue());
        }
        IOUtils.closeQuietly(pw);
    }
}