ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dlig...@apache.org
Subject svn commit: r1552350 - /ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/DurationTimeUnitFeatureExtractor.java
Date Thu, 19 Dec 2013 16:45:05 GMT
Author: dligach
Date: Thu Dec 19 16:45:05 2013
New Revision: 1552350

URL: http://svn.apache.org/r1552350
Log:
features that capture the time unit (e.g. "weeks") of temporal expressions

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

Added: ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/DurationTimeUnitFeatureExtractor.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/DurationTimeUnitFeatureExtractor.java?rev=1552350&view=auto
==============================================================================
--- ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/DurationTimeUnitFeatureExtractor.java
(added)
+++ ctakes/trunk/ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/DurationTimeUnitFeatureExtractor.java
Thu Dec 19 16:45:05 2013
@@ -0,0 +1,91 @@
+/**
+ * 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 info.bethard.timenorm.Period;
+import info.bethard.timenorm.PeriodSet;
+import info.bethard.timenorm.Temporal;
+import info.bethard.timenorm.TemporalExpressionParser;
+import info.bethard.timenorm.TimeSpan;
+import info.bethard.timenorm.TimeSpanSet;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.ctakes.relationextractor.ae.features.RelationFeaturesExtractor;
+import org.apache.ctakes.typesystem.type.textsem.IdentifiedAnnotation;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.jcas.JCas;
+import org.cleartk.classifier.Feature;
+import org.threeten.bp.temporal.TemporalField;
+import org.threeten.bp.temporal.TemporalUnit;
+
+import scala.collection.immutable.Set;
+import scala.util.Try;
+
+public class DurationTimeUnitFeatureExtractor implements RelationFeaturesExtractor {
+
+  @Override
+  public List<Feature> extract(JCas jCas, IdentifiedAnnotation arg1, IdentifiedAnnotation
arg2)
+      throws AnalysisEngineProcessException {
+
+    List<Feature> features = new ArrayList<Feature>();
+    String timeText = arg2.getCoveredText().toLowerCase();  // arg2 is a time mention
+
+    Set<TemporalUnit> units = normalize(timeText);
+    if(units == null) {
+      features.add(new Feature("time_unit", "not_available"));
+    } else {
+      scala.collection.Iterator<TemporalUnit> iterator = units.iterator();
+      while(iterator.hasNext()) {
+        TemporalUnit unit = iterator.next();
+        features.add(new Feature("time_unit", unit.getName()));
+      }
+    }
+
+    return features; 
+  }
+  
+  public static Set<TemporalUnit> normalize(String timex) {
+
+    URL grammarURL = DurationTimeUnitFeatureExtractor.class.getResource("/info/bethard/timenorm/en.grammar");
+    TemporalExpressionParser parser = new TemporalExpressionParser(grammarURL);
+    TimeSpan anchor = TimeSpan.of(2013, 12, 16);
+    Try<Temporal> result = parser.parse(timex, anchor);
+
+    Set<TemporalUnit> units = null;
+    if (result.isSuccess()) {
+      Temporal temporal = result.get();
+
+      if (temporal instanceof Period) {
+        units = ((Period) temporal).unitAmounts().keySet();
+      } else if (temporal instanceof PeriodSet) {
+        units = ((PeriodSet) temporal).period().unitAmounts().keySet();
+      } else if (temporal instanceof TimeSpan) {
+        units = ((TimeSpan) temporal).period().unitAmounts().keySet();
+      } else if (temporal instanceof TimeSpanSet) {
+        Set<TemporalField> fields = ((TimeSpanSet) temporal).fields().keySet();
+        units = null; // fill units by calling .getBaseUnit() on each field
+      }
+    }
+    
+    return units;
+  }
+}

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



Mime
View raw message