Author: seanfinan
Date: Wed Oct 12 19:52:03 2016
New Revision: 1764530

URL: http://svn.apache.org/viewvc?rev=1764530&view=rev
Log:
Example pipeline runner refactoring

Added:
    
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/
    
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldAggregatePipeline.java
    
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldBuildPipeRunner.java
    
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldReadPipeRunner.java

Added: 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldAggregatePipeline.java
URL: 
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldAggregatePipeline.java?rev=1764530&view=auto
==============================================================================
--- 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldAggregatePipeline.java
 (added)
+++ 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldAggregatePipeline.java
 Wed Oct 12 19:52:03 2016
@@ -0,0 +1,58 @@
+package org.apache.ctakes.examples.pipeline;
+
+import org.apache.ctakes.clinicalpipeline.ClinicalPipelineFactory;
+import org.apache.ctakes.examples.ae.ExampleHelloWorldAnnotator;
+import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
+import org.apache.uima.fit.factory.AggregateBuilder;
+import org.apache.uima.fit.factory.JCasFactory;
+import org.apache.uima.fit.pipeline.SimplePipeline;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.JCas;
+
+import java.io.FileWriter;
+
+/**
+ * Build and run a pipeline using uimafit {@link AggregateBuilder}, {@link 
SimplePipeline} and {@link JCasUtil}
+ * <p>
+ * Example of a running a pipeline programatically w/o uima xml descriptor xml 
files
+ * Adds the default Tokenization pipeline and adding the Example HelloWorld 
Annotator
+ */
+
+public class HelloWorldAggregatePipeline {
+
+
+   public static void main( String[] args ) throws Exception {
+
+      String note = "Hello World!";
+      JCas jcas = JCasFactory.createJCas();
+      jcas.setDocumentText( note );
+
+      AggregateBuilder builder = new AggregateBuilder();
+      // Get a simple pre-defined existing pipeline for Tokenization
+
+      // You can consider using ClinicalPipelineFactory.getDefaultPipeline();
+      // Which will include the UMLS Lookup, but you'll need to set your
+      // -Dctakes.umlsuser and -Dctatkes.umlspw in your environment
+      builder.add( ClinicalPipelineFactory.getTokenProcessingPipeline() );
+
+      //Add the new HelloWorld Example:
+      builder.add( ExampleHelloWorldAnnotator.createAnnotatorDescription() );
+
+      //Run the Aggregate Pipeline
+      SimplePipeline.runPipeline( jcas, builder.createAggregateDescription() );
+
+      //Print out the IdentifiedAnnotation objects
+      for ( IdentifiedAnnotation entity : JCasUtil.select( jcas, 
IdentifiedAnnotation.class ) ) {
+         System.out.println( "Entity: " + entity.getCoveredText() + " === 
Polarity: " + entity.getPolarity() );
+      }
+
+      //Example to save the Aggregate descriptor to an xml file for external
+      //use such as the UIMA CVD/CPE
+      if ( args.length > 0 ) {
+         builder.createAggregateDescription().toXML( new FileWriter( args[ 0 ] 
) );
+      }
+   }
+
+}
+
+

Added: 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldBuildPipeRunner.java
URL: 
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldBuildPipeRunner.java?rev=1764530&view=auto
==============================================================================
--- 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldBuildPipeRunner.java
 (added)
+++ 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldBuildPipeRunner.java
 Wed Oct 12 19:52:03 2016
@@ -0,0 +1,59 @@
+package org.apache.ctakes.examples.pipeline;
+
+
+import org.apache.ctakes.clinicalpipeline.ClinicalPipelineFactory;
+import org.apache.ctakes.core.pipeline.EntityCollector;
+import org.apache.ctakes.core.pipeline.PipelineBuilder;
+import org.apache.ctakes.examples.ae.ExampleHelloWorldAnnotator;
+import org.apache.log4j.Logger;
+import org.apache.uima.UIMAException;
+
+import java.io.IOException;
+
+/**
+ * Build and run a pipeline using a {@link PipelineBuilder}.
+ * <p>
+ * Example of a running a pipeline programatically w/o uima xml descriptor xml 
files
+ * Adds the default Tokenization pipeline and adding the Example HelloWorld 
Annotator
+ *
+ * @author SPF , chip-nlp
+ * @version %I%
+ * @since 10/10/2016
+ */
+final public class HelloWorldBuildPipeRunner {
+
+   static private final Logger LOGGER = Logger.getLogger( 
"HelloWorldBuildPipeRunner" );
+
+   static private final String DOC_TEXT = "Hello World!";
+
+   private HelloWorldBuildPipeRunner() {
+   }
+
+   /**
+    * @param args an output directory for xmi files or none if xmi files are 
not wanted
+    */
+   public static void main( final String... args ) {
+      try {
+         PipelineBuilder builder = new PipelineBuilder();
+         builder
+               // Add a simple pre-defined existing pipeline for Tokenization. 
 Could also add engines individually
+               .addDescription( 
ClinicalPipelineFactory.getTokenProcessingPipeline() )
+               // add the simple Hello World Annotator
+               .add( ExampleHelloWorldAnnotator.class )
+               // Collect discovered entities for post-run information
+               .collectEntities();
+         if ( args.length > 0 ) {
+            // Example to save the Aggregate descriptor to an xml file for 
external use such as the UIMA CVD/CPE
+            builder.writeXMIs( args[ 0 ] );
+         }
+         // Run the pipeline with specified text
+         builder.run( DOC_TEXT );
+      } catch ( IOException | UIMAException multE ) {
+         LOGGER.error( multE.getMessage() );
+      }
+      //Print out the IdentifiedAnnotation objects
+      LOGGER.info( "\n" + EntityCollector.getInstance().toString() );
+   }
+
+
+}

Added: 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldReadPipeRunner.java
URL: 
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldReadPipeRunner.java?rev=1764530&view=auto
==============================================================================
--- 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldReadPipeRunner.java
 (added)
+++ 
ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipeline/HelloWorldReadPipeRunner.java
 Wed Oct 12 19:52:03 2016
@@ -0,0 +1,55 @@
+package org.apache.ctakes.examples.pipeline;
+
+
+import org.apache.ctakes.core.pipeline.EntityCollector;
+import org.apache.ctakes.core.pipeline.PipelineBuilder;
+import org.apache.ctakes.core.pipeline.PipelineReader;
+import org.apache.log4j.Logger;
+import org.apache.uima.UIMAException;
+
+import java.io.IOException;
+
+/**
+ * Build and run a pipeline using a {@link PipelineReader} and a {@link 
PipelineBuilder}.
+ * <p>
+ * Example of a running a pipeline programatically w/o uima xml descriptor xml 
files
+ * Adds the default Tokenization pipeline and adding the Example HelloWorld 
Annotator
+ *
+ * @author SPF , chip-nlp
+ * @version %I%
+ * @since 10/10/2016
+ */
+final public class HelloWorldReadPipeRunner {
+
+   static private final Logger LOGGER = Logger.getLogger( 
"HelloWorldReadPipeRunner" );
+
+   static private final String PIPELINE_FILE_PATH = 
"org/apache/ctakes/examples/pipeline/HelloWorldPipeline.txt";
+
+   static private final String DOC_TEXT = "Hello World!";
+
+   private HelloWorldReadPipeRunner() {
+   }
+
+   /**
+    * @param args an output directory for xmi files or none if xmi files are 
not wanted
+    */
+   public static void main( final String... args ) {
+      try {
+         // Add a simple pre-defined existing pipeline for Tokenization from 
file
+         final PipelineReader reader = new PipelineReader( PIPELINE_FILE_PATH 
);
+         PipelineBuilder builder = reader.getBuilder();
+         if ( args.length > 0 ) {
+            // Example to save the Aggregate descriptor to an xml file for 
external use such as the UIMA CVD/CPE
+            builder.writeXMIs( args[ 0 ] );
+         }
+         // Run the pipeline with specified text
+         builder.run( DOC_TEXT );
+      } catch ( IOException | UIMAException multE ) {
+         LOGGER.error( multE.getMessage() );
+      }
+      // Log the IdentifiedAnnotation objects
+      LOGGER.info( "\n" + EntityCollector.getInstance().toString() );
+   }
+
+
+}


Reply via email to