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.IOException; 028import java.net.URI; 029import java.net.URISyntaxException; 030import java.util.ArrayList; 031import java.util.List; 032import java.util.regex.Matcher; 033import java.util.regex.Pattern; 034 035import org.apache.uima.UimaContext; 036import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 037import org.apache.uima.cas.CAS; 038import org.apache.uima.cas.CASException; 039import org.apache.uima.jcas.JCas; 040import org.apache.uima.jcas.cas.FSArray; 041import org.apache.uima.resource.ResourceInitializationException; 042import org.cleartk.ne.type.Chunk; 043import org.cleartk.ne.type.NamedEntity; 044import org.cleartk.ne.type.NamedEntityMention; 045import org.jdom2.Document; 046import org.jdom2.Element; 047import org.jdom2.JDOMException; 048import org.jdom2.input.SAXBuilder; 049import org.apache.uima.fit.component.JCasAnnotator_ImplBase; 050import org.apache.uima.fit.descriptor.SofaCapability; 051import org.apache.uima.fit.util.FSCollectionFactory; 052 053/** 054 * <br> 055 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 056 * All rights reserved. 057 * 058 * 059 * This parser works with the ACE2005CollectionReader to pre-populate the JCas with ACE named 060 * entities. Currently, it only loads information from the entity and entity_mention tags and adds 061 * NamedEntity and NamedEntityMention objects to the JCas. 062 * 063 * @author Philip Ogren 064 * 065 */ 066@SofaCapability( 067 inputSofas = { Ace2005Constants.ACE_2005_APF_URI_VIEW, CAS.NAME_DEFAULT_SOFA }, 068 outputSofas = {}) 069public class Ace2005GoldAnnotator extends JCasAnnotator_ImplBase { 070 071 Pattern ampPattern; 072 073 @Override 074 public void initialize(UimaContext aContext) throws ResourceInitializationException { 075 super.initialize(aContext); 076 ampPattern = Pattern.compile(Pattern.quote("&")); 077 } 078 079 @Override 080 public void process(JCas jCas) throws AnalysisEngineProcessException { 081 try { 082 String apfUri = jCas.getView(Ace2005Constants.ACE_2005_APF_URI_VIEW).getSofaDataURI(); 083 JCas initialView = jCas.getView(CAS.NAME_DEFAULT_SOFA); 084 String documentText = initialView.getDocumentText(); 085 SAXBuilder builder = new SAXBuilder(); 086 builder.setDTDHandler(null); 087 URI sofaDataURI = new URI(apfUri); 088 Document doc = builder.build(new File(sofaDataURI)); 089 090 Element apfSource = doc.getRootElement(); 091 Element apfDocument = apfSource.getChild("document"); 092 for (Element apfEntity : apfDocument.getChildren("entity")) { 093 NamedEntity namedEntity = new NamedEntity(initialView); 094 namedEntity.setEntityType(apfEntity.getAttributeValue("TYPE")); 095 namedEntity.setEntitySubtype(apfEntity.getAttributeValue("SUBTYPE")); 096 namedEntity.setEntityClass(apfEntity.getAttributeValue("CLASS")); 097 namedEntity.setEntityId(apfEntity.getAttributeValue("ID")); 098 namedEntity.addToIndexes(); 099 100 List<NamedEntityMention> mentions = new ArrayList<NamedEntityMention>(); 101 102 for (Element entityMention : apfEntity.getChildren("entity_mention")) { 103 int start = Integer.parseInt(entityMention.getChild("extent").getChild("charseq").getAttributeValue( 104 "START")); 105 int end = Integer.parseInt(entityMention.getChild("extent").getChild("charseq").getAttributeValue( 106 "END")); 107 String givenText = entityMention.getChild("extent").getChild("charseq").getText(); 108 String parsedText = documentText.substring(start, end + 1); 109 Matcher ampMatcher = ampPattern.matcher(parsedText); 110 parsedText = ampMatcher.replaceAll("&"); 111 112 NamedEntityMention mention = new NamedEntityMention(initialView, start, end + 1); 113 mention.setMentionId(entityMention.getAttributeValue("ID")); 114 mention.setMentionType(entityMention.getAttributeValue("TYPE")); 115 mention.setMentionedEntity(namedEntity); 116 117 Chunk chunk = new Chunk(initialView, start, end + 1); 118 mention.setAnnotation(chunk); 119 120 int headStart = Integer.parseInt(entityMention.getChild("head").getChild("charseq").getAttributeValue( 121 "START")); 122 int headEnd = Integer.parseInt(entityMention.getChild("head").getChild("charseq").getAttributeValue( 123 "END")); 124 Chunk head = new Chunk(initialView, headStart, headEnd + 1); 125 mention.setHead(head); 126 127 mention.addToIndexes(); 128 mentions.add(mention); 129 130 givenText = givenText.replaceAll("\\s+", " "); 131 parsedText = givenText.replaceAll("\\s+", " "); 132 133 // if(!givenText.equals(parsedText)) 134 // { 135 // 136 // System.out.println("given text and parsed text differ."); 137 // System.out.println(givenText); 138 // System.out.println(parsedText); 139 // System.out.println(apfDocument.getAttributeValue("DOCID")); 140 // System.out.println(apfEntity.getAttributeValue("ID")); 141 // System.out.println(documentText); 142 // } 143 } 144 namedEntity.setMentions(new FSArray(jCas, mentions.size())); 145 FSCollectionFactory.fillArrayFS(namedEntity.getMentions(), mentions); 146 } 147 148 } catch (CASException ce) { 149 throw new AnalysisEngineProcessException(ce); 150 } catch (IOException ioe) { 151 throw new AnalysisEngineProcessException(ioe); 152 } catch (JDOMException je) { 153 throw new AnalysisEngineProcessException(je); 154 } catch (URISyntaxException use) { 155 throw new AnalysisEngineProcessException(use); 156 } 157 } 158 159}