001/** 
002 * Copyright (c) 2011, 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;
025
026import java.io.File;
027
028import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
029import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
030import org.apache.uima.fit.factory.AnalysisEngineFactory;
031import org.apache.uima.fit.factory.UimaContextFactory;
032import org.apache.uima.fit.pipeline.SimplePipeline;
033import org.apache.uima.jcas.JCas;
034import org.apache.uima.util.Level;
035import org.apache.uima.util.Logger;
036import org.cleartk.corpus.timeml.TempEval2007Writer;
037import org.cleartk.opennlp.tools.ParserAnnotator;
038import org.cleartk.opennlp.tools.PosTaggerAnnotator;
039import org.cleartk.opennlp.tools.SentenceAnnotator;
040import org.cleartk.snowball.DefaultSnowballStemmer;
041import org.cleartk.timeml.event.EventAnnotator;
042import org.cleartk.timeml.event.EventAspectAnnotator;
043import org.cleartk.timeml.event.EventClassAnnotator;
044import org.cleartk.timeml.event.EventModalityAnnotator;
045import org.cleartk.timeml.event.EventPolarityAnnotator;
046import org.cleartk.timeml.event.EventTenseAnnotator;
047import org.cleartk.timeml.time.TimeAnnotator;
048import org.cleartk.timeml.time.TimeTypeAnnotator;
049import org.cleartk.timeml.tlink.TemporalLinkEventToDocumentCreationTimeAnnotator;
050import org.cleartk.timeml.tlink.TemporalLinkEventToSameSentenceTimeAnnotator;
051import org.cleartk.timeml.tlink.TemporalLinkEventToSubordinatedEventAnnotator;
052import org.cleartk.timeml.type.DocumentCreationTime;
053import org.cleartk.token.tokenizer.TokenAnnotator;
054import org.cleartk.util.cr.FilesCollectionReader;
055
056/**
057 * Command line utility for annotating plain text files with TimeML annotations. Usage:
058 * 
059 * <pre>
060 * java org.cleartk.timeml.TimeMLAnnotate input-file-or-dir [output-dir]
061 * </pre>
062 * 
063 * <br>
064 * Copyright (c) 2011, Regents of the University of Colorado <br>
065 * All rights reserved.
066 * 
067 * @author Steven Bethard
068 */
069public class TimeMlAnnotate {
070
071  private static void error(String message) throws Exception {
072    Logger logger = UimaContextFactory.createUimaContext().getLogger();
073    logger.log(
074        Level.SEVERE,
075        String.format(
076            "%s\nusage: java %s input-file-or-dir [output-dir]",
077            TimeMlAnnotate.class.getName(),
078            message));
079    System.exit(1);
080  }
081
082  public static void main(String... args) throws Exception {
083    // check arguments
084    if (args.length != 1 && args.length != 2) {
085      error("wrong number of arguments");
086    } else if (!new File(args[0]).exists()) {
087      error("file or directory not found: " + args[0]);
088    }
089
090    // parse arguments
091    String inputFileOrDir = args[0];
092    File outputDir = new File(args.length == 2 ? args[1] : ".");
093    if (!outputDir.exists()) {
094      outputDir.mkdirs();
095    }
096
097    // run the components on the selected documents
098    SimplePipeline.runPipeline(
099        FilesCollectionReader.getCollectionReader(inputFileOrDir),
100        SentenceAnnotator.getDescription(),
101        TokenAnnotator.getDescription(),
102        PosTaggerAnnotator.getDescription(),
103        DefaultSnowballStemmer.getDescription("English"),
104        ParserAnnotator.getDescription(),
105        TimeAnnotator.FACTORY.getAnnotatorDescription(),
106        TimeTypeAnnotator.FACTORY.getAnnotatorDescription(),
107        EventAnnotator.FACTORY.getAnnotatorDescription(),
108        EventTenseAnnotator.FACTORY.getAnnotatorDescription(),
109        EventAspectAnnotator.FACTORY.getAnnotatorDescription(),
110        EventClassAnnotator.FACTORY.getAnnotatorDescription(),
111        EventPolarityAnnotator.FACTORY.getAnnotatorDescription(),
112        EventModalityAnnotator.FACTORY.getAnnotatorDescription(),
113        AnalysisEngineFactory.createEngineDescription(AddEmptyDCT.class),
114        TemporalLinkEventToDocumentCreationTimeAnnotator.FACTORY.getAnnotatorDescription(),
115        TemporalLinkEventToSameSentenceTimeAnnotator.FACTORY.getAnnotatorDescription(),
116        TemporalLinkEventToSubordinatedEventAnnotator.FACTORY.getAnnotatorDescription(),
117        TempEval2007Writer.getDescription(outputDir.getPath()));
118  }
119  
120  public static class AddEmptyDCT extends JCasAnnotator_ImplBase {
121
122    @Override
123    public void process(JCas jCas) throws AnalysisEngineProcessException {
124      DocumentCreationTime dct = new DocumentCreationTime(jCas, 0, 0);
125      dct.setFunctionInDocument("CREATION_TIME");
126      dct.setId("t0");
127      dct.addToIndexes();
128    }
129    
130  }
131}