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.tlink; 025 026import java.util.ArrayList; 027import java.util.HashMap; 028import java.util.Iterator; 029import java.util.List; 030import java.util.Map; 031 032import org.apache.uima.UimaContext; 033import org.apache.uima.analysis_engine.AnalysisEngineDescription; 034import org.apache.uima.jcas.JCas; 035import org.apache.uima.resource.ResourceInitializationException; 036import org.cleartk.ml.feature.extractor.FeatureExtractor1; 037import org.cleartk.ml.feature.extractor.TypePathExtractor; 038import org.cleartk.ml.liblinear.LibLinearStringOutcomeDataWriter; 039import org.cleartk.syntax.constituent.type.TreebankNode; 040import org.cleartk.syntax.constituent.type.TreebankNodeUtil; 041import org.cleartk.timeml.type.Event; 042import org.cleartk.timeml.util.CleartkInternalModelFactory; 043import org.cleartk.token.type.Sentence; 044import org.apache.uima.fit.factory.AnalysisEngineFactory; 045import org.apache.uima.fit.util.JCasUtil; 046 047import com.google.common.collect.Lists; 048 049/** 050 * <br> 051 * Copyright (c) 2011, Regents of the University of Colorado <br> 052 * All rights reserved. 053 * 054 * @author Steven Bethard 055 */ 056public class TemporalLinkMainEventToNextSentenceMainEventAnnotator extends 057 TemporalLinkAnnotator_ImplBase<Event, Event> { 058 059 public static final CleartkInternalModelFactory FACTORY = new CleartkInternalModelFactory() { 060 @Override 061 public Class<?> getAnnotatorClass() { 062 return TemporalLinkMainEventToNextSentenceMainEventAnnotator.class; 063 } 064 065 @Override 066 public Class<?> getDataWriterClass() { 067 return LibLinearStringOutcomeDataWriter.class; 068 } 069 070 @Override 071 public AnalysisEngineDescription getBaseDescription() throws ResourceInitializationException { 072 return AnalysisEngineFactory.createEngineDescription(TemporalLinkMainEventToNextSentenceMainEventAnnotator.class); 073 } 074 }; 075 076 public TemporalLinkMainEventToNextSentenceMainEventAnnotator() { 077 super(Event.class, Event.class, "BEFORE", "OVERLAP", "AFTER"); 078 } 079 080 @Override 081 public void initialize(UimaContext context) throws ResourceInitializationException { 082 super.initialize(context); 083 084 List<FeatureExtractor1<Event>> extractors = Lists.newArrayList(); 085 extractors.add(new TypePathExtractor<Event>(Event.class, "tense")); 086 extractors.add(new TypePathExtractor<Event>(Event.class, "aspect")); 087 extractors.add(new TypePathExtractor<Event>(Event.class, "eventClass")); 088 089 this.setSourceExtractors(extractors); 090 this.setTargetExtractors(extractors); 091 } 092 093 @Override 094 protected List<SourceTargetPair> getSourceTargetPairs(JCas jCas) { 095 List<SourceTargetPair> pairs = Lists.newArrayList(); 096 Iterator<Sentence> sentences = JCasUtil.select(jCas, Sentence.class).iterator(); 097 Sentence prev = sentences.hasNext() ? sentences.next() : null; 098 while (sentences.hasNext()) { 099 Sentence curr = sentences.next(); 100 Event source = getMainEvent(jCas, prev); 101 Event target = getMainEvent(jCas, curr); 102 if (source != null && target != null) { 103 pairs.add(new SourceTargetPair(source, target)); 104 } 105 prev = curr; 106 } 107 return pairs; 108 } 109 110 private Event getMainEvent(JCas jCas, Sentence sentence) { 111 // group events by their depth, and record the minimum depth 112 Integer minDepth = null; 113 Map<Integer, List<Event>> depthEvents = new HashMap<Integer, List<Event>>(); 114 for (Event event : JCasUtil.selectCovered(jCas, Event.class, sentence)) { 115 TreebankNode node = TreebankNodeUtil.selectMatchingLeaf(jCas, event); 116 Integer depth = node == null ? null : TreebankNodeUtil.getDepth(node); 117 if (!depthEvents.containsKey(depth)) { 118 depthEvents.put(depth, new ArrayList<Event>()); 119 } 120 depthEvents.get(depth).add(event); 121 if (depth != null && (minDepth == null || depth < minDepth)) { 122 minDepth = depth; 123 } 124 } 125 // select the last event 126 if (depthEvents.isEmpty()) { 127 return null; 128 } else { 129 List<Event> events = depthEvents.get(minDepth); 130 return events.get(events.size() - 1); 131 } 132 } 133}