private SUTime.Duration extractDuration(MatchResult results, String val) {
      String unit = null;
      if (unitMatchGroup >= 0) unit = results.group(unitMatchGroup);
      if (val == null) {
        val = (unit.endsWith("s")) ? "X" : "1";
        //        val = "1";
      }
      if (underspecifiedValMatchGroup >= 0) {
        val = defaultUnderspecifiedValue;
        if (results.groupCount() >= underspecifiedValMatchGroup) {
          if (results.group(underspecifiedValMatchGroup) != null) {
            val = "X";
          }
        }
      }

      SUTime.Duration d = patterns.getDuration(val, unit);
      if (d == null) {
        logger.warning(
            "Unable to get duration with: val="
                + val
                + ", unit="
                + unit
                + ", matched="
                + results.group());
      }
      return d;
    }
 public SUTime.TemporalOp apply(MatchResult in) {
   if (group >= 0) {
     String expr = in.group(group);
     if (expr != null) {
       return patterns.lookupTemporalOp(expr);
     }
   }
   return null;
 }
  public List<TimeExpression> extractTimeExpressions(CoreMap annotation, String docDateStr) {
    List<CoreMap> mergedNumbers = NumberNormalizer.findAndMergeNumbers(annotation);
    annotation.set(CoreAnnotations.NumerizedTokensAnnotation.class, mergedNumbers);

    // TODO: docDate may not have century....
    SUTime.Time docDate = timexPatterns.parseDateTime(docDateStr);

    List<? extends MatchedExpression> matchedExpressions =
        expressionExtractor.extractExpressions(annotation);
    List<TimeExpression> timeExpressions = new ArrayList<TimeExpression>(matchedExpressions.size());
    for (MatchedExpression expr : matchedExpressions) {
      if (expr instanceof TimeExpression) {
        timeExpressions.add((TimeExpression) expr);
      } else {
        timeExpressions.add(new TimeExpression(expr));
      }
    }

    // Add back nested time expressions for ranges....
    // For now only one level of nesting...
    if (options.includeNested) {
      List<TimeExpression> nestedTimeExpressions = new ArrayList<TimeExpression>();
      for (TimeExpression te : timeExpressions) {
        if (te.isIncludeNested()) {
          List<? extends CoreMap> children =
              te.getAnnotation().get(TimeExpression.ChildrenAnnotation.class);
          if (children != null) {
            for (CoreMap child : children) {
              TimeExpression childTe = child.get(TimeExpression.Annotation.class);
              if (childTe != null) {
                nestedTimeExpressions.add(childTe);
              }
            }
          }
        }
      }
      timeExpressions.addAll(nestedTimeExpressions);
    }
    Collections.sort(timeExpressions, MatchedExpression.EXPR_TOKEN_OFFSETS_NESTED_FIRST_COMPARATOR);
    timeExpressions = filterInvalidTimeExpressions(timeExpressions);

    // Some resolving is done even if docDate null...
    if (
    /*docDate != null && */ timeExpressions != null) {
      resolveTimeExpressions(annotation, timeExpressions, docDate);
    }
    // Annotate timex
    return timeExpressions;
  }
 public void init(Options options) {
   this.options = options;
   timexPatterns = new TimeExpressionPatterns(options);
   // TODO: does not allow for multiple loggers
   if (options.verbose) {
     logger.setLevel(Level.FINE);
   } else {
     logger.setLevel(Level.SEVERE);
   }
   NumberNormalizer.setVerbose(options.verbose);
   if (options.grammarFilename != null) {
     List<String> filenames = StringUtils.split(options.grammarFilename, "\\s*[,;]\\s*");
     this.expressionExtractor =
         CoreMapExpressionExtractor.createExtractorFromFiles(timexPatterns.env, filenames);
     // this.expressionExtractor =
     // CoreMapExpressionExtractor.createExtractorFromFile(timexPatterns.env,
     // options.grammarFilename);
   } else {
     this.expressionExtractor = new CoreMapExpressionExtractor();
     this.expressionExtractor.setExtractRules(
         timexPatterns.getTimeExtractionRule(), timexPatterns.getCompositeTimeExtractionRule());
   }
   this.expressionExtractor.setLogger(logger);
 }
 private List<TimeExpression> filterInvalidTimeExpressions(List<TimeExpression> timeExprs) {
   int nfiltered = 0;
   List<TimeExpression> filtered =
       new ArrayList<TimeExpression>(timeExprs.size()); // Approximate size
   for (TimeExpression timeExpr : timeExprs) {
     if (timexPatterns.checkTimeExpression(timeExpr)) {
       filtered.add(timeExpr);
     } else {
       nfiltered++;
     }
   }
   if (nfiltered > 0) {
     logger.finest("Filtered " + nfiltered);
   }
   return filtered;
 }
 private void resolveTimeExpressions(
     CoreMap annotation, List<TimeExpression> timeExpressions, SUTime.Time docDate) {
   for (TimeExpression te : timeExpressions) {
     SUTime.Temporal temporal = te.getTemporal();
     if (temporal != null) {
       // TODO: use correct time for anchor
       try {
         int flags = timexPatterns.determineRelFlags(annotation, te);
         // int flags = 0;
         SUTime.Temporal grounded = temporal.resolve(docDate, flags);
         if (grounded == null) {
           logger.warning("Error resolving " + temporal + ", using docDate=" + docDate);
         }
         if (grounded != temporal) {
           te.origTemporal = temporal;
           te.setTemporal(grounded);
         }
       } catch (Exception ex) {
         logger.log(Level.WARNING, "Error resolving " + temporal, ex);
       }
     }
   }
 }