ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlig...@apache.org
Subject svn commit: r1489571 - in /ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis: RelationContextAnalysisConsumer.java RelationContextAnalysisPipeline.java RelationContextViewerPipeline.java
Date Tue, 04 Jun 2013 19:18:51 GMT
Author: dligach
Date: Tue Jun  4 19:18:29 2013
New Revision: 1489571

URL: http://svn.apache.org/r1489571
Log:
moved the consumer inside the pipeline class

Added:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextViewerPipeline.java
      - copied, changed from r1489563, ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextAnalysisPipeline.java
Removed:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextAnalysisConsumer.java
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextAnalysisPipeline.java

Copied: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextViewerPipeline.java
(from r1489563, ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextAnalysisPipeline.java)
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextViewerPipeline.java?p2=ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextViewerPipeline.java&p1=ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextAnalysisPipeline.java&r1=1489563&r2=1489571&rev=1489571&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextAnalysisPipeline.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/data/analysis/RelationContextViewerPipeline.java
Tue Jun  4 19:18:29 2013
@@ -18,38 +18,49 @@
  */
 package org.apache.ctakes.temporal.data.analysis;
 
+import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 import org.apache.ctakes.relationextractor.eval.XMIReader;
+import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
 import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CASException;
 import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
 import org.cleartk.util.Options_ImplBase;
 import org.kohsuke.args4j.Option;
+import org.uimafit.component.JCasAnnotator_ImplBase;
+import org.uimafit.descriptor.ConfigurationParameter;
 import org.uimafit.factory.AnalysisEngineFactory;
 import org.uimafit.factory.CollectionReaderFactory;
 import org.uimafit.pipeline.SimplePipeline;
+import org.uimafit.util.JCasUtil;
 
 /**
  * Print gold standard relations and their context.
  * 
  * @author dmitriy dligach
  */
-public class RelationContextAnalysisPipeline {
+public class RelationContextViewerPipeline {
 
   public static class Options extends Options_ImplBase {
 
     @Option(
         name = "--input-dir",
-        usage = "specify the path to the directory containing the clinical notes to be processed",
+        usage = "specify the path to the directory containing the xmi files",
         required = true)
     public File inputDirectory;
 
     @Option(
         name = "--output-file",
-        usage = "specify the path to the directory containing the clinical notes to be processed",
+        usage = "specify the path to the file to which the output will be written",
         required = true)
     public File outputFile;
   }
@@ -63,13 +74,91 @@ public class RelationContextAnalysisPipe
     CollectionReader collectionReader = getCollectionReader(trainFiles);
 		
     AnalysisEngine annotationConsumer = AnalysisEngineFactory.createPrimitive(
-    		RelationContextAnalysisConsumer.class,
+    		RelationContextPrinter.class,
     		"OutputFile",
     		options.outputFile);
     		
 		SimplePipeline.runPipeline(collectionReader, annotationConsumer);
 	}
-	
+	  
+  /**
+   * Print gold standard relations and their context.
+   * 
+   * @author dmitriy dligach
+   */
+  public static class RelationContextPrinter extends JCasAnnotator_ImplBase {
+
+    @ConfigurationParameter(
+        name = "OutputFile",
+        mandatory = true,
+        description = "path to the file that stores relation data")
+    private String outputFile;
+    
+    @Override
+    public void process(JCas jCas) throws AnalysisEngineProcessException {
+      
+      JCas goldView;
+      try {
+        goldView = jCas.getView("GoldView");
+      } catch (CASException e) {
+        throw new AnalysisEngineProcessException(e);
+      }
+      
+      JCas systemView;
+      try {
+        systemView = jCas.getView("_InitialView");
+      } catch (CASException e) {
+        throw new AnalysisEngineProcessException(e);
+      }
+
+      BufferedWriter writer = getWriter(outputFile, true);
+      try {
+        for(BinaryTextRelation binaryTextRelation : JCasUtil.select(goldView, BinaryTextRelation.class))
{
+          
+          Annotation arg1 = binaryTextRelation.getArg1().getArgument();
+          Annotation arg2 = binaryTextRelation.getArg2().getArgument();
+      
+          String category = binaryTextRelation.getCategory();
+          String text = getTextBetweenAnnotations(systemView, arg1, arg2);
+          String output = String.format("%s|%s|%s|%s\n", category, arg1.getCoveredText(),
arg2.getCoveredText(), text);
+      
+          try {
+            writer.write(output);
+          } catch (IOException e) {
+            throw new AnalysisEngineProcessException(e);
+          }
+        }
+      } finally {      
+        try {
+          writer.close();
+        } catch (IOException e) {
+          throw new AnalysisEngineProcessException(e);
+        }
+      }
+    }
+    
+    private static String getTextBetweenAnnotations(JCas jCas, Annotation arg1, Annotation
arg2) {
+      
+      int begin = Math.min(arg1.getBegin(), arg2.getBegin());
+      int end = Math.max(arg1.getEnd(), arg2.getEnd());
+      
+      return jCas.getDocumentText().substring(begin, end).replaceAll("[\r\n]", " ");
+    }
+    
+    private static BufferedWriter getWriter(String filePath, boolean append) {
+
+      BufferedWriter bufferedWriter = null;
+      try {
+        FileWriter fileWriter = new FileWriter(filePath, append);
+        bufferedWriter = new BufferedWriter(fileWriter);
+      } catch (IOException e) {
+        e.printStackTrace();
+      }
+
+      return bufferedWriter;
+    }
+  }
+
   private static CollectionReader getCollectionReader(List<File> inputFiles) throws
Exception {
     
     List<String> fileNames = new ArrayList<String>();



Mime
View raw message