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.propbank; 025 026import java.io.BufferedReader; 027import java.io.File; 028import java.io.FileNotFoundException; 029import java.io.FileReader; 030import java.io.IOException; 031import java.util.Collections; 032import java.util.LinkedList; 033 034import org.apache.uima.UimaContext; 035import org.apache.uima.cas.CASException; 036import org.apache.uima.collection.CollectionException; 037import org.apache.uima.jcas.JCas; 038import org.apache.uima.resource.ResourceInitializationException; 039import org.apache.uima.util.FileUtils; 040import org.apache.uima.util.Progress; 041import org.apache.uima.util.ProgressImpl; 042import org.cleartk.corpus.penntreebank.ListSpecification; 043import org.cleartk.corpus.penntreebank.PennTreebankReader; 044import org.cleartk.corpus.propbank.util.Propbank; 045import org.cleartk.util.CleartkInitializationException; 046import org.cleartk.util.ViewUriUtil; 047import org.apache.uima.fit.component.JCasCollectionReader_ImplBase; 048import org.apache.uima.fit.descriptor.ConfigurationParameter; 049import org.apache.uima.fit.descriptor.SofaCapability; 050 051/** 052 * <br> 053 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 054 * All rights reserved. 055 * 056 * 057 * <p> 058 * PropbankCollectionReader reads all <tt>.mrg</tt> files of the WSJ part of Treebank in lexical 059 * order, then reads the corresponding Propbank entries, and populates the "TreebankView" and 060 * "PropbankView" SOFAs. 061 * </p> 062 * 063 * @author Philip Ogren, Philipp Wetzler 064 */ 065 066@SofaCapability(outputSofas = { 067 PropbankConstants.PROPBANK_VIEW, 068 PennTreebankReader.TREEBANK_VIEW, 069 ViewUriUtil.URI }) 070public class PropbankGoldReader extends JCasCollectionReader_ImplBase { 071 072 public static final String PARAM_PROPBANK_FILE_NAME = "propbankFileName"; 073 074 @ConfigurationParameter( 075 name = PARAM_PROPBANK_FILE_NAME, description = "points to propbank data file", mandatory = true) 076 private String propbankFileName; 077 078 public static final String PARAM_PENNTREEBANK_DIRECTORY_NAME = "penntreebankDirectoryName"; 079 080 private static final String PENN_TREEBANK_DIRECTORY_DESCRIPTION = "points to the PennTreebank corpus. " 081 + "The directory should contain subdirectories corresponding to the sections (e.g. \"00\", \"01\", etc.) " 082 + "That is, if a local copy of PennTreebank sits at C:/Data/PTB/wsj/mrg, then the subdirectory C:/Data/PTB/wsj/mrg/00 should exist. " 083 + "There are 24 sections in PTB corresponding to the directories 00, 01, 02, ... 24."; 084 085 @ConfigurationParameter( 086 name = PARAM_PENNTREEBANK_DIRECTORY_NAME, 087 description = PENN_TREEBANK_DIRECTORY_DESCRIPTION, mandatory = true) 088 private String penntreebankDirectoryName; 089 090 public static final String PARAM_WSJ_SECTIONS = "wsjSections"; 091 092 @ConfigurationParameter( 093 name = PARAM_WSJ_SECTIONS, 094 description = "Determines which sections of WSJ will be used. The format allows for comma-separated section numbers and section ranges, for example \"02,07-12,16\".", 095 mandatory = true) 096 private String wsjSections; 097 098 /** 099 * holds all of the propbank data from props.txt. One entry per line in the file 100 */ 101 protected LinkedList<String> propbankData; 102 103 protected File treebankDirectory; 104 105 protected LinkedList<File> treebankFiles; 106 107 protected int totalTreebankFiles = 0; 108 109 protected ListSpecification wsjSpecification; 110 111 @Override 112 public void initialize(UimaContext context) throws ResourceInitializationException { 113 try { 114 this.wsjSpecification = new ListSpecification(wsjSections); 115 116 File propbankFile = new File(propbankFileName); 117 if (!propbankFile.exists()) { 118 throw CleartkInitializationException.fileNotFound(propbankFile); 119 } 120 BufferedReader reader = new BufferedReader(new FileReader(propbankFile)); 121 try { 122 propbankData = new LinkedList<String>(); 123 String line; 124 while ((line = reader.readLine()) != null) { 125 propbankData.add(line); 126 } 127 } finally { 128 reader.close(); 129 } 130 Collections.sort(propbankData); 131 132 this.treebankFiles = new LinkedList<File>(); 133 treebankDirectory = new File(penntreebankDirectoryName); 134 // don't forget that the paths in props.txt have "wsj" in the name. 135 File wsjDirectory = new File(treebankDirectory, "wsj"); 136 if (!wsjDirectory.exists()) { 137 throw CleartkInitializationException.fileNotFound(wsjDirectory); 138 } 139 PennTreebankReader.collectSections(wsjDirectory, this.treebankFiles, this.wsjSpecification); 140 Collections.sort(treebankFiles); 141 this.totalTreebankFiles = treebankFiles.size(); 142 143 } catch (FileNotFoundException fnfe) { 144 throw new ResourceInitializationException(fnfe); 145 } catch (IOException ioe) { 146 throw new ResourceInitializationException(ioe); 147 } 148 } 149 150 /** 151 * Reads the next file and stores its text in <b>cas</b> as the "TreebankView" SOFA. Then stores 152 * the corresponding Propbank entries in the "PropbankView" SOFA. 153 */ 154 public void getNext(JCas jCas) throws IOException, CollectionException { 155 JCas tbView, pbView; 156 try { 157 tbView = jCas.createView(PennTreebankReader.TREEBANK_VIEW); 158 pbView = jCas.createView(PropbankConstants.PROPBANK_VIEW); 159 } catch (CASException ce) { 160 throw new CollectionException(ce); 161 } 162 163 File treebankFile = treebankFiles.removeFirst(); 164 ViewUriUtil.setURI(jCas, treebankFile.toURI()); 165 166 StringBuffer propbankText = new StringBuffer(); 167 168 /* 169 * The logic here is rather fragile and should be rewritten and/or unit tested. I changed the 170 * code so that the comparison is between the canonical paths. (PVO) 171 */ 172 while (propbankData.size() > 0) { 173 File nextPbFile = new File(treebankDirectory.getPath() + File.separator 174 + Propbank.filenameFromString(propbankData.getFirst())).getCanonicalFile(); 175 176 int c = treebankFile.getCanonicalFile().compareTo(nextPbFile); 177 if (c < 0) { 178 break; 179 } else if (c > 0) { 180 propbankData.removeFirst(); 181 continue; 182 } 183 184 propbankText.append(propbankData.removeFirst() + "\n"); 185 } 186 187 tbView.setSofaDataString(FileUtils.file2String(treebankFile), "text/plain"); 188 pbView.setSofaDataString(propbankText.toString(), "text/plain"); 189 } 190 191 public void close() throws IOException { 192 } 193 194 public Progress[] getProgress() { 195 return new Progress[] { new ProgressImpl( 196 totalTreebankFiles - treebankFiles.size(), 197 totalTreebankFiles, 198 Progress.ENTITIES) }; 199 } 200 201 public boolean hasNext() throws IOException, CollectionException { 202 if (treebankFiles.size() > 0) 203 return true; 204 else 205 return false; 206 } 207 208 public void setPropbankFileName(String propbankFileName) { 209 this.propbankFileName = propbankFileName; 210 } 211 212 public void setPenntreebankDirectoryName(String treebankDirectoryName) { 213 this.penntreebankDirectoryName = treebankDirectoryName; 214 } 215 216 public void setWsjSections(String wsjSections) { 217 this.wsjSections = wsjSections; 218 } 219 220}