Ace2005Writer extends JCasAnnotator_ImplBase { @Override public void process(JCas jCas) throws AnalysisEngineProcessException { String uri = new File(ViewUriUtil.getURI(jCas)).getName(); String docId = uri.substring(0, uri.indexOf(".sgm")); Ace2005Document document; document = JCasUtil.select(jCas, Ace2005Document.class).iterator().next(); Document xml = new Document(); Element sourceFileElement = new Element("source_file"); sourceFileElement.setAttribute("URI", uri); sourceFileElement.setAttribute("SOURCE", document.getAceSource()); sourceFileElement.setAttribute("TYPE", document.getAceType()); xml.addContent(sourceFileElement); Element documentElement = new Element("document"); documentElement.setAttribute("DOCID", docId); sourceFileElement.addContent(documentElement); FSIterator<FeatureStructure> namedEntities = jCas.getFSIndexRepository().getAllIndexedFS( jCas.getCasType(NamedEntity.type)); while (namedEntities.hasNext()) { NamedEntity namedEntity = (NamedEntity) namedEntities.next(); Element namedEntityElement = new Element("entity"); namedEntityElement.setAttribute("ID", "" + idIndex++); namedEntityElement.setAttribute("TYPE", namedEntity.getEntityType()); String entitySubtype = namedEntity.getEntitySubtype(); if (entitySubtype != null) namedEntityElement.setAttribute("SUBTYPE", entitySubtype); String entityClass = namedEntity.getEntityClass(); if (entityClass != null) namedEntityElement.setAttribute("CLASS", entityClass); FSArray namedEntityMentions = namedEntity.getMentions(); for (int i = 0; i < namedEntityMentions.size(); i++) { NamedEntityMention namedEntityMention = (NamedEntityMention) namedEntityMentions.get(i); Element namedEntityMentionElement = new Element("entity_mention"); namedEntityMentionElement.setAttribute("ID", "" + idIndex++); namedEntityMentionElement.setAttribute("TYPE", namedEntityMention.getMentionType()); namedEntityMentionElement.addContent(createExtentElement( "extent", namedEntityMention.getAnnotation())); namedEntityMentionElement.addContent(createExtentElement( "head", namedEntityMention.getHead())); namedEntityElement.addContent(namedEntityMentionElement); } documentElement.addContent(namedEntityElement); } XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); try { FileOutputStream stream = new FileOutputStream(new File(outputDirectory, docId + ".cleartk.xml")); xmlOut.output(xml, stream); stream.close(); } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }  @Override void initialize(UimaContext context); @Override void process(JCas jCas); void setOutputDirectoryName(String outputDirectoryName); static final String PARAM_OUTPUT_DIRECTORY_NAME; }
@Test public void testOutputFile() throws Exception { AnalysisEngine engine = AnalysisEngineFactory.createEngine( Ace2005Writer.class, Ace2005Writer.PARAM_OUTPUT_DIRECTORY_NAME, this.outputDirectory.getPath()); Ace2005Document document = new Ace2005Document(jCas); document.setAceSource("=source="); document.setAceType("=type="); document.setAceUri("uri.sgm"); document.addToIndexes(); jCas.setDocumentText("UCAR in North Boulder"); Chunk ucarChunk = new Chunk(jCas, 0, 4); Chunk northBoulderChunk = new Chunk(jCas, 8, 21); Chunk boulderChunk = new Chunk(jCas, 14, 21); NamedEntityMention ucarMention = new NamedEntityMention(jCas, 0, 4); ucarMention.setAnnotation(ucarChunk); ucarMention.setHead(ucarChunk); ucarMention.setMentionType("=ORG="); NamedEntity ucarEntity = new NamedEntity(jCas); ucarEntity.setEntityClass("=ucar-class="); ucarEntity.setEntityId("=ucar-id="); ucarEntity.setEntitySubtype("=ucar-subtype="); ucarEntity.setEntityType("=ucar-type"); ucarEntity.setMentions(new FSArray(jCas, 1)); ucarEntity.setMentions(0, ucarMention); ucarMention.setMentionedEntity(ucarEntity); NamedEntityMention boulderMention = new NamedEntityMention(jCas, 14, 21); boulderMention.setAnnotation(northBoulderChunk); boulderMention.setHead(boulderChunk); boulderMention.setMentionType("=LOC="); NamedEntity boulderEntity = new NamedEntity(jCas); boulderEntity.setEntityClass("=boulder-class="); boulderEntity.setEntityId("=boulder-id="); boulderEntity.setEntitySubtype("=boulder-subtype="); boulderEntity.setEntityType("=boulder-type"); boulderEntity.setMentions(new FSArray(jCas, 1)); boulderEntity.setMentions(0, boulderMention); boulderMention.setMentionedEntity(boulderEntity); TOP[] items = new TOP[] { document, ucarChunk, northBoulderChunk, boulderChunk, ucarMention, ucarEntity, boulderMention, boulderEntity }; for (TOP item : items) { item.addToIndexes(); } ViewUriUtil.setURI(jCas, new File("uri.sgm").toURI()); engine.process(jCas); engine.collectionProcessComplete(); String expectedText = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<source_file URI=\"uri.sgm\" SOURCE=\"=source=\" TYPE=\"=type=\">\n" + " <document DOCID=\"uri\">\n" + " <entity ID=\"0\" TYPE=\"=ucar-type\" SUBTYPE=\"=ucar-subtype=\" CLASS=\"=ucar-class=\">\n" + " <entity_mention ID=\"1\" TYPE=\"=ORG=\">\n" + " <extent>\n" + " <charseq START=\"0\" END=\"3\">UCAR</charseq>\n" + " </extent>\n" + " <head>\n" + " <charseq START=\"0\" END=\"3\">UCAR</charseq>\n" + " </head>\n" + " </entity_mention>\n" + " </entity>\n" + " <entity ID=\"2\" TYPE=\"=boulder-type\" SUBTYPE=\"=boulder-subtype=\" CLASS=\"=boulder-class=\">\n" + " <entity_mention ID=\"3\" TYPE=\"=LOC=\">\n" + " <extent>\n" + " <charseq START=\"8\" END=\"20\">North Boulder</charseq>\n" + " </extent>\n" + " <head>\n" + " <charseq START=\"14\" END=\"20\">Boulder</charseq>\n" + " </head>\n" + " </entity_mention>\n" + " </entity>\n" + " </document>\n" + "</source_file>\n"); File outputFile = new File(this.outputDirectory, "uri.cleartk.xml"); String actualText = FileUtils.file2String(outputFile).replace("\r", ""); Assert.assertEquals(expectedText, actualText); }