PPDWriter extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { FSIterator<Annotation> sentenceIt = aJCas.getAnnotationIndex(Sentence.type).iterator(); while (sentenceIt.hasNext()) { StringBuilder sb = new StringBuilder(); Sentence sentence = (Sentence) sentenceIt.next(); Iterator<Token> tokens = JCoReAnnotationTools.getIncludedAnnotations(aJCas, sentence, Token.class).iterator(); while (tokens.hasNext()) { Token token = (Token) tokens.next(); sb.append(token.getCoveredText()); sb.append("|"); for (Map.Entry<Class<? extends Annotation>, JCoReFeaturePath> entry : metaDataFeaturePathMap.entrySet()) { Class<? extends Annotation> metaDataType = entry.getKey(); JCoReFeaturePath featurePath = entry.getValue(); Annotation metaDataAnnotation = JCoReAnnotationTools.getAnnotationAtMatchingOffsets(aJCas, token, metaDataType); String metaData = "<N/A>"; if (null != metaDataAnnotation) { metaData = featurePath.getValueAsString(metaDataAnnotation); } else { log.warn("MetaData annotation for type \"{}\" for token {} does not exist.", metaDataType.getCanonicalName(), token); } sb.append(metaData); sb.append("|"); } List<Class<? extends Annotation>> labelAnnotations = new ArrayList<>(1); for (Class<? extends Annotation> labelType : typeToLabelMap.keySet()) { Annotation includingAnnotation = JCoReAnnotationTools.getIncludingAnnotation(aJCas, token, labelType); if (null != includingAnnotation) { labelAnnotations.add(labelType); String label = typeToLabelMap.get(labelType); sb.append(label); } } for (Class<? extends Annotation> labelType : typeToFeaturePathMap.keySet()) { Annotation includingAnnotation = JCoReAnnotationTools.getIncludingAnnotation(aJCas, token, labelType); if (null != includingAnnotation) { labelAnnotations.add(labelType); JCoReFeaturePath featurePath = typeToFeaturePathMap.get(labelType); String label = featurePath.getValueAsString(includingAnnotation); sb.append(label); } } if (labelAnnotations.size() > 1) throw new IllegalStateException("Multiple label types for token " + token + " have been found. However, it can be at most one. Found label types are:" + StringUtils.join(labelAnnotations, ", ")); else if (labelAnnotations.size() == 0) sb.append(outsideLabel); sb.append(" "); } sb.deleteCharAt(sb.length() - 1); ppdSentences.add(sb.toString()); } }  @Override void initialize(UimaContext aContext); @Override void process(JCas aJCas); @Override void batchProcessComplete(); @Override void collectionProcessComplete(); static final String PARAM_TYPE_LABEL_MAPPINGS; static final String PARAM_META_DATA_TYPE_MAPPINGS; static final String PARAM_OUTSIDE_LABEL; static final String PARAM_OUTPUT_FILE; }
@Test public void testProcess() throws Exception { File correctOutput = new File("src/test/resources/testoutput-correct.ppd"); String outputFileString = "src/test/resources/testoutput.ppd"; File outputFile = new File(outputFileString); if (outputFile.exists()) outputFile.delete(); AnalysisEngine ppdConsumer = AnalysisEngineFactory.createEngine(PPDWriter.class, PPDWriter.PARAM_TYPE_LABEL_MAPPINGS, new String[] { "de.julielab.jcore.types.Gene=GENE" }, PPDWriter.PARAM_META_DATA_TYPE_MAPPINGS, new String[] { "de.julielab.jcore.types.PennBioIEPOSTag=/value" }, PPDWriter.PARAM_OUTSIDE_LABEL, "O", PPDWriter.PARAM_OUTPUT_FILE, outputFileString); JCas jcas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); XmiCasDeserializer.deserialize(new FileInputStream("src/test/resources/10438902.xmi"), jcas.getCas()); ppdConsumer.process(jcas.getCas()); ppdConsumer.collectionProcessComplete(); assertTrue(outputFile.exists()); assertEquals(FileUtils.readFileToString(correctOutput, "UTF-8"), FileUtils.readFileToString(outputFile, "UTF-8")); outputFile.delete(); }