ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlig...@apache.org
Subject svn commit: r1549631 - in /ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature: EventDurationFeatureExtractor.java ExpectedDurationFeatureExtractor.java
Date Mon, 09 Dec 2013 17:56:24 GMT
Author: dligach
Date: Mon Dec  9 17:56:23 2013
New Revision: 1549631

URL: http://svn.apache.org/r1549631
Log:
added a duration feature extractor to be used with docTimeRel

Added:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ExpectedDurationFeatureExtractor.java
  (with props)
Modified:
    ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/EventDurationFeatureExtractor.java

Modified: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/EventDurationFeatureExtractor.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/EventDurationFeatureExtractor.java?rev=1549631&r1=1549630&r2=1549631&view=diff
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/EventDurationFeatureExtractor.java
(original)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/EventDurationFeatureExtractor.java
Mon Dec  9 17:56:23 2013
@@ -59,7 +59,7 @@ public class EventDurationFeatureExtract
     if(arg1Distribution == null) {
       features.add(new Feature("arg1_no_duration_info"));
     } else {
-      float expectation1 = expectedDuration(arg1Distribution);
+      float expectation1 = ExpectedDurationFeatureExtractor.expectedDuration(arg1Distribution);
       features.add(new Feature("arg1_expected_duration", expectation1));
     }
     
@@ -67,36 +67,12 @@ public class EventDurationFeatureExtract
     if(arg2Distribution == null) {
       features.add(new Feature("arg2_no_duration_info"));
     } else {
-      float expectation2 = expectedDuration(arg2Distribution);
+      float expectation2 = ExpectedDurationFeatureExtractor.expectedDuration(arg2Distribution);
       features.add(new Feature("arg2_expected_duration", expectation2));
     }
     
     return features;
   }
-
-  /**
-   * Compute expected duration in seconds (days?).
-   */
-  private static float expectedDuration(Map<String, Float> distribution) {
-    
-    // unit of time -> duration in seconds
-    final Map<String, Integer> converter = ImmutableMap.<String, Integer>builder()
-        .put("second", 1)
-        .put("minute", 60)
-        .put("hour", 60 * 60)
-        .put("day", 60 * 60 * 24)
-        .put("week", 60 * 60 * 24 * 7)
-        .put("month", 60 * 60 * 24 * 30)
-        .put("year", 60 * 60 * 24 * 365)
-        .build();
-
-    float expectation = 0f;
-    for(String unit : distribution.keySet()) {
-      expectation = expectation + (converter.get(unit) * distribution.get(unit));
-    }
-  
-    return expectation / converter.get("year");
-  }
   
   private static class Callback implements LineProcessor <Map<String, Map<String,
Float>>> {
 

Added: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ExpectedDurationFeatureExtractor.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ExpectedDurationFeatureExtractor.java?rev=1549631&view=auto
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ExpectedDurationFeatureExtractor.java
(added)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ExpectedDurationFeatureExtractor.java
Mon Dec  9 17:56:23 2013
@@ -0,0 +1,119 @@
+/**
+ * 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.temporal.ae.feature;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.tcas.Annotation;
+import org.cleartk.classifier.Feature;
+import org.cleartk.classifier.feature.extractor.CleartkExtractorException;
+import org.cleartk.classifier.feature.extractor.simple.SimpleFeatureExtractor;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.io.Files;
+import com.google.common.io.LineProcessor;
+
+public class ExpectedDurationFeatureExtractor implements SimpleFeatureExtractor {
+
+  @Override
+  public List<Feature> extract(JCas view, Annotation annotation) throws CleartkExtractorException
{ 
+
+    List<Feature> features = new ArrayList<Feature>();
+    File durationLookup = new File("/Users/dima/Boston/Thyme/Duration/Output/Duration/distribution.txt");
+    String annotationText = annotation.getCoveredText().toLowerCase();
+    
+    Map<String, Map<String, Float>> textToDistribution = null;
+    try {
+      textToDistribution = Files.readLines(durationLookup, Charsets.UTF_8, new Callback());
+    } catch(IOException e) {
+      e.printStackTrace();
+      return features;
+    }
+    
+    Map<String, Float> argDistribution = textToDistribution.get(annotationText);
+    if(argDistribution == null) {
+      features.add(new Feature("arg1_no_duration_info"));
+    } else {
+      float expectation1 = expectedDuration(argDistribution);
+      features.add(new Feature("arg1_expected_duration", expectation1));
+    }
+    
+    return features;
+  }
+
+  /**
+   * Compute expected duration in seconds. Normalize by number of seconds in a year.
+   */
+  public static float expectedDuration(Map<String, Float> distribution) {
+    
+    // unit of time -> duration in seconds
+    final Map<String, Integer> converter = ImmutableMap.<String, Integer>builder()
+        .put("second", 1)
+        .put("minute", 60)
+        .put("hour", 60 * 60)
+        .put("day", 60 * 60 * 24)
+        .put("week", 60 * 60 * 24 * 7)
+        .put("month", 60 * 60 * 24 * 30)
+        .put("year", 60 * 60 * 24 * 365)
+        .build();
+
+    float expectation = 0f;
+    for(String unit : distribution.keySet()) {
+      expectation = expectation + (converter.get(unit) * distribution.get(unit));
+    }
+  
+    return expectation / converter.get("year");
+  }
+  
+  private static class Callback implements LineProcessor <Map<String, Map<String,
Float>>> {
+
+    // map event text to its duration distribution
+    private Map<String, Map<String, Float>> textToDistribution;
+    
+    public Callback() {
+      textToDistribution = new HashMap<String, Map<String, Float>>();
+    }
+    
+    public boolean processLine(String line) throws IOException {
+
+      String[] elements = line.split(", "); // e.g. pain, second:0.000, minute:0.005, hour:0.099,
...
+      Map<String, Float> distribution = new HashMap<String, Float>();
+      
+      for(int durationBinNumber = 1; durationBinNumber < elements.length; durationBinNumber++)
{
+        String[] durationAndValue = elements[durationBinNumber].split(":"); // e.g. "day:0.475"
+        distribution.put(durationAndValue[0], Float.parseFloat(durationAndValue[1]));
+      }
+      
+      textToDistribution.put(elements[0], distribution);
+      return true;
+    }
+
+    public Map<String, Map<String, Float>> getResult() {
+
+      return textToDistribution;
+    }
+  }
+}
\ No newline at end of file

Propchange: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ExpectedDurationFeatureExtractor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message