Java tutorial
/* * 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; import de.tudarmstadt.ukp.dkpro.tc.svmhmm.util.ConfusionMatrix; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import java.io.File; import java.io.FileFilter; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * @author Ivan Habernal */ public class EvalHelper { public static final FileFilter DIRECTORY_FILTER = new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }; public static File[] listSubFolders(File folder) { File[] subFolders = folder.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); if (subFolders.length == 0) { throw new IllegalStateException("No sub-folders found in " + folder.getAbsolutePath()); } return subFolders; } public static Map<String, File> listSubFoldersAndRemoveUUID(File methodFolder) { File[] subFolders = listSubFolders(methodFolder); Map<String, File> result = new HashMap<>(); for (File subFolder : subFolders) { String name = removeUUID(subFolder.getName()); result.put(name, subFolder); } return result; } public static String removeUUID(String name) { return name.replaceAll("-(\\w{8}(-\\w{4}){3}-\\w{12}?)", ""); } /** * Reads predicted and gold data from predictions CSV file and fills&returns the confusion matrix * * @param csvPredictions predictions (gold; predicted) * @return matrix */ public static ConfusionMatrix createConfusionMatrixFromCSVPredictions(File csvPredictions) throws IOException { CSVParser csvParser = new CSVParser(new FileReader(csvPredictions), CSVFormat.DEFAULT.withCommentMarker('#')); ConfusionMatrix result = new ConfusionMatrix(); for (CSVRecord csvRecord : csvParser) { // copy record // update confusion matrix result.increaseValue(csvRecord.get(0), csvRecord.get(1)); } return result; } /** * Reads predicted and gold data from predictions CSV file and fills&returns the confusion matrix * * @param csvPredictions predictions (gold; predicted) * @return matrix */ // needs to be updated to the new implementation /* public static ConfusionMatrix createProbabilisticConfusionMatrixFromCSVPredictions( File csvPredictions) throws IOException { CSVParser csvParser = new CSVParser(new FileReader(csvPredictions), CSVFormat.DEFAULT.withCommentMarker('#')); ConfusionMatrix result = new ConfusionMatrix(); Set<String> categories = new TreeSet<>(); for (CSVRecord csvRecord : csvParser) { categories.add(csvRecord.get(0)); categories.add(csvRecord.get(1)); } ConfusionMatrix pcm = new ConfusionMatrix( categories.toArray(new String[categories.size()])); csvParser = new CSVParser(new FileReader(csvPredictions), CSVFormat.DEFAULT.withCommentMarker('#')); for (CSVRecord csvRecord : csvParser) { pcm.inc(csvRecord.get(0), csvRecord.get(1)); } return pcm; } */ }