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.File; 027import java.io.IOException; 028import java.util.Collection; 029import java.util.List; 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.apache.uima.util.FileUtils; 037import org.apache.uima.util.Level; 038import org.cleartk.corpus.penntreebank.TreebankNodeConverter; 039import org.cleartk.syntax.constituent.type.TopTreebankNode; 040import org.cleartk.syntax.constituent.type.TreebankNode; 041import org.cleartk.timeml.type.Text; 042import org.cleartk.token.type.Sentence; 043import org.cleartk.token.type.Token; 044import org.cleartk.util.ViewUriUtil; 045import org.cleartk.util.treebank.TreebankFormatParser; 046import org.apache.uima.fit.component.JCasAnnotator_ImplBase; 047import org.apache.uima.fit.descriptor.ConfigurationParameter; 048import org.apache.uima.fit.factory.AnalysisEngineFactory; 049import org.apache.uima.fit.util.JCasUtil; 050 051/** 052 * <br> 053 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 054 * All rights reserved. 055 * 056 * 057 * 058 * @author Steven Bethard 059 */ 060public class TreebankAligningAnnotator extends JCasAnnotator_ImplBase { 061 062 public static final String PARAM_TREEBANK_DIRECTORY_NAME = "treebankDirectoryName"; 063 064 @ConfigurationParameter( 065 name = PARAM_TREEBANK_DIRECTORY_NAME, 066 mandatory = true, 067 description = "the path to the treebank directory containing the XX/wsj_XXXX.mrg files.") 068 private String treebankDirectoryName; 069 070 public void setTreebankDirectoryName(String treebankDirectoryName) { 071 this.treebankDirectoryName = treebankDirectoryName; 072 } 073 074 public static AnalysisEngineDescription getDescription(String treeBankDir) 075 throws ResourceInitializationException { 076 return AnalysisEngineFactory.createEngineDescription( 077 TreebankAligningAnnotator.class, 078 PARAM_TREEBANK_DIRECTORY_NAME, 079 treeBankDir); 080 081 } 082 083 private File treebankDirectory; 084 085 @Override 086 public void initialize(UimaContext context) throws ResourceInitializationException { 087 super.initialize(context); 088 this.treebankDirectory = new File(this.treebankDirectoryName); 089 } 090 091 @Override 092 public void process(JCas jCas) throws AnalysisEngineProcessException { 093 094 // determine the appropriate .mrg file name 095 String wsjPath = ViewUriUtil.getURI(jCas).getPath(); 096 String wsjName = new File(wsjPath).getName(); 097 String subdir = wsjName.substring(4, 6); 098 String mrgName = wsjName.replaceAll("\\.tml", ".mrg"); 099 File mrgFile = new File(new File(this.treebankDirectory, subdir), mrgName); 100 101 // read the parse text 102 String mrgText; 103 try { 104 mrgText = FileUtils.file2String(mrgFile); 105 } catch (IOException e) { 106 throw new AnalysisEngineProcessException(e); 107 } 108 109 // we need a TEXT element to know where to start 110 Collection<Text> texts = JCasUtil.select(jCas, Text.class); 111 if (texts.size() != 1) { 112 throw new IllegalArgumentException("expected 1 Text annotation, found " + texts.size()); 113 } 114 115 // parse the trees, skipping the document if there are alignment 116 // problems 117 int offset = texts.iterator().next().getBegin(); 118 String text = jCas.getDocumentText(); 119 List<org.cleartk.util.treebank.TopTreebankNode> utilTrees; 120 try { 121 utilTrees = TreebankFormatParser.parseDocument(mrgText, offset, text); 122 } catch (Exception e) { 123 this.getContext().getLogger().log( 124 Level.WARNING, 125 String.format("Skipping %s due to alignment problems", wsjPath), 126 e); 127 return; 128 } 129 130 // add Token, Sentence and TreebankNode annotations for the text 131 for (org.cleartk.util.treebank.TopTreebankNode utilTree : utilTrees) { 132 133 // create a Sentence and set its parse 134 TopTreebankNode tree = TreebankNodeConverter.convert(utilTree, jCas, true); 135 Sentence sentence = new Sentence(jCas, tree.getBegin(), tree.getEnd()); 136 sentence.addToIndexes(); 137 138 // create the Tokens and add them to the Sentence 139 for (int i = 0; i < tree.getTerminals().size(); i++) { 140 TreebankNode leaf = tree.getTerminals(i); 141 if (leaf.getBegin() != leaf.getEnd()) { 142 Token token = new Token(jCas, leaf.getBegin(), leaf.getEnd()); 143 token.setPos(leaf.getNodeType()); 144 token.addToIndexes(); 145 } 146 } 147 } 148 } 149}