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.List; 028 029import org.apache.uima.UimaContext; 030import org.apache.uima.analysis_engine.AnalysisEngineDescription; 031import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 032import org.apache.uima.jcas.JCas; 033import org.apache.uima.resource.ResourceInitializationException; 034import org.cleartk.ml.CleartkSequenceAnnotator; 035import org.cleartk.ml.Feature; 036import org.cleartk.ml.Instances; 037import org.cleartk.ml.chunking.BioChunking; 038import org.cleartk.ml.feature.extractor.CleartkExtractor; 039import org.cleartk.ml.feature.extractor.CoveredTextExtractor; 040import org.cleartk.ml.feature.extractor.FeatureExtractor1; 041import org.cleartk.ml.feature.extractor.NamedFeatureExtractor1; 042import org.cleartk.ml.feature.extractor.TypePathExtractor; 043import org.cleartk.ml.feature.extractor.CleartkExtractor.Following; 044import org.cleartk.ml.feature.extractor.CleartkExtractor.Preceding; 045import org.cleartk.ml.feature.function.CharacterCategoryPatternFunction; 046import org.cleartk.ml.liblinear.LibLinearStringOutcomeDataWriter; 047import org.cleartk.timeml.type.Time; 048import org.cleartk.timeml.util.CleartkInternalModelFactory; 049import org.cleartk.timeml.util.TimeWordsExtractor; 050import org.cleartk.token.type.Sentence; 051import org.cleartk.token.type.Token; 052import org.apache.uima.fit.factory.AnalysisEngineFactory; 053import org.apache.uima.fit.util.JCasUtil; 054 055import com.google.common.collect.Lists; 056 057/** 058 * <br> 059 * Copyright (c) 2011, Regents of the University of Colorado <br> 060 * All rights reserved. 061 * 062 * @author Steven Bethard 063 */ 064public class TimeAnnotator extends CleartkSequenceAnnotator<String> { 065 066 public static final CleartkInternalModelFactory FACTORY = new CleartkInternalModelFactory() { 067 068 @Override 069 public Class<?> getAnnotatorClass() { 070 return TimeAnnotator.class; 071 } 072 073 @Override 074 public Class<?> getDataWriterClass() { 075 return LibLinearStringOutcomeDataWriter.class; 076 } 077 078 @Override 079 public AnalysisEngineDescription getBaseDescription() throws ResourceInitializationException { 080 return AnalysisEngineFactory.createEngineDescription(TimeAnnotator.class); 081 } 082 }; 083 084 private List<NamedFeatureExtractor1<Token>> tokenFeatureExtractors; 085 086 private List<CleartkExtractor<Token, Token>> contextFeatureExtractors; 087 088 private BioChunking<Token, Time> chunking; 089 090 @Override 091 public void initialize(UimaContext context) throws ResourceInitializationException { 092 super.initialize(context); 093 094 // define chunking type 095 this.chunking = new BioChunking<Token, Time>(Token.class, Time.class); 096 097 // add features: word, character pattern, stem, pos 098 this.tokenFeatureExtractors = Lists.newArrayList(); 099 this.tokenFeatureExtractors.add(new CoveredTextExtractor<Token>()); 100 NamedFeatureExtractor1<Token> ex = CharacterCategoryPatternFunction.createExtractor(); 101 this.tokenFeatureExtractors.add(ex); 102 this.tokenFeatureExtractors.add(new TimeWordsExtractor<Token>()); 103 this.tokenFeatureExtractors.add(new TypePathExtractor<Token>(Token.class, "stem")); 104 this.tokenFeatureExtractors.add(new TypePathExtractor<Token>(Token.class, "pos")); 105 106 // add window of features before and after 107 this.contextFeatureExtractors = Lists.newArrayList(); 108 for (FeatureExtractor1<Token> extractor : this.tokenFeatureExtractors) { 109 this.contextFeatureExtractors.add(new CleartkExtractor<Token, Token>(Token.class, extractor, new Preceding( 110 3), new Following(3))); 111 } 112 } 113 114 @Override 115 public void process(JCas jCas) throws AnalysisEngineProcessException { 116 117 // classify tokens within each sentence 118 for (Sentence sentence : JCasUtil.select(jCas, Sentence.class)) { 119 List<Token> tokens = JCasUtil.selectCovered(jCas, Token.class, sentence); 120 121 // extract features for all tokens 122 List<List<Feature>> featureLists = new ArrayList<List<Feature>>(); 123 for (Token token : tokens) { 124 List<Feature> features = new ArrayList<Feature>(); 125 for (FeatureExtractor1<Token> extractor : this.tokenFeatureExtractors) { 126 features.addAll(extractor.extract(jCas, token)); 127 } 128 for (CleartkExtractor<Token, Token> extractor : this.contextFeatureExtractors) { 129 features.addAll(extractor.extractWithin(jCas, token, sentence)); 130 } 131 featureLists.add(features); 132 } 133 134 // during training, convert Times to chunk labels and write the training Instances 135 if (this.isTraining()) { 136 List<Time> times = JCasUtil.selectCovered(jCas, Time.class, sentence); 137 List<String> outcomes = this.chunking.createOutcomes(jCas, tokens, times); 138 this.dataWriter.write(Instances.toInstances(outcomes, featureLists)); 139 } 140 141 // during prediction, convert chunk labels to Times and add them to the CAS 142 else { 143 List<String> outcomes = this.classifier.classify(featureLists); 144 this.chunking.createChunks(jCas, tokens, outcomes); 145 } 146 } 147 148 // add IDs to all Times 149 int timeIndex = 1; 150 for (Time time : JCasUtil.select(jCas, Time.class)) { 151 if (time.getId() == null) { 152 String id = String.format("t%d", timeIndex); 153 time.setId(id); 154 timeIndex += 1; 155 } 156 } 157 } 158}