001/** 002 * Copyright (c) 2007-2008, 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.corpus.timeml; 025 026import java.io.BufferedReader; 027import java.io.File; 028import java.io.IOException; 029import java.io.InputStreamReader; 030import java.net.URI; 031import java.net.URL; 032import java.util.ArrayList; 033import java.util.HashMap; 034import java.util.List; 035import java.util.Map; 036 037import org.apache.uima.UimaContext; 038import org.apache.uima.analysis_engine.AnalysisEngineDescription; 039import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 040import org.apache.uima.jcas.JCas; 041import org.apache.uima.resource.ResourceInitializationException; 042import org.cleartk.timeml.type.Anchor; 043import org.cleartk.timeml.type.Event; 044import org.cleartk.timeml.type.TemporalLink; 045import org.cleartk.util.ParamUtil; 046import org.cleartk.util.ViewUriUtil; 047import org.apache.uima.fit.component.JCasAnnotator_ImplBase; 048import org.apache.uima.fit.descriptor.ConfigurationParameter; 049import org.apache.uima.fit.factory.AnalysisEngineFactory; 050import org.apache.uima.fit.util.JCasUtil; 051 052/** 053 * <br> 054 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 055 * All rights reserved. 056 * 057 * 058 * @author Steven Bethard 059 * 060 */ 061public class PlainTextTlinkGoldAnnotator extends JCasAnnotator_ImplBase { 062 063 public static final String PARAM_TLINK_FILE_URL = "tlinkFileUrl"; 064 065 @ConfigurationParameter( 066 name = PARAM_TLINK_FILE_URL, 067 mandatory = true, 068 description = "the URL to a plain-text TLINK file, e.g." 069 + "http://www.bethard.info/data/timebank-verb-clause.txt") 070 private String tlinkFileUrl; 071 072 public void setTlinkFileUrl(String tlinkFileUrl) { 073 this.tlinkFileUrl = tlinkFileUrl; 074 } 075 076 public static AnalysisEngineDescription getDescription() throws ResourceInitializationException { 077 return AnalysisEngineFactory.createEngineDescription( 078 PlainTextTlinkGoldAnnotator.class, 079 PARAM_TLINK_FILE_URL, 080 ParamUtil.getParameterValue( 081 PARAM_TLINK_FILE_URL, 082 "http://www.bethard.info/data/timebank-verb-clause.txt")); 083 } 084 085 private Map<String, List<TLINK>> fileTLINKs; 086 087 @Override 088 public void initialize(UimaContext context) throws ResourceInitializationException { 089 super.initialize(context); 090 091 this.fileTLINKs = new HashMap<String, List<TLINK>>(); 092 try { 093 BufferedReader tlinkFileReader = new BufferedReader(new InputStreamReader(new URL( 094 this.tlinkFileUrl).openStream())); 095 String line; 096 while ((line = tlinkFileReader.readLine()) != null) 097 if (!line.startsWith("#")) { 098 String[] columns = line.split("\\s+"); 099 TLINK tlink = new TLINK(columns[1], columns[2], columns[3]); 100 if (!this.fileTLINKs.containsKey(columns[0])) { 101 this.fileTLINKs.put(columns[0], new ArrayList<TLINK>()); 102 } 103 this.fileTLINKs.get(columns[0]).add(tlink); 104 } 105 tlinkFileReader.close(); 106 } catch (IOException e) { 107 throw new ResourceInitializationException(e); 108 } 109 } 110 111 @Override 112 public void process(JCas jCas) throws AnalysisEngineProcessException { 113 URI uri = ViewUriUtil.getURI(jCas); 114 String filePath = uri.getPath(); 115 String fileBase = new File(filePath).getName().replaceAll("\\..*", ""); 116 if (this.fileTLINKs.containsKey(fileBase)) { 117 Map<String, Anchor> anchors = new HashMap<String, Anchor>(); 118 for (Anchor anchor : JCasUtil.select(jCas, Anchor.class)) { 119 anchors.put(anchor.getId(), anchor); 120 if (anchor instanceof Event) { 121 Event event = (Event) anchor; 122 anchors.put(event.getEventInstanceID(), event); 123 } 124 } 125 for (TLINK tlink : this.fileTLINKs.get(fileBase)) { 126 int offset = jCas.getDocumentText().length(); 127 TemporalLink temporalLink = new TemporalLink(jCas, offset, offset); 128 Anchor source = this.getAnchor(anchors, tlink.sourceID, uri); 129 Anchor target = this.getAnchor(anchors, tlink.targetID, uri); 130 if (source != null && target != null) { 131 temporalLink.setSource(source); 132 temporalLink.setTarget(target); 133 temporalLink.setRelationType(tlink.relationType); 134 temporalLink.addToIndexes(); 135 } 136 } 137 } 138 } 139 140 private Anchor getAnchor(Map<String, Anchor> anchors, String id, URI uri) { 141 Anchor anchor = anchors.get(id); 142 if (anchor == null) { 143 this.getLogger().warn(String.format("no anchor for id %s in %s", id, uri)); 144 } 145 return anchor; 146 } 147 148 private static class TLINK { 149 public String sourceID; 150 151 public String targetID; 152 153 public String relationType; 154 155 public TLINK(String sourceID, String targetID, String relationType) { 156 this.sourceID = sourceID; 157 this.targetID = targetID; 158 this.relationType = relationType; 159 } 160 161 @Override 162 public String toString() { 163 return String.format("TLINK(%s, %s, %s)", this.sourceID, this.targetID, this.relationType); 164 } 165 } 166 167}