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.List; 028 029import org.apache.uima.UimaContext; 030import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 031import org.apache.uima.jcas.JCas; 032import org.apache.uima.resource.ResourceInitializationException; 033import org.cleartk.ml.CleartkAnnotator; 034import org.cleartk.ml.Feature; 035import org.cleartk.ml.Instance; 036import org.cleartk.ml.feature.extractor.CleartkExtractor; 037import org.cleartk.ml.feature.extractor.FeatureExtractor1; 038import org.cleartk.timeml.type.Event; 039import org.cleartk.token.type.Sentence; 040import org.cleartk.token.type.Token; 041import org.apache.uima.fit.util.JCasUtil; 042 043import com.google.common.collect.Lists; 044 045/** 046 * <br> 047 * Copyright (c) 2010, Regents of the University of Colorado <br> 048 * All rights reserved. 049 * 050 * Base class for annotators of TimeML EVENT attributes. 051 * 052 * @author Steven Bethard 053 */ 054public abstract class EventAttributeAnnotator<OUTCOME_TYPE> extends CleartkAnnotator<OUTCOME_TYPE> { 055 056 /** 057 * The list of feature extractors that will be applied directly to the Event annotation. 058 * 059 * Subclasses should override {@link #initialize(org.apache.uima.UimaContext)} to fill this list. 060 */ 061 protected List<FeatureExtractor1<Event>> eventFeatureExtractors; 062 063 /** 064 * The list of feature extractors that will be applied to the Event annotation, with a Sentence 065 * window. 066 * 067 * Subclasses should override {@link #initialize(org.apache.uima.UimaContext)} to fill this list. 068 */ 069 protected List<CleartkExtractor<Event, Token>> contextExtractors; 070 071 /** 072 * The attribute value that should be considered as a default, e.g. "NONE". When the attribute 073 * value is null in the CAS, this value will be used instead. Additionally, when the classifier 074 * produces this value, no attribute will be added. 075 * 076 * @return The default attribute value. 077 */ 078 protected abstract OUTCOME_TYPE getDefaultValue(); 079 080 /** 081 * Get the attribute value from the Event annotation. Typically this will be by calling something 082 * like {@link Event#getTense()}. 083 * 084 * If this method returns null, {@link #getDefaultValue()} will be called to produce an 085 * appropriate attribute value. 086 * 087 * @param event 088 * The Event annotation whose attribute is to be retrieved. 089 * @return The selected attribute value. 090 */ 091 protected abstract OUTCOME_TYPE getAttribute(Event event); 092 093 /** 094 * Set the attribute value on the Event annotation. Typically this will be by calling something 095 * like {@link Event#setTense(String)}. 096 * 097 * This method will not be called if the value is equal to {@link #getDefaultValue()}. 098 * 099 * @param event 100 * The Event annotation whose attribute is to be set. 101 * @param value 102 * The new attribute value. 103 */ 104 protected abstract void setAttribute(Event event, OUTCOME_TYPE value); 105 106 @Override 107 public void initialize(UimaContext context) throws ResourceInitializationException { 108 super.initialize(context); 109 this.eventFeatureExtractors = Lists.newArrayList(); 110 this.contextExtractors = Lists.newArrayList(); 111 } 112 113 @Override 114 public void process(JCas jCas) throws AnalysisEngineProcessException { 115 for (Sentence sentence : JCasUtil.select(jCas, Sentence.class)) { 116 for (Event event : JCasUtil.selectCovered(jCas, Event.class, sentence)) { 117 118 // assemble features 119 List<Feature> features = new ArrayList<Feature>(); 120 for (FeatureExtractor1<Event> extractor : this.eventFeatureExtractors) { 121 features.addAll(extractor.extract(jCas, event)); 122 } 123 for (CleartkExtractor<Event, Token> extractor : this.contextExtractors) { 124 features.addAll(extractor.extractWithin(jCas, event, sentence)); 125 } 126 127 // if training, determine the attribute value and write the 128 // instance 129 if (this.isTraining()) { 130 OUTCOME_TYPE attribute = this.getAttribute(event); 131 if (attribute == null) { 132 attribute = this.getDefaultValue(); 133 } 134 Instance<OUTCOME_TYPE> instance = new Instance<OUTCOME_TYPE>(); 135 instance.addAll(features); 136 instance.setOutcome(attribute); 137 this.dataWriter.write(instance); 138 } 139 140 // if predicting, propose an attribute value and modify the 141 // event annotation 142 else { 143 OUTCOME_TYPE label = this.classifier.classify(features); 144 this.setAttribute(event, label); 145 } 146 } 147 } 148 } 149}