de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.helpers.FinalTableExtractor.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.helpers.FinalTableExtractor.java

Source

/*
 * Copyright 2016
 * 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.experiments.argumentation.sequence.evaluation.helpers;

import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;
import de.tudarmstadt.ukp.experiments.argumentation.sequence.DocumentDomain;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.*;

/**
 * (c) 2015 Ivan Habernal
 */
public class FinalTableExtractor {
    public static void main(String[] args) throws IOException {
        //        String summaryFile = args[0];

        String mainFolder = args[0];
        //                extractCDResults(summaryFile, "/tmp/outID.csv");
        //        extractCVResults(summaryFile, "/tmp/outCV.csv");

        //        createCrossEvaluationTables(new File(mainFolder), "cd_");
        createCrossEvaluationTables(new File(mainFolder), "cr_");
        createCrossEvaluationTables(new File(mainFolder), "cr_");

    }

    static final Set<String> ignoredMeasures = new HashSet<>();

    static {
        ignoredMeasures
                .addAll(Arrays.asList("Rebuttal-B", "Rebuttal-I", "Refutation-B", "Refutation-I", "Macro-Fm"));
    }

    public static void createCrossEvaluationTables(File mainFolder, final String prefix) throws IOException {
        Map<String, Table<String, String, Double>> domainResults = new TreeMap<>();

        File[] crossDomainFolders = mainFolder.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory() && pathname.getName().startsWith(prefix);
            }
        });

        System.out.println(Arrays.toString(crossDomainFolders));

        for (File crossDomainFolder : crossDomainFolders) {
            // get the target domain
            String[] folderNameSplit = crossDomainFolder.getName().split("_");
            String domain = folderNameSplit[1];
            String featureSet = folderNameSplit[2];

            File resultSummaryFile = new File(crossDomainFolder, "resultSummary.txt");
            for (String line : FileUtils.readLines(resultSummaryFile)) {
                // parse the line with evaluation
                String[] split = line.split("\\s+");
                String argumentComponent = split[0];
                Double value = Double.valueOf(split[1]);

                // create a new table if needed
                if (!domainResults.containsKey(domain)) {
                    domainResults.put(domain, TreeBasedTable.<String, String, Double>create());
                }

                // fill the table
                Table<String, String, Double> table = domainResults.get(domain);

                if (includeColumn(argumentComponent)) {
                    table.put(featureSet, argumentComponent, value);
                }
            }
        }

        System.out.println(domainResults.keySet());

        for (Map.Entry<String, Table<String, String, Double>> entry : domainResults.entrySet()) {

            System.out.println(entry.getKey());
            System.out.println(tableToCsv(entry.getValue()));
        }
    }

    @Deprecated
    public static void extractCDResults(String inFile, String outFile) throws IOException {
        File file = new File(inFile);

        Table<String, String, String> table = TreeBasedTable.create();

        List<String> lines = IOUtils.readLines(new FileInputStream(file));
        Iterator<String> iterator = lines.iterator();
        while (iterator.hasNext()) {
            String featureSet = iterator.next();

            // shorten fs name
            featureSet = featureSet.replaceAll("^\\./", "");

            // extract domain
            DocumentDomain domain = null;
            for (DocumentDomain currentDomain : DocumentDomain.values()) {
                if (featureSet.contains(currentDomain.toString())) {
                    if (domain != null) {
                        throw new IllegalStateException("!!!");
                    }
                    domain = currentDomain;
                }
            }
            if (domain == null) {
                throw new IllegalStateException("No domain found! " + featureSet);
            }

            String shortFeatureSet = featureSet.replaceAll("/.*", "");

            System.out.println(shortFeatureSet);

            String[] fsSettings = shortFeatureSet.split("_", 6);
            String clusters = fsSettings[4];

            if (includeRow(clusters)) {
                table.put(shortFeatureSet, "FS", fsSettings[1]);
            }

            // 12 lines with results
            for (int i = 0; i < 12; i++) {
                String line = iterator.next();
                String[] split = line.split("\\s+");
                String measure = split[0];
                Double value = Double.valueOf(split[1]);

                // only a50 and a100,s1000
                if (includeRow(clusters)) {
                    table.put(shortFeatureSet, "Clusters", clusters);
                    if (includeColumn(measure)) {
                        table.put(shortFeatureSet, domain.toString() + measure,
                                String.format(Locale.ENGLISH, "%.3f", value));
                    }
                }
            }
        }

        //        tableToCsv(table, new FileWriter(outFile));
    }

    public static void extractCVResults(String inFile, String outFile) throws IOException {
        File file = new File(inFile);

        Table<String, String, String> table = TreeBasedTable.create();

        List<String> lines = IOUtils.readLines(new FileInputStream(file));
        Iterator<String> iterator = lines.iterator();
        while (iterator.hasNext()) {
            String featureSet = iterator.next();

            featureSet = featureSet.replaceAll("^\\./", "");

            System.out.println(featureSet);
            // shorten fs name
            featureSet = featureSet.replaceAll("/.*", "");

            String[] fsSettings = featureSet.split("_", 6);
            String clusters = fsSettings[4];

            if (includeRow(clusters)) {
                table.put(featureSet, "FS", fsSettings[1]);
                table.put(featureSet, "Clusters", clusters);
            }

            // 12 lines with results
            for (int i = 0; i < 12; i++) {
                String line = iterator.next();
                String[] split = line.split("\\s+");
                String measure = split[0];
                Double value = Double.valueOf(split[1]);

                if (includeRow(clusters)) {
                    table.put(featureSet, "Clusters", clusters);
                    if (includeColumn(measure)) {
                        table.put(featureSet, measure, String.format(Locale.ENGLISH, "%.3f", value));
                    }
                }
            }
        }

        //        tableToCsv(table, new FileWriter(outFile));
    }

    private static boolean includeColumn(String measure) {
        return !ignoredMeasures.contains(measure);
    }

    private static boolean includeRow(String clusters) {
        return "a50".equals(clusters) || "a100,s1000".equals(clusters);
    }

    public static <T> String tableToCsv(Table<String, String, T> table) throws IOException {
        StringWriter sw = new StringWriter();
        CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);

        List<String> firstRow = new ArrayList<>();
        firstRow.add(" ");
        firstRow.addAll(table.columnKeySet());
        printer.printRecord(firstRow);

        for (String rowKey : table.rowKeySet()) {
            printer.print(rowKey);
            for (String columnKey : table.columnKeySet()) {
                printer.print(table.get(rowKey, columnKey));
            }
            printer.println();
        }

        printer.close();

        return sw.toString();
    }
}