ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlig...@apache.org
Subject svn commit: r1759449 - /ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/data/EventTimeRelPrinter.java
Date Tue, 06 Sep 2016 15:47:57 GMT
Author: dligach
Date: Tue Sep  6 15:47:57 2016
New Revision: 1759449

URL: http://svn.apache.org/viewvc?rev=1759449&view=rev
Log:
cleanup

Modified:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/data/EventTimeRelPrinter.java

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/data/EventTimeRelPrinter.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/data/EventTimeRelPrinter.java?rev=1759449&r1=1759448&r2=1759449&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/data/EventTimeRelPrinter.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/nn/data/EventTimeRelPrinter.java
Tue Sep  6 15:47:57 2016
@@ -106,7 +106,7 @@ public class EventTimeRelPrinter {
     // write training data to file
     CollectionReader trainCollectionReader = Utils.getCollectionReader(trainFiles);
     AnalysisEngine trainDataWriter = AnalysisEngineFactory.createEngine(
-        RelationSnippetPrinter.class,
+        TrainRelationSnippetPrinter.class,
         "OutputFile",
         trainFile.getAbsoluteFile());
     SimplePipeline.runPipeline(trainCollectionReader, trainDataWriter);
@@ -114,7 +114,7 @@ public class EventTimeRelPrinter {
     // write dev data to file
     CollectionReader devCollectionReader = Utils.getCollectionReader(devFiles);
     AnalysisEngine devDataWriter = AnalysisEngineFactory.createEngine(
-        RelationSnippetPrinter.class,
+        TestRelationSnippetPrinter.class,
         "OutputFile",
         devFile.getAbsolutePath());
     SimplePipeline.runPipeline(devCollectionReader, devDataWriter);
@@ -125,7 +125,7 @@ public class EventTimeRelPrinter {
    * 
    * @author dmitriy dligach
    */
-  public static class RelationSnippetPrinter extends JCasAnnotator_ImplBase {
+  public static class TrainRelationSnippetPrinter extends JCasAnnotator_ImplBase {
 
     @ConfigurationParameter(
         name = "OutputFile",
@@ -185,15 +185,98 @@ public class EventTimeRelPrinter {
             if(timeEventRelation != null) {
               if(timeEventRelation.getCategory().equals("CONTAINS")) {
                 label = "contains";  // this is contains
-              } else {
-                // label = "none";      // e.g. before or overlap
+              }
+            }
+            // there is at least one instance where both event-time and time-event rels exist?
+            if(eventTimeRelation != null) {
+              if(eventTimeRelation.getCategory().equals("CONTAINS")) {
+                label = "contains-1"; // this is contains
+              } 
+            } 
+
+            String context;
+            if(time.getBegin() < event.getBegin()) {
+              // ... time ... event ... scenario
+              context = getTokenContext(systemView, sentence, time, "t", event, "e", 2);
 
+            } else {
+              // ... event ... time ... scenario
+              context = getTokenContext(systemView, sentence, event, "e", time, "t", 2);
+            }
+
+            String text = String.format("%s|%s", label, context);
+            eventTimeRelationsInSentence.add(text.toLowerCase());
+          }
+        }  
+
+        try {
+          Files.write(Paths.get(outputFile), eventTimeRelationsInSentence, StandardOpenOption.APPEND);
+        } catch (IOException e) {
+          e.printStackTrace();
+        }
+      }
+    }
+  }
+
+  /**
+   * Print gold standard relations and their context.
+   * 
+   * @author dmitriy dligach
+   */
+  public static class TestRelationSnippetPrinter extends JCasAnnotator_ImplBase {
+
+    @ConfigurationParameter(
+        name = "OutputFile",
+        mandatory = true,
+        description = "path to the output file")
+    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);
+      }
+
+      // 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, TemporalTextRelation.class))
{
+        Annotation arg1 = relation.getArg1().getArgument();
+        Annotation arg2 = relation.getArg2().getArgument();
+        relationLookup.put(Arrays.asList(arg1, arg2), relation);
+      }
+
+      // go over sentences, extracting event-event relation instances
+      for(Sentence sentence : JCasUtil.select(systemView, Sentence.class)) {
+        List<String> eventTimeRelationsInSentence = new ArrayList<>();
+
+        // retrieve event-time relations in this sentence
+        for(EventMention event : JCasUtil.selectCovered(goldView, EventMention.class, sentence))
{
+          for(TimeMention time : JCasUtil.selectCovered(goldView, TimeMention.class, sentence))
{
+
+            BinaryTextRelation timeEventRelation = relationLookup.get(Arrays.asList(time,
event));
+            BinaryTextRelation eventTimeRelation = relationLookup.get(Arrays.asList(event,
time));
+
+            // TODO: am I capturing multiple relations here? probably not...
+            String label = "none";
+            if(timeEventRelation != null) {
+              if(timeEventRelation.getCategory().equals("CONTAINS")) {
+                label = "contains";  // this is contains
               }
             } 
             if(eventTimeRelation != null) {
               if(eventTimeRelation.getCategory().equals("CONTAINS")) {
                 label = "contains-1"; // this is contains
-              } else {
-                // label = "none";       // some other relation type
               }
             } 
 



Mime
View raw message