@Test public void test() throws Exception { String namespace = this.getClass().getPackage().getName().replaceAll("\\.", "/"); String name = namespace + "/" + "PlainTextAnnotatorTest.txt"; URL textURL = PlainTextAnnotatorTest.class.getClassLoader().getResource(name); File textFile = new File(textURL.toURI()); String text = FileUtils.file2String(textFile, "UTF-8"); URL url = PlainTextAnnotator.class.getClassLoader().getResource("PlainTextAnnotator.xml"); if (url == null) { url = HtmlAnnotator.class.getClassLoader().getResource( "org/apache/uima/ruta/engine/PlainTextAnnotator.xml"); } XMLInputSource in = new XMLInputSource(url); ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in); AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier); CAS cas = ae.newCAS(); AnnotationIndex<AnnotationFS> ai = null; cas.setDocumentText(text); ae.process(cas); ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("org.apache.uima.ruta.type.AnyLine")); assertEquals(18, ai.size()); ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("org.apache.uima.ruta.type.Line")); assertEquals(10, ai.size()); ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("org.apache.uima.ruta.type.EmptyLine")); assertEquals(8, ai.size()); ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("org.apache.uima.ruta.type.WSLine")); assertEquals(5, ai.size()); ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("org.apache.uima.ruta.type.Paragraph")); assertEquals(4, ai.size()); ae.destroy(); cas.release(); }
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { CAS cas = jcas.getCas(); String documentText = cas.getDocumentText(); BufferedReader br = new BufferedReader(new StringReader(documentText)); Type lineType = cas.getTypeSystem().getType(TYPE_LINE); Type wsLineType = cas.getTypeSystem().getType(TYPE_WSLINE); Type emptyLineType = cas.getTypeSystem().getType(TYPE_EMPTYLINE); Type paragraphType = cas.getTypeSystem().getType(TYPE_PARAGRAPH); int offsetTillNow = 0; int paragraphBegin = -1; int lastLineEnd = 0; boolean lastWasEmpty = true; String eachLine = null; try { while ((eachLine = br.readLine()) != null) { boolean wsLine = StringUtils.isBlank(eachLine); if (!wsLine && StringUtils.isBlank(eachLine.trim().replaceAll( "\u00A0|\u202F|\uFEFF|\u2007|\u180E", ""))) { wsLine = true; } boolean emptyLine = StringUtils.isEmpty(eachLine); int offsetAfterLine = offsetTillNow + eachLine.length(); int nlLength = 1; if (documentText.length() >= offsetAfterLine + 2) { String substring = documentText.substring(offsetAfterLine, offsetAfterLine + 2); if (substring.equals("\r\n")) { nlLength = 2; } } if (lastWasEmpty && !wsLine) { paragraphBegin = offsetTillNow; } if (wsLine && emptyLine) { AnnotationFS newEmptyLineFS = cas.createAnnotation(emptyLineType, offsetTillNow, offsetTillNow + nlLength); cas.addFsToIndexes(newEmptyLineFS); } else if (wsLine && !emptyLine) { AnnotationFS newWSLineFS = cas.createAnnotation(wsLineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newWSLineFS); } else if (!emptyLine) { AnnotationFS newLineFS = cas.createAnnotation(lineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newLineFS); lastWasEmpty = false; lastLineEnd = offsetTillNow + eachLine.length(); } if (wsLine && !lastWasEmpty && lastLineEnd != 0) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, lastLineEnd); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine + nlLength == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } if (wsLine) { lastWasEmpty = true; } offsetTillNow = offsetTillNow + eachLine.length() + nlLength; } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }
PlainTextAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { CAS cas = jcas.getCas(); String documentText = cas.getDocumentText(); BufferedReader br = new BufferedReader(new StringReader(documentText)); Type lineType = cas.getTypeSystem().getType(TYPE_LINE); Type wsLineType = cas.getTypeSystem().getType(TYPE_WSLINE); Type emptyLineType = cas.getTypeSystem().getType(TYPE_EMPTYLINE); Type paragraphType = cas.getTypeSystem().getType(TYPE_PARAGRAPH); int offsetTillNow = 0; int paragraphBegin = -1; int lastLineEnd = 0; boolean lastWasEmpty = true; String eachLine = null; try { while ((eachLine = br.readLine()) != null) { boolean wsLine = StringUtils.isBlank(eachLine); if (!wsLine && StringUtils.isBlank(eachLine.trim().replaceAll( "\u00A0|\u202F|\uFEFF|\u2007|\u180E", ""))) { wsLine = true; } boolean emptyLine = StringUtils.isEmpty(eachLine); int offsetAfterLine = offsetTillNow + eachLine.length(); int nlLength = 1; if (documentText.length() >= offsetAfterLine + 2) { String substring = documentText.substring(offsetAfterLine, offsetAfterLine + 2); if (substring.equals("\r\n")) { nlLength = 2; } } if (lastWasEmpty && !wsLine) { paragraphBegin = offsetTillNow; } if (wsLine && emptyLine) { AnnotationFS newEmptyLineFS = cas.createAnnotation(emptyLineType, offsetTillNow, offsetTillNow + nlLength); cas.addFsToIndexes(newEmptyLineFS); } else if (wsLine && !emptyLine) { AnnotationFS newWSLineFS = cas.createAnnotation(wsLineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newWSLineFS); } else if (!emptyLine) { AnnotationFS newLineFS = cas.createAnnotation(lineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newLineFS); lastWasEmpty = false; lastLineEnd = offsetTillNow + eachLine.length(); } if (wsLine && !lastWasEmpty && lastLineEnd != 0) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, lastLineEnd); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine + nlLength == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } if (wsLine) { lastWasEmpty = true; } offsetTillNow = offsetTillNow + eachLine.length() + nlLength; } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } } }
PlainTextAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { CAS cas = jcas.getCas(); String documentText = cas.getDocumentText(); BufferedReader br = new BufferedReader(new StringReader(documentText)); Type lineType = cas.getTypeSystem().getType(TYPE_LINE); Type wsLineType = cas.getTypeSystem().getType(TYPE_WSLINE); Type emptyLineType = cas.getTypeSystem().getType(TYPE_EMPTYLINE); Type paragraphType = cas.getTypeSystem().getType(TYPE_PARAGRAPH); int offsetTillNow = 0; int paragraphBegin = -1; int lastLineEnd = 0; boolean lastWasEmpty = true; String eachLine = null; try { while ((eachLine = br.readLine()) != null) { boolean wsLine = StringUtils.isBlank(eachLine); if (!wsLine && StringUtils.isBlank(eachLine.trim().replaceAll( "\u00A0|\u202F|\uFEFF|\u2007|\u180E", ""))) { wsLine = true; } boolean emptyLine = StringUtils.isEmpty(eachLine); int offsetAfterLine = offsetTillNow + eachLine.length(); int nlLength = 1; if (documentText.length() >= offsetAfterLine + 2) { String substring = documentText.substring(offsetAfterLine, offsetAfterLine + 2); if (substring.equals("\r\n")) { nlLength = 2; } } if (lastWasEmpty && !wsLine) { paragraphBegin = offsetTillNow; } if (wsLine && emptyLine) { AnnotationFS newEmptyLineFS = cas.createAnnotation(emptyLineType, offsetTillNow, offsetTillNow + nlLength); cas.addFsToIndexes(newEmptyLineFS); } else if (wsLine && !emptyLine) { AnnotationFS newWSLineFS = cas.createAnnotation(wsLineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newWSLineFS); } else if (!emptyLine) { AnnotationFS newLineFS = cas.createAnnotation(lineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newLineFS); lastWasEmpty = false; lastLineEnd = offsetTillNow + eachLine.length(); } if (wsLine && !lastWasEmpty && lastLineEnd != 0) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, lastLineEnd); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine + nlLength == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } if (wsLine) { lastWasEmpty = true; } offsetTillNow = offsetTillNow + eachLine.length() + nlLength; } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }  }
PlainTextAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { CAS cas = jcas.getCas(); String documentText = cas.getDocumentText(); BufferedReader br = new BufferedReader(new StringReader(documentText)); Type lineType = cas.getTypeSystem().getType(TYPE_LINE); Type wsLineType = cas.getTypeSystem().getType(TYPE_WSLINE); Type emptyLineType = cas.getTypeSystem().getType(TYPE_EMPTYLINE); Type paragraphType = cas.getTypeSystem().getType(TYPE_PARAGRAPH); int offsetTillNow = 0; int paragraphBegin = -1; int lastLineEnd = 0; boolean lastWasEmpty = true; String eachLine = null; try { while ((eachLine = br.readLine()) != null) { boolean wsLine = StringUtils.isBlank(eachLine); if (!wsLine && StringUtils.isBlank(eachLine.trim().replaceAll( "\u00A0|\u202F|\uFEFF|\u2007|\u180E", ""))) { wsLine = true; } boolean emptyLine = StringUtils.isEmpty(eachLine); int offsetAfterLine = offsetTillNow + eachLine.length(); int nlLength = 1; if (documentText.length() >= offsetAfterLine + 2) { String substring = documentText.substring(offsetAfterLine, offsetAfterLine + 2); if (substring.equals("\r\n")) { nlLength = 2; } } if (lastWasEmpty && !wsLine) { paragraphBegin = offsetTillNow; } if (wsLine && emptyLine) { AnnotationFS newEmptyLineFS = cas.createAnnotation(emptyLineType, offsetTillNow, offsetTillNow + nlLength); cas.addFsToIndexes(newEmptyLineFS); } else if (wsLine && !emptyLine) { AnnotationFS newWSLineFS = cas.createAnnotation(wsLineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newWSLineFS); } else if (!emptyLine) { AnnotationFS newLineFS = cas.createAnnotation(lineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newLineFS); lastWasEmpty = false; lastLineEnd = offsetTillNow + eachLine.length(); } if (wsLine && !lastWasEmpty && lastLineEnd != 0) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, lastLineEnd); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine + nlLength == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } if (wsLine) { lastWasEmpty = true; } offsetTillNow = offsetTillNow + eachLine.length() + nlLength; } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }  @Override void process(JCas jcas); }
PlainTextAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { CAS cas = jcas.getCas(); String documentText = cas.getDocumentText(); BufferedReader br = new BufferedReader(new StringReader(documentText)); Type lineType = cas.getTypeSystem().getType(TYPE_LINE); Type wsLineType = cas.getTypeSystem().getType(TYPE_WSLINE); Type emptyLineType = cas.getTypeSystem().getType(TYPE_EMPTYLINE); Type paragraphType = cas.getTypeSystem().getType(TYPE_PARAGRAPH); int offsetTillNow = 0; int paragraphBegin = -1; int lastLineEnd = 0; boolean lastWasEmpty = true; String eachLine = null; try { while ((eachLine = br.readLine()) != null) { boolean wsLine = StringUtils.isBlank(eachLine); if (!wsLine && StringUtils.isBlank(eachLine.trim().replaceAll( "\u00A0|\u202F|\uFEFF|\u2007|\u180E", ""))) { wsLine = true; } boolean emptyLine = StringUtils.isEmpty(eachLine); int offsetAfterLine = offsetTillNow + eachLine.length(); int nlLength = 1; if (documentText.length() >= offsetAfterLine + 2) { String substring = documentText.substring(offsetAfterLine, offsetAfterLine + 2); if (substring.equals("\r\n")) { nlLength = 2; } } if (lastWasEmpty && !wsLine) { paragraphBegin = offsetTillNow; } if (wsLine && emptyLine) { AnnotationFS newEmptyLineFS = cas.createAnnotation(emptyLineType, offsetTillNow, offsetTillNow + nlLength); cas.addFsToIndexes(newEmptyLineFS); } else if (wsLine && !emptyLine) { AnnotationFS newWSLineFS = cas.createAnnotation(wsLineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newWSLineFS); } else if (!emptyLine) { AnnotationFS newLineFS = cas.createAnnotation(lineType, offsetTillNow, offsetTillNow + eachLine.length()); cas.addFsToIndexes(newLineFS); lastWasEmpty = false; lastLineEnd = offsetTillNow + eachLine.length(); } if (wsLine && !lastWasEmpty && lastLineEnd != 0) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, lastLineEnd); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine + nlLength == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } else if (offsetAfterLine == documentText.length()) { AnnotationFS newParaFS = cas.createAnnotation(paragraphType, paragraphBegin, offsetAfterLine); cas.addFsToIndexes(newParaFS); } if (wsLine) { lastWasEmpty = true; } offsetTillNow = offsetTillNow + eachLine.length() + nlLength; } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }  @Override void process(JCas jcas); static final String TYPE_LINE; static final String TYPE_WSLINE; static final String TYPE_EMPTYLINE; static final String TYPE_PARAGRAPH; }