Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Timex Expressions in .NET Core

Using LUIS engine, I receive XXXX-10-28 as a date in the Entity Value.

I've tried using Chronic to parse but Chronic does not work with timex library/formats.

I'm expecting following strings as inputs

  • XXXX-10-28 should equate to 2018-10-28 (future)
  • 2018-10-02TMO should equate to 2018-10-02 i.e. tomorrow

Please note that XXXX-XX represents YYYY-MM but it doesn't have numeric values

Is there any library, or way to parse such strings to a valid datetime format in ASP.NET Core?

like image 462
GeekzSG Avatar asked Sep 11 '25 13:09

GeekzSG


1 Answers

You can use the Microsoft.Recognizers.Text.DataTypes.TimexExpression package from NuGet. It's part of the Microsoft Recognizers Text project here on github

I found two ways you can use this library:

Parse the expression using a TimexProperty and guess the year yourself:

var parsed = new Microsoft.Recognizers.Text.DataTypes.TimexExpression.TimexProperty("XXXX-10-28");
Console.WriteLine(parsed.Year); // = null
Console.WriteLine(parsed.Month); // = 28
Console.WriteLine(parsed.DayOfMonth); // = 10

Resolve times useing the TimexResolver

var resolution = Microsoft.Recognizers.Text.DataTypes.TimexExpression.TimexResolver.Resolve(new [] { "XXXX-10-28" }, System.DateTime.Today)

The resolution.Values will contain an array with two resolution entries, one for the previous occurrence of that date and one for the next occurrence of that date (based on the DateTime you pass into the Resolve method.

Note that from personal experience and from what I saw on github, that at the time of writing, this package can be quite buggy with the more advanced expressions.

like image 165
Wessel T. Avatar answered Sep 14 '25 02:09

Wessel T.