Skip to content

Example Collection Reader

Sean Finan edited this page Dec 19, 2025 · 1 revision

Example Collection Reader

// Extend AbstractFileTreeReader, which does a lot of work for you.
class ExampleReader extends AbstractFileTreeReader {
   
   // Only one method needs to be implemented.
   protected void readFile( JCas jCas, File file ) throws IOException {
      
      // Read the file, building a document using only lines preceded by "Text:".
      String documentText;
      try ( Stream<String> textStream = Files.lines( file.toPath() ) ) {
         documentText = textStream.filter( line -> line.startsWith( "Text:" ) )
                                  .map( line -> line.substring( 5 ).trim() )
                                  .collect( Collectors.joining( "\n" ) );
      }
      
      // JCasBuilder can attempt to create document metadata based upon the file.
      getJCasBuilder( file ).setDocText( documentText )
                            .populate( jCas );
   }
}

Clone this wiki locally