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.time;
025
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.List;
029
030import org.apache.uima.UimaContext;
031import org.apache.uima.analysis_engine.AnalysisEngineDescription;
032import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
033import org.apache.uima.fit.factory.AnalysisEngineFactory;
034import org.apache.uima.fit.util.JCasUtil;
035import org.apache.uima.jcas.JCas;
036import org.apache.uima.jcas.tcas.Annotation;
037import org.apache.uima.resource.ResourceInitializationException;
038import org.cleartk.ml.CleartkAnnotator;
039import org.cleartk.ml.Feature;
040import org.cleartk.ml.Instance;
041import org.cleartk.ml.feature.extractor.CleartkExtractor;
042import org.cleartk.ml.feature.extractor.CoveredTextExtractor;
043import org.cleartk.ml.feature.extractor.FeatureExtractor1;
044import org.cleartk.ml.feature.extractor.NamedFeatureExtractor1;
045import org.cleartk.ml.feature.extractor.CleartkExtractor.Bag;
046import org.cleartk.ml.feature.extractor.CleartkExtractor.Covered;
047import org.cleartk.ml.feature.function.CharacterCategoryPatternFunction;
048import org.cleartk.ml.liblinear.LibLinearStringOutcomeDataWriter;
049import org.cleartk.timeml.type.Time;
050import org.cleartk.timeml.util.CleartkInternalModelFactory;
051import org.cleartk.timeml.util.TimeWordsExtractor;
052import org.cleartk.token.type.Token;
053
054import com.google.common.collect.Lists;
055
056/**
057 * <br>
058 * Copyright (c) 2011, Regents of the University of Colorado <br>
059 * All rights reserved.
060 * 
061 * @author Steven Bethard
062 */
063public class TimeTypeAnnotator extends CleartkAnnotator<String> {
064
065  public static final CleartkInternalModelFactory FACTORY = new CleartkInternalModelFactory() {
066    @Override
067    public Class<?> getAnnotatorClass() {
068      return TimeTypeAnnotator.class;
069    }
070
071    @Override
072    public Class<?> getDataWriterClass() {
073      return LibLinearStringOutcomeDataWriter.class;
074    }
075
076    @Override
077    public AnalysisEngineDescription getBaseDescription() throws ResourceInitializationException {
078      return AnalysisEngineFactory.createEngineDescription(TimeTypeAnnotator.class);
079    }
080  };
081
082  private List<FeatureExtractor1<Time>> featuresExtractors;
083
084  @Override
085  public void initialize(UimaContext context) throws ResourceInitializationException {
086    super.initialize(context);
087    this.featuresExtractors = Lists.newArrayList();
088    this.featuresExtractors.add(new LastWordExtractor<Time>());
089    FeatureExtractor1<Time> ex = CharacterCategoryPatternFunction.createExtractor();
090    this.featuresExtractors.add(ex);
091    this.featuresExtractors.add(new TimeWordsExtractor<Time>());
092    this.featuresExtractors.add(new CleartkExtractor<Time, Token>(Token.class, new CoveredTextExtractor<Token>(), new Bag(new Covered())));
093  }
094
095  @Override
096  public void process(JCas jCas) throws AnalysisEngineProcessException {
097    for (Time time : JCasUtil.select(jCas, Time.class)) {
098      List<Feature> features = new ArrayList<Feature>();
099      for (FeatureExtractor1<Time> extractor : this.featuresExtractors) {
100        features.addAll(extractor.extract(jCas, time));
101      }
102      if (this.isTraining()) {
103        this.dataWriter.write(new Instance<String>(time.getTimeType(), features));
104      } else {
105        time.setTimeType(this.classifier.classify(features));
106      }
107    }
108  }
109
110  private static class LastWordExtractor<T extends Annotation> implements NamedFeatureExtractor1<T> {
111    
112    private String featureName;
113
114    public LastWordExtractor() {
115      this.featureName = "LastWord";
116    }
117    
118    @Override
119    public String getFeatureName() {
120      return this.featureName;
121    }
122
123    @Override
124    public List<Feature> extract(JCas view, T focusAnnotation) {
125      String[] words = focusAnnotation.getCoveredText().split("\\W+");
126      return Arrays.asList(new Feature(this.featureName, words[words.length - 1]));
127    }
128
129  }
130}