001/** 002 * Copyright (c) 2010, 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.event; 025 026import java.util.ArrayList; 027import java.util.HashSet; 028import java.util.List; 029import java.util.Set; 030 031import org.apache.uima.UimaContext; 032import org.apache.uima.analysis_engine.AnalysisEngineDescription; 033import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 034import org.apache.uima.jcas.JCas; 035import org.apache.uima.resource.ResourceInitializationException; 036import org.cleartk.ml.CleartkAnnotator; 037import org.cleartk.ml.Feature; 038import org.cleartk.ml.Instance; 039import org.cleartk.ml.feature.extractor.CleartkExtractor; 040import org.cleartk.ml.feature.extractor.CleartkExtractorException; 041import org.cleartk.ml.feature.extractor.CoveredTextExtractor; 042import org.cleartk.ml.feature.extractor.FeatureExtractor1; 043import org.cleartk.ml.feature.extractor.TypePathExtractor; 044import org.cleartk.ml.feature.extractor.CleartkExtractor.Following; 045import org.cleartk.ml.feature.extractor.CleartkExtractor.Preceding; 046import org.cleartk.ml.liblinear.LibLinearStringOutcomeDataWriter; 047import org.cleartk.syntax.constituent.type.TreebankNode; 048import org.cleartk.syntax.constituent.type.TreebankNodeUtil; 049import org.cleartk.timeml.type.Event; 050import org.cleartk.timeml.util.CleartkInternalModelFactory; 051import org.cleartk.token.type.Sentence; 052import org.cleartk.token.type.Token; 053import org.apache.uima.fit.factory.AnalysisEngineFactory; 054import org.apache.uima.fit.util.JCasUtil; 055 056import com.google.common.collect.Lists; 057 058/** 059 * <br> 060 * Copyright (c) 2010, Regents of the University of Colorado <br> 061 * All rights reserved. 062 * 063 * Annotator for TimeML EVENT identification. 064 * 065 * @author Steven Bethard 066 */ 067public class EventAnnotator extends CleartkAnnotator<String> { 068 069 public static final CleartkInternalModelFactory FACTORY = new CleartkInternalModelFactory() { 070 071 @Override 072 public Class<?> getAnnotatorClass() { 073 return EventAnnotator.class; 074 } 075 076 @Override 077 public Class<?> getDataWriterClass() { 078 return LibLinearStringOutcomeDataWriter.class; 079 } 080 081 @Override 082 public AnalysisEngineDescription getBaseDescription() throws ResourceInitializationException { 083 return AnalysisEngineFactory.createEngineDescription(EventAnnotator.class); 084 } 085 }; 086 087 protected List<FeatureExtractor1<Token>> tokenFeatureExtractors; 088 089 protected List<CleartkExtractor<Token, Token>> contextExtractors; 090 091 @Override 092 public void initialize(UimaContext context) throws ResourceInitializationException { 093 super.initialize(context); 094 095 // add features: word, stem, pos 096 this.tokenFeatureExtractors = Lists.newArrayList(); 097 this.tokenFeatureExtractors.add(new CoveredTextExtractor<Token>()); 098 this.tokenFeatureExtractors.add(new TypePathExtractor<Token>(Token.class, "stem")); 099 this.tokenFeatureExtractors.add(new TypePathExtractor<Token>(Token.class, "pos")); 100 this.tokenFeatureExtractors.add(new ParentNodeFeaturesExtractor()); 101 102 // add window of features before and after 103 this.contextExtractors = Lists.newArrayList(); 104 this.contextExtractors.add(new CleartkExtractor<Token, Token>( 105 Token.class, 106 new CoveredTextExtractor<Token>(), 107 new Preceding(3), 108 new Following(3))); 109 } 110 111 @Override 112 public void process(JCas jCas) throws AnalysisEngineProcessException { 113 Set<Token> eventTokens = new HashSet<Token>(); 114 if (this.isTraining()) { 115 for (Event event : JCasUtil.select(jCas, Event.class)) { 116 for (Token token : JCasUtil.selectCovered(jCas, Token.class, event)) { 117 eventTokens.add(token); 118 } 119 } 120 } 121 122 int index = 1; 123 for (Sentence sentence : JCasUtil.select(jCas, Sentence.class)) { 124 for (Token token : JCasUtil.selectCovered(jCas, Token.class, sentence)) { 125 List<Feature> features = new ArrayList<Feature>(); 126 for (FeatureExtractor1<Token> extractor : this.tokenFeatureExtractors) { 127 features.addAll(extractor.extract(jCas, token)); 128 } 129 for (CleartkExtractor<Token, Token> extractor : this.contextExtractors) { 130 features.addAll(extractor.extractWithin(jCas, token, sentence)); 131 } 132 if (this.isTraining()) { 133 String label = eventTokens.contains(token) ? "Event" : "O"; 134 this.dataWriter.write(new Instance<String>(label, features)); 135 } else { 136 if (this.classifier.classify(features).equals("Event")) { 137 Event event = new Event(jCas, token.getBegin(), token.getEnd()); 138 event.setId(String.format("e%d", index)); 139 event.setEventInstanceID(String.format("ei%d", index)); 140 event.addToIndexes(); 141 index += 1; 142 } 143 } 144 } 145 } 146 } 147 148 private static class ParentNodeFeaturesExtractor implements FeatureExtractor1<Token> { 149 public ParentNodeFeaturesExtractor() { 150 } 151 152 @Override 153 public List<Feature> extract(JCas view, Token token) 154 throws CleartkExtractorException { 155 TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(view, token); 156 List<Feature> features = new ArrayList<Feature>(); 157 if (node != null) { 158 TreebankNode parent = node.getParent(); 159 if (parent != null) { 160 features.add(new Feature("ParentNodeType", parent.getNodeType())); 161 TreebankNode firstSibling = parent.getChildren(0); 162 if (firstSibling != node && firstSibling.getLeaf()) { 163 features.add(new Feature("FirstSiblingText", firstSibling.getCoveredText())); 164 } 165 } 166 } 167 return features; 168 } 169 } 170}