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.ace2005; 025 026import java.io.File; 027import java.io.FileOutputStream; 028import java.io.IOException; 029 030import org.apache.uima.UimaContext; 031import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 032import org.apache.uima.cas.FSIterator; 033import org.apache.uima.cas.FeatureStructure; 034import org.apache.uima.jcas.JCas; 035import org.apache.uima.jcas.cas.FSArray; 036import org.apache.uima.jcas.tcas.Annotation; 037import org.apache.uima.resource.ResourceInitializationException; 038import org.cleartk.ne.type.Ace2005Document; 039import org.cleartk.ne.type.NamedEntity; 040import org.cleartk.ne.type.NamedEntityMention; 041import org.cleartk.util.ViewUriUtil; 042import org.jdom2.Document; 043import org.jdom2.Element; 044import org.jdom2.output.Format; 045import org.jdom2.output.XMLOutputter; 046import org.apache.uima.fit.component.JCasAnnotator_ImplBase; 047import org.apache.uima.fit.descriptor.ConfigurationParameter; 048import org.apache.uima.fit.util.JCasUtil; 049 050/** 051 * <br> 052 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 053 * All rights reserved. 054 * 055 * 056 * @author Philip Ogren 057 * 058 */ 059 060public class Ace2005Writer extends JCasAnnotator_ImplBase { 061 062 public static final String PARAM_OUTPUT_DIRECTORY_NAME = "outputDirectoryName"; 063 064 @ConfigurationParameter( 065 name = PARAM_OUTPUT_DIRECTORY_NAME, 066 mandatory = true, 067 description = "provides the path of the directory where the XML files should be written.") 068 private String outputDirectoryName; 069 070 private File outputDirectory; 071 072 private int idIndex = 0; 073 074 @Override 075 public void initialize(UimaContext context) throws ResourceInitializationException { 076 super.initialize(context); 077 078 outputDirectory = new File(outputDirectoryName); 079 if (!outputDirectory.exists()) { 080 outputDirectory.mkdirs(); 081 } 082 } 083 084 private Element createExtentElement(String elementName, Annotation annotation) { 085 Element extent = new Element(elementName); 086 Element charseq = new Element("charseq"); 087 charseq.setAttribute("START", "" + annotation.getBegin()); 088 charseq.setAttribute("END", "" + (annotation.getEnd() - 1)); 089 charseq.setText(annotation.getCoveredText()); 090 extent.addContent(charseq); 091 return extent; 092 } 093 094 @Override 095 public void process(JCas jCas) throws AnalysisEngineProcessException { 096 String uri = new File(ViewUriUtil.getURI(jCas)).getName(); 097 String docId = uri.substring(0, uri.indexOf(".sgm")); 098 Ace2005Document document; 099 document = JCasUtil.select(jCas, Ace2005Document.class).iterator().next(); 100 101 Document xml = new Document(); 102 103 Element sourceFileElement = new Element("source_file"); 104 sourceFileElement.setAttribute("URI", uri); 105 sourceFileElement.setAttribute("SOURCE", document.getAceSource()); 106 sourceFileElement.setAttribute("TYPE", document.getAceType()); 107 xml.addContent(sourceFileElement); 108 109 Element documentElement = new Element("document"); 110 documentElement.setAttribute("DOCID", docId); 111 sourceFileElement.addContent(documentElement); 112 113 FSIterator<FeatureStructure> namedEntities = jCas.getFSIndexRepository().getAllIndexedFS( 114 jCas.getCasType(NamedEntity.type)); 115 while (namedEntities.hasNext()) { 116 NamedEntity namedEntity = (NamedEntity) namedEntities.next(); 117 Element namedEntityElement = new Element("entity"); 118 namedEntityElement.setAttribute("ID", "" + idIndex++); 119 namedEntityElement.setAttribute("TYPE", namedEntity.getEntityType()); 120 String entitySubtype = namedEntity.getEntitySubtype(); 121 if (entitySubtype != null) 122 namedEntityElement.setAttribute("SUBTYPE", entitySubtype); 123 String entityClass = namedEntity.getEntityClass(); 124 if (entityClass != null) 125 namedEntityElement.setAttribute("CLASS", entityClass); 126 127 FSArray namedEntityMentions = namedEntity.getMentions(); 128 for (int i = 0; i < namedEntityMentions.size(); i++) { 129 NamedEntityMention namedEntityMention = (NamedEntityMention) namedEntityMentions.get(i); 130 Element namedEntityMentionElement = new Element("entity_mention"); 131 namedEntityMentionElement.setAttribute("ID", "" + idIndex++); 132 namedEntityMentionElement.setAttribute("TYPE", namedEntityMention.getMentionType()); 133 134 namedEntityMentionElement.addContent(createExtentElement( 135 "extent", 136 namedEntityMention.getAnnotation())); 137 namedEntityMentionElement.addContent(createExtentElement( 138 "head", 139 namedEntityMention.getHead())); 140 namedEntityElement.addContent(namedEntityMentionElement); 141 } 142 documentElement.addContent(namedEntityElement); 143 } 144 145 XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); 146 try { 147 FileOutputStream stream = new FileOutputStream(new File(outputDirectory, docId 148 + ".cleartk.xml")); 149 xmlOut.output(xml, stream); 150 stream.close(); 151 } catch (IOException e) { 152 throw new AnalysisEngineProcessException(e); 153 } 154 155 } 156 157 public void setOutputDirectoryName(String outputDirectoryName) { 158 this.outputDirectoryName = outputDirectoryName; 159 } 160 161}