ParagraphSplitter extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { String input = aJCas.getDocumentText(); if (input.length() < 1) { throw new AnalysisEngineProcessException(new Throwable("Document text is empty.")); } Pattern ParagraphPattern = splitPattern; Matcher matcher = ParagraphPattern.matcher(input); int pos = 0; int nextBeginning = 0; while (matcher.find(pos)) { Paragraph paragraph = new Paragraph(aJCas, nextBeginning, matcher.start()); paragraph.addToIndexes(); nextBeginning = matcher.end(); pos = matcher.end(); } if (pos < input.length()) { Paragraph paragraph = new Paragraph(aJCas, nextBeginning, input.length()); paragraph.addToIndexes(); } }  @Override void process(JCas aJCas); static final String SINGLE_LINE_BREAKS_PATTERN; static final String DOUBLE_LINE_BREAKS_PATTERN; static final String PARAM_SPLIT_PATTERN; }
@Test public void paragraphSplitterTest_SingleLineBreaks() throws Exception { StringBuilder sb = new StringBuilder(); sb.append("paragraph1"); sb.append(System.getProperty("line.separator")); sb.append(System.getProperty("line.separator")); sb.append("paragraph2"); sb.append(System.getProperty("line.separator")); sb.append("paragraph3"); AnalysisEngine ae = createEngine(ParagraphSplitter.class, ParagraphSplitter.PARAM_SPLIT_PATTERN, ParagraphSplitter.SINGLE_LINE_BREAKS_PATTERN); JCas jcas = ae.newJCas(); jcas.setDocumentLanguage("en"); jcas.setDocumentText(sb.toString()); ae.process(jcas); int i = 0; for (Paragraph paragraph : select(jcas, Paragraph.class)) { if (i == 0) { assertEquals("paragraph1", paragraph.getCoveredText()); } else if (i == 1) { assertEquals("paragraph2", paragraph.getCoveredText()); } else if (i == 2) { assertEquals("paragraph3", paragraph.getCoveredText()); } else { fail("too many paragraphs"); } i++; } }
@Test public void paragraphSplitterTest_DoubleLineBreaks() throws Exception { StringBuilder sb = new StringBuilder(); sb.append("paragraph1"); sb.append(System.getProperty("line.separator")); sb.append(System.getProperty("line.separator")); sb.append("paragraph2"); AnalysisEngine ae = createEngine(ParagraphSplitter.class); JCas jcas = ae.newJCas(); jcas.setDocumentLanguage("en"); jcas.setDocumentText(sb.toString()); ae.process(jcas); int i = 0; for (Paragraph paragraph : select(jcas, Paragraph.class)) { if (i == 0) { assertEquals("paragraph1", paragraph.getCoveredText()); } else if (i == 1) { assertEquals("paragraph2", paragraph.getCoveredText()); } else { fail("too many paragraphs"); } i++; } }