You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by dl...@apache.org on 2015/09/09 21:52:12 UTC

svn commit: r1702095 - in /ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap: ./ NarrativeContainers.java

Author: dligach
Date: Wed Sep  9 19:52:12 2015
New Revision: 1702095

URL: http://svn.apache.org/r1702095
Log:
Code to find nearest event to a key word/pattern

Added:
    ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/
    ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/NarrativeContainers.java   (with props)

Added: ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/NarrativeContainers.java
URL: http://svn.apache.org/viewvc/ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/NarrativeContainers.java?rev=1702095&view=auto
==============================================================================
--- ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/NarrativeContainers.java (added)
+++ ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/NarrativeContainers.java Wed Sep  9 19:52:12 2015
@@ -0,0 +1,107 @@
+/**
+ * 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.bootstrap;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.ctakes.typesystem.type.syntax.WordToken;
+import org.apache.ctakes.typesystem.type.textsem.EventMention;
+import org.apache.ctakes.typesystem.type.textspan.Sentence;
+import org.apache.ctakes.utils.Utils;
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.pipeline.SimplePipeline;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.JCas;
+
+import com.lexicalscope.jewel.cli.CliFactory;
+import com.lexicalscope.jewel.cli.Option;
+
+/**
+ * Read cTAKES annotations from XMI files.
+ *  
+ * @author dmitriy dligach
+ */
+public class NarrativeContainers {
+  
+  static interface Options {
+
+    @Option(
+        longName = "xmi-dir",
+        description = "path to xmi files")
+    public File getInputDirectory();
+  }
+  
+	public static void main(String[] args) throws Exception {
+		  
+		Options options = CliFactory.parseArguments(Options.class, args);
+    CollectionReader collectionReader = Utils.getCollectionReader(options.getInputDirectory());
+    AnalysisEngine annotationConsumer = AnalysisEngineFactory.createEngine(NarrativeContainerExtractor.class);
+		SimplePipeline.runPipeline(collectionReader, annotationConsumer);
+	}
+
+  /**
+   */
+  public static class NarrativeContainerExtractor extends JCasAnnotator_ImplBase {
+    
+    public static final String anchor = "during";
+    public static final int maxDistance = 0;
+    
+    @Override
+    public void process(JCas jCas) throws AnalysisEngineProcessException {
+
+      for(Sentence sentence : JCasUtil.select(jCas, Sentence.class)) {
+        for(WordToken word : JCasUtil.selectCovered(jCas, WordToken.class, sentence)) {
+          if(word.getCoveredText().equals(anchor)) {
+            EventMention nearestEvent = getNearestEventMention(jCas, word);
+            if(nearestEvent != null) {
+              System.out.println(nearestEvent.getCoveredText());
+            }
+          } 
+        }
+      }
+    }
+    
+    /**
+     * Find nearest event mention on the right of a word token. 
+     * Must be within allowable distance. Return null if none found.
+     */
+    private static EventMention getNearestEventMention(JCas jCas, WordToken word) {
+
+      List<EventMention> eventMentions = JCasUtil.selectFollowing(jCas, EventMention.class, word, 1);
+      if(eventMentions.size() < 1) {
+        return null;
+      }
+
+      EventMention nearestEventMention = eventMentions.get(0);
+      int distance = JCasUtil.selectBetween(jCas, WordToken.class, word, nearestEventMention).size();
+      if(distance > maxDistance) {
+        return null;
+      }
+      
+      return nearestEventMention;
+    }
+  }
+}
+
+  
\ No newline at end of file

Propchange: ctakes/sandbox/ctakes-wsd/src/main/java/org/apache/ctakes/bootstrap/NarrativeContainers.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain