@Test public void testWriteIOB() throws Exception { final JCas jCas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-morpho-syntax-types", "de.julielab.jcore.types.jcore-semantics-biology-types" , "de.julielab.jcore.types.jcore-document-meta-types"); jCas.setDocumentText("BRCA influences cancer."); new Sentence(jCas, 0, jCas.getDocumentText().length()).addToIndexes(); new Gene(jCas, 0, 4).addToIndexes(); new Token(jCas, 0, 4).addToIndexes(); new Token(jCas, 5, 15).addToIndexes(); new Token(jCas, 16, 22).addToIndexes(); new Token(jCas, 22, 23).addToIndexes(); final String outputDir = "src/test/resources/iob-output"; final AnalysisEngine iobwriter = AnalysisEngineFactory.createEngine("de.julielab.jcore.consumer.cas2iob.desc.jcore-iob-consumer", ToIOBConsumer.PARAM_OUTFOLDER, outputDir, ToIOBConsumer.PARAM_LABEL_METHODS, new String[]{"Gene"}, ToIOBConsumer.PARAM_IOB_LABEL_NAMES, new String[]{"de.julielab.jcore.types.Gene=Gene"}, ToIOBConsumer.PARAM_TYPE_PATH, "de.julielab.jcore.types."); iobwriter.process(jCas); final File file = Path.of(outputDir, "1.iob").toFile(); assertThat(file.exists()); assertThat(IOUtils.readLines(new FileInputStream(file), "UTF-8")).containsExactly("BRCA B_Gene", "influences O", "cancer O", ". O"); }
public void process(JCas jCas) { LOGGER.trace("Converting CAS to IO(B)Tokens..."); IOToken[] ioTokens = convertToIOB(jCas); LOGGER.trace("Writing IO(B) file..."); BufferedWriter bw; String outPathName = Paths.get(outFolder, getDocumentId(jCas)).toString() + ".iob"; if (Files.notExists(Paths.get(outFolder))) { (new File(outFolder)).mkdirs(); } try { bw = new BufferedWriter(new FileWriter(outPathName)); for (IOToken token : ioTokens) { if (token.getText().equals("") || token.getText().equals(SENTENCE_END_MARK)) { bw.newLine(); } else if (token.getText().equals("") || token.getText().equals(PARAGRAPH_END_MARK)) { bw.newLine(); bw.newLine(); } else { final Stream.Builder<String> sb = Stream.builder(); sb.accept(token.getText()); sb.accept(token.getPos()); sb.accept(token.getIobMark().equals("O") ? token.getIobMark() : token.getIobMark() + iobMarkSeparator + token.getLabel()); String line = sb.build().filter(Objects::nonNull).collect(Collectors.joining(separator)); bw.write(line); bw.newLine(); } } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } LOGGER.trace("The IO(B) file was written to " + outPathName); }
ToIOBConsumer extends JCasAnnotator_ImplBase { public void process(JCas jCas) { LOGGER.trace("Converting CAS to IO(B)Tokens..."); IOToken[] ioTokens = convertToIOB(jCas); LOGGER.trace("Writing IO(B) file..."); BufferedWriter bw; String outPathName = Paths.get(outFolder, getDocumentId(jCas)).toString() + ".iob"; if (Files.notExists(Paths.get(outFolder))) { (new File(outFolder)).mkdirs(); } try { bw = new BufferedWriter(new FileWriter(outPathName)); for (IOToken token : ioTokens) { if (token.getText().equals("") || token.getText().equals(SENTENCE_END_MARK)) { bw.newLine(); } else if (token.getText().equals("") || token.getText().equals(PARAGRAPH_END_MARK)) { bw.newLine(); bw.newLine(); } else { final Stream.Builder<String> sb = Stream.builder(); sb.accept(token.getText()); sb.accept(token.getPos()); sb.accept(token.getIobMark().equals("O") ? token.getIobMark() : token.getIobMark() + iobMarkSeparator + token.getLabel()); String line = sb.build().filter(Objects::nonNull).collect(Collectors.joining(separator)); bw.write(line); bw.newLine(); } } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } LOGGER.trace("The IO(B) file was written to " + outPathName); } }
ToIOBConsumer extends JCasAnnotator_ImplBase { public void process(JCas jCas) { LOGGER.trace("Converting CAS to IO(B)Tokens..."); IOToken[] ioTokens = convertToIOB(jCas); LOGGER.trace("Writing IO(B) file..."); BufferedWriter bw; String outPathName = Paths.get(outFolder, getDocumentId(jCas)).toString() + ".iob"; if (Files.notExists(Paths.get(outFolder))) { (new File(outFolder)).mkdirs(); } try { bw = new BufferedWriter(new FileWriter(outPathName)); for (IOToken token : ioTokens) { if (token.getText().equals("") || token.getText().equals(SENTENCE_END_MARK)) { bw.newLine(); } else if (token.getText().equals("") || token.getText().equals(PARAGRAPH_END_MARK)) { bw.newLine(); bw.newLine(); } else { final Stream.Builder<String> sb = Stream.builder(); sb.accept(token.getText()); sb.accept(token.getPos()); sb.accept(token.getIobMark().equals("O") ? token.getIobMark() : token.getIobMark() + iobMarkSeparator + token.getLabel()); String line = sb.build().filter(Objects::nonNull).collect(Collectors.joining(separator)); bw.write(line); bw.newLine(); } } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } LOGGER.trace("The IO(B) file was written to " + outPathName); }  }
ToIOBConsumer extends JCasAnnotator_ImplBase { public void process(JCas jCas) { LOGGER.trace("Converting CAS to IO(B)Tokens..."); IOToken[] ioTokens = convertToIOB(jCas); LOGGER.trace("Writing IO(B) file..."); BufferedWriter bw; String outPathName = Paths.get(outFolder, getDocumentId(jCas)).toString() + ".iob"; if (Files.notExists(Paths.get(outFolder))) { (new File(outFolder)).mkdirs(); } try { bw = new BufferedWriter(new FileWriter(outPathName)); for (IOToken token : ioTokens) { if (token.getText().equals("") || token.getText().equals(SENTENCE_END_MARK)) { bw.newLine(); } else if (token.getText().equals("") || token.getText().equals(PARAGRAPH_END_MARK)) { bw.newLine(); bw.newLine(); } else { final Stream.Builder<String> sb = Stream.builder(); sb.accept(token.getText()); sb.accept(token.getPos()); sb.accept(token.getIobMark().equals("O") ? token.getIobMark() : token.getIobMark() + iobMarkSeparator + token.getLabel()); String line = sb.build().filter(Objects::nonNull).collect(Collectors.joining(separator)); bw.write(line); bw.newLine(); } } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } LOGGER.trace("The IO(B) file was written to " + outPathName); }  void initialize(UimaContext aContext); void process(JCas jCas); IOToken[] convertToIOB(JCas jcas); }
ToIOBConsumer extends JCasAnnotator_ImplBase { public void process(JCas jCas) { LOGGER.trace("Converting CAS to IO(B)Tokens..."); IOToken[] ioTokens = convertToIOB(jCas); LOGGER.trace("Writing IO(B) file..."); BufferedWriter bw; String outPathName = Paths.get(outFolder, getDocumentId(jCas)).toString() + ".iob"; if (Files.notExists(Paths.get(outFolder))) { (new File(outFolder)).mkdirs(); } try { bw = new BufferedWriter(new FileWriter(outPathName)); for (IOToken token : ioTokens) { if (token.getText().equals("") || token.getText().equals(SENTENCE_END_MARK)) { bw.newLine(); } else if (token.getText().equals("") || token.getText().equals(PARAGRAPH_END_MARK)) { bw.newLine(); bw.newLine(); } else { final Stream.Builder<String> sb = Stream.builder(); sb.accept(token.getText()); sb.accept(token.getPos()); sb.accept(token.getIobMark().equals("O") ? token.getIobMark() : token.getIobMark() + iobMarkSeparator + token.getLabel()); String line = sb.build().filter(Objects::nonNull).collect(Collectors.joining(separator)); bw.write(line); bw.newLine(); } } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } LOGGER.trace("The IO(B) file was written to " + outPathName); }  void initialize(UimaContext aContext); void process(JCas jCas); IOToken[] convertToIOB(JCas jcas); static final String PARAM_LABELS; static final String PARAM_OUTFOLDER; static final String PARAM_LABEL_METHODS; static final String PARAM_IOB_LABEL_NAMES; static final String PARAM_TYPE_PATH; static final String PARAM_MODE; static final String PARAM_ADD_POS; static final String PARAM_COLUMN_SEPARATOR; static final String PARAM_IOB_MARK_SEPARATOR; }