ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlig...@apache.org
Subject svn commit: r1662028 - in /ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis: ./ GoldRelationViewer.java Utils.java
Date Tue, 24 Feb 2015 17:10:47 GMT
Author: dligach
Date: Tue Feb 24 17:10:47 2015
New Revision: 1662028

URL: http://svn.apache.org/r1662028
Log:
code to view relations in SHARP gold standard

Added:
    ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/
    ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/GoldRelationViewer.java
  (with props)
    ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/Utils.java
  (with props)

Added: ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/GoldRelationViewer.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/GoldRelationViewer.java?rev=1662028&view=auto
==============================================================================
--- ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/GoldRelationViewer.java
(added)
+++ ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/GoldRelationViewer.java
Tue Feb 24 17:10:47 2015
@@ -0,0 +1,129 @@
+/**
+ * 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.relationextractor.data.analysis;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.ctakes.typesystem.type.relation.BinaryTextRelation;
+import org.apache.ctakes.typesystem.type.textspan.Sentence;
+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.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 org.apache.uima.jcas.tcas.Annotation;
+import org.cleartk.util.ViewUriUtil;
+
+import com.lexicalscope.jewel.cli.CliFactory;
+import com.lexicalscope.jewel.cli.Option;
+
+/**
+ * Print gold standard relations and their context.
+ * 
+ * @author dmitriy dligach
+ */
+public class GoldRelationViewer {
+  
+  static interface Options {
+
+    @Option(longName = "xmi-dir")
+    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(RelationContextPrinter.class);
+		SimplePipeline.runPipeline(collectionReader, annotationConsumer);
+	}
+
+  /**
+   * Print gold standard relations and their context.
+   * 
+   * @author dmitriy dligach
+   */
+  public static class RelationContextPrinter extends JCasAnnotator_ImplBase {
+    
+    @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);
+      }
+
+      // can't iterate over binary text relations in a sentence, so need
+      // a lookup from pair of annotations to binary text relation
+      Map<List<Annotation>, BinaryTextRelation> relationLookup = new HashMap<>();
+      for(BinaryTextRelation relation : JCasUtil.select(goldView, BinaryTextRelation.class))
{
+        Annotation arg1 = relation.getArg1().getArgument();
+        Annotation arg2 = relation.getArg2().getArgument();
+        relationLookup.put(Arrays.asList(arg1, arg2), relation);
+      }
+ 
+      File noteFile = new File(ViewUriUtil.getURI(jCas).toString());
+      String fileName = noteFile.getName();
+      
+      for(Sentence sentence : JCasUtil.select(systemView, Sentence.class)) {
+        List<String> formattedRelationsInSentence = new ArrayList<>();
+        List<Annotation> annotationsInSentence = JCasUtil.selectCovered(goldView, Annotation.class,
sentence);
+        for(Annotation annot1 : annotationsInSentence) {
+          for(Annotation annot2 : annotationsInSentence) {
+            if(annot1 == annot2) {
+              continue;
+            }
+            BinaryTextRelation relation = relationLookup.get(Arrays.asList(annot1, annot2));
+            if(relation != null) {
+              System.out.println(relation.getCategory());
+              String text = String.format("%s(%s, %s)", relation.getCategory(), annot1.getCoveredText(),
annot2.getCoveredText());
+              formattedRelationsInSentence.add(text);
+            }
+          }
+        }
+        
+        if(formattedRelationsInSentence.size() > 0) {
+          System.out.println(fileName + ": " + sentence.getCoveredText());
+          for(String text : formattedRelationsInSentence) {
+            System.out.println(text);
+          }
+          System.out.println();
+        }
+      }
+    }
+  }
+}

Propchange: ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/GoldRelationViewer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/Utils.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/Utils.java?rev=1662028&view=auto
==============================================================================
--- ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/Utils.java
(added)
+++ ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/Utils.java
Tue Feb 24 17:10:47 2015
@@ -0,0 +1,54 @@
+/**
+ * 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.relationextractor.data.analysis;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.ctakes.core.cr.XMIReader;
+import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.fit.factory.CollectionReaderFactory;
+
+/**
+ * Various useful classes and methods.
+ */
+public class Utils {
+  
+  /**
+   * Instantiate an XMI collection reader.
+   */
+  public static CollectionReader getCollectionReader(File inputDirectory) throws Exception
{
+
+    List<String> fileNames = new ArrayList<>();
+    for(File file : inputDirectory.listFiles()) {
+      if(! (file.isHidden())) {
+        fileNames.add(file.getPath());
+      }
+    }
+
+    String[] paths = new String[fileNames.size()];
+    fileNames.toArray(paths);
+
+    return CollectionReaderFactory.createReader(
+        XMIReader.class,
+        XMIReader.PARAM_FILES,
+        paths);
+  }
+}

Propchange: ctakes/trunk/ctakes-relation-extractor/src/main/java/org/apache/ctakes/relationextractor/data/analysis/Utils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message