-
Notifications
You must be signed in to change notification settings - Fork 433
Description
I am trying to parse individual dates at the beginning of a string. If found, the function should return the input substring which represents that text. This is a function I wrote
public static bool TryGetDateTimeAtStringStart(this string timexString, DateTime? referenceTime,[NotNullWhen(true)] out DateTime? dateTime, [NotNullWhen(true)] out string? text)
{
dateTime = null;
text = null;
var results = DateTimeRecognizer.RecognizeDateTime(timexString, Culture.English, refTime: referenceTime ?? DateTime.Today);
if (results.FirstOrDefault() is not ModelResult result)
{
return false;
}
// The Resolution dictionary contains the structured data
var resolutionValues = (IList<Dictionary<string, string>>)result.Resolution["values"];
// The first value in the list is usually the most likely interpretation
var mainResolution = resolutionValues[0];
if (results[0].Start == 0 && result.TypeName is "datetimeV2.date" or "datetimeV2.time" or "datetimeV2.datetime")
{
text = timexString.Substring(0, result.End - result.Start+1);
dateTime = HandlePointInTime(mainResolution);
return dateTime!=null;
}
return false;
}
This works perfectly if the input text is "this Monday". or "on 31/12/2023". However, if there is a range at the beginning it directly recognizes date range without giving any information about the underlying text which represents from and two dates.
For example; 3pm Monday to 3pm next Tuesday would be recognized as an entire date range. But within the result object, there is no information about the input constituents of the date range.
Is there a way to extract dates in ranges like these? Or this is fundamentally impossible task? Perhaps because the library relies on statistical parsing/pattern matching, and hence, the entire expression can be identified as range but not individual components?
For example; 3pm Monday to 3pm next Tuesday would be recognized as an entire date range.
Is there a way to extract dates in ranges like these?