ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chen...@apache.org
Subject svn commit: r1606116 - in /ctakes/trunk/ctakes-examples/src/main/java/org: ./ apache/ apache/ctakes/ apache/ctakes/examples/ apache/ctakes/examples/ae/ apache/ctakes/examples/pipelines/
Date Fri, 27 Jun 2014 15:05:23 GMT
Author: chenpei
Date: Fri Jun 27 15:05:23 2014
New Revision: 1606116

URL: http://svn.apache.org/r1606116
Log:
CTAKES-223 - ctakes-examples
Checking in an Example Anntotator that illustrates how to create a new Annotator and now to
add it to the pipeline and run.

Added:
    ctakes/trunk/ctakes-examples/src/main/java/org/
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/ExampleHelloWorldAnnotator.java
  (with props)
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/
    ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/ExampleAggregatePipeline.java
  (with props)

Added: ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/ExampleHelloWorldAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/ExampleHelloWorldAnnotator.java?rev=1606116&view=auto
==============================================================================
--- ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/ExampleHelloWorldAnnotator.java
(added)
+++ ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/ExampleHelloWorldAnnotator.java
Fri Jun 27 15:05:23 2014
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ctakes.examples.ae;
+
+import java.util.Collection;
+
+import org.apache.ctakes.typesystem.type.syntax.BaseToken;
+import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
+import org.apache.log4j.Logger;
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.uimafit.component.JCasAnnotator_ImplBase;
+import org.uimafit.descriptor.ConfigurationParameter;
+import org.uimafit.factory.AnalysisEngineFactory;
+import org.uimafit.util.JCasUtil;
+
+public class ExampleHelloWorldAnnotator extends JCasAnnotator_ImplBase {
+
+	public static final String PARAM_SAVE_ANN = "PARAM_SAVE_ANN";
+	public static final String PARAM_PRINT_ANN = "PARAM_PRINT_ANN";
+	private Logger LOG = Logger.getLogger(getClass().getName());
+
+	@ConfigurationParameter(name = PARAM_SAVE_ANN, mandatory = false, description = "Example
of Options/Parameters Save Annotation?")
+	protected boolean saveAnnotation = true;
+
+	@ConfigurationParameter(name = PARAM_PRINT_ANN, mandatory = false, description = "Example
of Options/Parameters Print Annotation?")
+	protected boolean printAnnotation = true;
+
+	@Override
+	public void process(JCas jcas) throws AnalysisEngineProcessException {
+		// Create a dummy IdentifiedAnnotation in the type system
+		// If the BaseToken Part Of Speech is a Noun
+		Collection<BaseToken> tokens = JCasUtil.select(jcas, BaseToken.class);
+		for (BaseToken toke : tokens) {
+			if (saveAnnotation && toke.getPartOfSpeech() != null
+					&& toke.getPartOfSpeech().startsWith("N")) {
+				IdentifiedAnnotation ann = new IdentifiedAnnotation(jcas);
+				ann.setBegin(toke.getBegin());
+				ann.setEnd(toke.getEnd());
+				ann.addToIndexes();
+
+				if (printAnnotation) {
+					LOG.info("Token:" + toke.getCoveredText() + " POS:"
+							+ toke.getPartOfSpeech());
+				}
+			}
+		}
+	}
+
+	@Override
+	public void initialize(UimaContext context)
+			throws ResourceInitializationException {
+		super.initialize(context);
+	}
+
+	public static AnalysisEngineDescription createAnnotatorDescription(
+			boolean saveAnn, boolean printAnn)
+			throws ResourceInitializationException {
+		return AnalysisEngineFactory.createPrimitiveDescription(
+				ExampleHelloWorldAnnotator.class,
+				ExampleHelloWorldAnnotator.PARAM_SAVE_ANN, saveAnn,
+				ExampleHelloWorldAnnotator.PARAM_PRINT_ANN, printAnn);
+	}
+
+	public static AnalysisEngineDescription createAnnotatorDescription()
+			throws ResourceInitializationException {
+		return AnalysisEngineFactory.createPrimitiveDescription(
+				ExampleHelloWorldAnnotator.class,
+				ExampleHelloWorldAnnotator.PARAM_SAVE_ANN, true,
+				ExampleHelloWorldAnnotator.PARAM_PRINT_ANN, true);
+	}
+}

Propchange: ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/ae/ExampleHelloWorldAnnotator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/ExampleAggregatePipeline.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/ExampleAggregatePipeline.java?rev=1606116&view=auto
==============================================================================
--- ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/ExampleAggregatePipeline.java
(added)
+++ ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/ExampleAggregatePipeline.java
Fri Jun 27 15:05:23 2014
@@ -0,0 +1,56 @@
+package org.apache.ctakes.examples.pipelines;
+
+import java.io.FileWriter;
+
+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.jcas.JCas;
+import org.uimafit.factory.AggregateBuilder;
+import org.uimafit.factory.JCasFactory;
+import org.uimafit.pipeline.SimplePipeline;
+import org.uimafit.util.JCasUtil;
+
+/**
+ * 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 ExampleAggregatePipeline {
+
+
+	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]));
+	  }
+
+}
+
+

Propchange: ctakes/trunk/ctakes-examples/src/main/java/org/apache/ctakes/examples/pipelines/ExampleAggregatePipeline.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message