private void ResolveTimeExpression(ICoreMap annotation, TimeExpression te, SUTime.Time docDate) { 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.Debug("Error resolving " + temporal + ", using docDate=" + docDate); } if (grounded != temporal) { te.origTemporal = temporal; te.SetTemporal(grounded); } } catch (Exception ex) { if (options.verbose) { logger.Warn("Error resolving " + temporal, ex); logger.Warn(ex); } } } }
public virtual int DetermineRelFlags(ICoreMap annotation, TimeExpression te) { int flags = 0; bool flagsSet = false; if (te.value.GetTags() != null) { IValue v = te.value.GetTags().GetTag("resolveTo"); if (v != null && v.Get() is Number) { flags = ((Number)v.Get()); flagsSet = true; } } if (!flagsSet) { if (te.GetTemporal() is SUTime.PartialTime) { flags = SUTime.ResolveToClosest; } } return(flags); }
public virtual IList <TimeExpression> ExtractTimeExpressions(ICoreMap annotation, SUTime.Time refDate, SUTime.TimeIndex timeIndex) { if (!annotation.ContainsKey(typeof(CoreAnnotations.NumerizedTokensAnnotation))) { try { IList <ICoreMap> mergedNumbers = NumberNormalizer.FindAndMergeNumbers(annotation); annotation.Set(typeof(CoreAnnotations.NumerizedTokensAnnotation), mergedNumbers); } catch (NumberFormatException e) { logger.Warn("Caught bad number: " + e.Message); annotation.Set(typeof(CoreAnnotations.NumerizedTokensAnnotation), new List <ICoreMap>()); } } IList <MatchedExpression> matchedExpressions = expressionExtractor.ExtractExpressions(annotation); IList <TimeExpression> timeExpressions = new List <TimeExpression>(matchedExpressions.Count); foreach (MatchedExpression expr in matchedExpressions) { // Make sure we have the correct type (instead of just MatchedExpression) //timeExpressions.add(TimeExpression.TimeExpressionConverter.apply(expr)); // TODO: Fix the extraction pipeline so it creates TimeExpression instead of MatchedExpressions // For now, grab the time expression from the annotation (this is good, so we don't have duplicate copies) TimeExpression annoTe = expr.GetAnnotation().Get(typeof(TimeExpression.Annotation)); if (annoTe != null) { timeExpressions.Add(annoTe); } } // We cache the document date in the timeIndex if (timeIndex.docDate == null) { if (refDate != null) { timeIndex.docDate = refDate; } else { if (options.searchForDocDate) { // there was no document date but option was set to look for document date timeIndex.docDate = FindReferenceDate(timeExpressions); } } } // Didn't have a reference date - try using cached doc date if (refDate == null) { refDate = timeIndex.docDate; } // Some resolving is done even if refDate null... ResolveTimeExpressions(annotation, timeExpressions, refDate); if (options.restrictToTimex3) { // Keep only TIMEX3 compatible timeExpressions IList <TimeExpression> kept = new List <TimeExpression>(timeExpressions.Count); foreach (TimeExpression te in timeExpressions) { if (te.GetTemporal() != null && te.GetTemporal().GetTimexValue() != null) { kept.Add(te); } else { IList <ICoreMap> children = te.GetAnnotation().Get(typeof(TimeExpression.ChildrenAnnotation)); if (children != null) { foreach (ICoreMap child in children) { TimeExpression childTe = child.Get(typeof(TimeExpression.Annotation)); if (childTe != null) { ResolveTimeExpression(annotation, childTe, refDate); if (childTe.GetTemporal() != null && childTe.GetTemporal().GetTimexValue() != null) { kept.Add(childTe); } } } } } } timeExpressions = kept; } // Add back nested time expressions for ranges.... // For now only one level of nesting... if (options.includeNested) { IList <TimeExpression> nestedTimeExpressions = new List <TimeExpression>(); foreach (TimeExpression te in timeExpressions) { if (te.IsIncludeNested()) { IList <ICoreMap> children = te.GetAnnotation().Get(typeof(TimeExpression.ChildrenAnnotation)); if (children != null) { foreach (ICoreMap child in children) { TimeExpression childTe = child.Get(typeof(TimeExpression.Annotation)); if (childTe != null) { nestedTimeExpressions.Add(childTe); } } } } } ResolveTimeExpressions(annotation, nestedTimeExpressions, refDate); Sharpen.Collections.AddAll(timeExpressions, nestedTimeExpressions); } timeExpressions.Sort(MatchedExpression.ExprTokenOffsetsNestedFirstComparator); // Some resolving is done even if refDate null... ResolveTimeExpressions(annotation, timeExpressions, refDate); return(timeExpressions); }
public override IValue Apply(Env env, IList <IValue> @in) { if (@in.Count == 2) { SUTime.Duration d = (SUTime.Duration)@in[0].Get(); if (@in[1].Get() is Number) { int m = ((Number)@in[1].Get()); return(new Expressions.PrimitiveValue("DURATION", d.MultiplyBy(m))); } else { if (@in[1].Get() is string) { Number n = System.Convert.ToInt32((string)@in[1].Get()); if (n != null) { return(new Expressions.PrimitiveValue("DURATION", d.MultiplyBy(n))); } else { return(null); } } else { throw new ArgumentException("Invalid arguments to " + this.name); } } } else { if (@in.Count == 5 || @in.Count == 3) { IList <ICoreMap> durationStartTokens = (IList <ICoreMap>)@in[0].Get(); Number durationStartVal = (durationStartTokens != null) ? durationStartTokens[0].Get(typeof(CoreAnnotations.NumericCompositeValueAnnotation)) : null; IList <ICoreMap> durationEndTokens = (IList <ICoreMap>)@in[1].Get(); Number durationEndVal = (durationEndTokens != null) ? durationEndTokens[0].Get(typeof(CoreAnnotations.NumericCompositeValueAnnotation)) : null; IList <ICoreMap> durationUnitTokens = (IList <ICoreMap>)@in[2].Get(); TimeExpression te = (durationUnitTokens != null) ? durationUnitTokens[0].Get(typeof(TimeExpression.Annotation)) : null; SUTime.Duration durationUnit = (SUTime.Duration)te.GetTemporal(); SUTime.Duration durationStart = (durationStartVal != null) ? durationUnit.MultiplyBy(durationStartVal) : null; SUTime.Duration durationEnd = (durationEndVal != null) ? durationUnit.MultiplyBy(durationEndVal) : null; SUTime.Duration duration = durationStart; if (duration == null) { if (durationEnd != null) { duration = durationEnd; } else { duration = new SUTime.InexactDuration(durationUnit); } } else { if (durationEnd != null) { duration = new SUTime.DurationRange(durationStart, durationEnd); } } SUTime.Time beginTime = (@in.Count > 3) ? (SUTime.Time)@in[3].Get() : null; SUTime.Time endTime = (@in.Count > 4) ? (SUTime.Time)@in[4].Get() : null; SUTime.Temporal temporal = this.AddEndPoints(duration, beginTime, endTime); if (temporal is SUTime.Range) { return(new Expressions.PrimitiveValue("RANGE", temporal)); } else { return(new Expressions.PrimitiveValue("DURATION", temporal)); } } else { throw new ArgumentException("Invalid number of arguments to " + this.name); } } }