001/*
002 * Copyright (c) 2012, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.timeml.eval;
025
026import java.io.File;
027import java.util.Arrays;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.uima.UIMAFramework;
033import org.apache.uima.util.Level;
034import org.cleartk.eval.AnnotationStatistics;
035import org.cleartk.corpus.timeml.TempEval2010CollectionReader;
036
037/**
038 * <br>
039 * Copyright (c) 2012, Regents of the University of Colorado <br>
040 * All rights reserved.
041 * 
042 * @author Steven Bethard
043 */
044public abstract class TempEval2010Main {
045
046  public static void main(String[] args) throws Exception {
047    for (TempEval2010Main main : Arrays.asList(
048        new TempEval2010TaskAAttributes(),
049        new TempEval2010TaskAExtents(),
050        new TempEval2010TaskBAttributes(),
051        new TempEval2010TaskBExtents(),
052        new TempEval2010TaskC(),
053        new TempEval2010TaskD(),
054        new TempEval2010TaskE(),
055        new TempEval2010TaskF())) {
056      main.runMain(args);
057    }
058  }
059
060  enum Command {
061    CV, TEST, TRAIN
062  }
063
064  public void runMain(String[] args) throws Exception {
065    String className = this.getClass().getName();
066    if (args.length != 4) {
067      System.err.printf(
068          "usage: java %s {cv,test,train} tempeval-training-dir tempeval-test-dir output-dir",
069          className);
070      System.exit(1);
071    }
072
073    Command command = Command.valueOf(args[0].toUpperCase());
074    char[] lineChars = new char[className.length()];
075    Arrays.fill(lineChars, '=');
076    String line = new String(lineChars);
077    String message = String.format("\n%s\n%s\n%s", line, className, line);
078    UIMAFramework.getLogger(this.getClass()).log(Level.INFO, message);
079    File tempEvalTrainingDir = new File(args[1]);
080    File tempEvalTestDir = new File(args[2]);
081    File outputDirectory = new File(args[3]);
082
083    TempEval2010Evaluation evaluation = this.getEvaluation(
084        tempEvalTrainingDir,
085        tempEvalTestDir,
086        outputDirectory);
087
088    switch (command) {
089      case CV:
090        List<Map<ModelInfo<?>, AnnotationStatistics<String>>> foldResults = evaluation.crossValidation(
091            TempEval2010CollectionReader.getAnnotatedFileNames(tempEvalTrainingDir),
092            5);
093        Map<String, AnnotationStatistics<String>> overallResults = new HashMap<String, AnnotationStatistics<String>>();
094        for (Map<ModelInfo<?>, AnnotationStatistics<String>> results : foldResults) {
095          for (ModelInfo<?> modelInfo : results.keySet()) {
096            String key = modelInfo.annotatedFeatureName;
097            if (!overallResults.containsKey(key)) {
098              overallResults.put(key, new AnnotationStatistics<String>());
099            }
100            overallResults.get(key).addAll(results.get(modelInfo));
101          }
102        }
103        for (String key : overallResults.keySet()) {
104          System.err.println(key);
105          System.err.println(overallResults.get(key));
106        }
107        break;
108      case TEST:
109        Map<ModelInfo<?>, AnnotationStatistics<String>> results = evaluation.trainAndTest(
110            TempEval2010CollectionReader.getAnnotatedFileNames(tempEvalTrainingDir),
111            TempEval2010CollectionReader.getAnnotatedFileNames(tempEvalTestDir));
112        for (ModelInfo<?> modelInfo : results.keySet()) {
113          System.err.println(modelInfo.annotatedFeatureName);
114          System.err.println(results.get(modelInfo));
115          System.err.println(results.get(modelInfo).confusions());
116        }
117        break;
118      case TRAIN:
119        throw new UnsupportedOperationException();
120        // break;
121    }
122  }
123
124  protected abstract TempEval2010Evaluation getEvaluation(
125      File trainDir,
126      File testDir,
127      File outputDir) throws Exception;
128}