Using the prebuilt entity datetimeV2 to capture daterange

Viewed 319

I am using the prebuilt entity datetimeV2 to capture daterange. I am using it to map it to two different utterances - this week and specifically between two dates. How do I map it in FormFlow model?

What I want to do is this -

Search for all flights this week

or

Search for all flights between today and 7/10/2017

LUIS entity is accurately captured but am not able to map it to FormFlow model - my model contains fromDate and ToDate. How to map a single matched Luis Entity with these properties?

1 Answers

The code below illustrates the best solution I've come up with for this. I'm still looking for a better solution and am hopeful of finding one. You have add the datetime recognizer nuget package to your solution and add the appropriate using statements.

using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.DateTime;

Note that turnContext.Activity.Text contains the text Luis is interpreting, ie. "last week".

var culture = Culture.English;
var r = DateTimeRecognizer.RecognizeDateTime(turnContext.Activity.Text, culture);
if (r.Count > 0 && r.First().TypeName.StartsWith("datetimeV2"))
{
    var first = r.First();
    resolutionValues = (IList<Dictionary<string, string>>)first.Resolution["values"];
    var asString = string.Join(";", resolutionValues[0]);
    await turnContext.SendActivityAsync($"==>LUIS: resolutions values: {asString}\n");
    var subType = first.TypeName.Split('.').Last();
    if (subType.Contains("date") && !subType.Contains("range"))
    {
        // a date (or date & time) or multiple
        var moment = resolutionValues.Select(v => DateTime.Parse(v["value"])).FirstOrDefault();
        await turnContext.SendActivityAsync($"==>LUIS DateTime Moment: {moment}\n");
    }
    else if (subType.Contains("date") && subType.Contains("range"))
    {
        // range
        var from = DateTime.Parse(resolutionValues.First()["start"]);
        var to = DateTime.Parse(resolutionValues.First()["end"]);
        await turnContext.SendActivityAsync($"==>LUIS DateTime Range: from: {from} to: {to}\n");
    }
}

I posted a more specific version of this question at: Retrieve complete LUIS DateTimeV2 entity from recognizer

Related