0
votes

I have a chatbot in C# that takes the users message, and using LUIS decides what intent the user has. When a specific intent is found it starts a FormFlow. I have been able to successfully fill fields in the form from the users initial message using LUIS entities. However I am stuck on the Date and Time entity. When LUIS provides the entities it sends them as 2 separate entities (builtin.datetime.time & builtin.datetime.time), yet I need these saved under one Form Field (DateTime). How can I have the entity Time AND Date get saved to the DateTime field?

I currently only know how to save only one field (save time and defaults to today's date, or save date and defaults to 12AM).

Here is how I currently save the date entity to my Form field

        EntityRecommendation entityDateTime;
        result.TryFindEntity("builtin.datetime.date", out entityDateTime);
        if (entityDateTime != null)
            entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityDateTime.Entity });
2

2 Answers

0
votes

You can use Chronic Parser (it's being used in some of the examples in official botbuilder github source code)

URL: https://github.com/robertwilczynski/nChronic

To merge date and time into one single datetime entity, see the sample code below

EntityRecommendation time;
EntityRecommendation date;

var timeFound = result.TryFindEntity(EntityConstant.EntityBuiltInTime, out time);
if (result.TryFindEntity(EntityConstant.EntityBuiltInDate, out date))
{
    return timeFound ? (date.Entity + " " + time.Entity).Parse() : date.Entity.Parse();
}

ChronicParserExtension.cs

public static Tuple<DateTime, DateTime> Parse(this string input)
{
    var parser = new Parser(new Options
    {
        FirstDayOfWeek = DayOfWeek.Monday
    });

    var span = parser.Parse(input);

    if (span.Start != null && span.End != null)
    {
        return Tuple.Create(span.Start.Value, span.End.Value);        
    }

    return null;
}

Hope it helps.

0
votes

Much thanks to @kienct89, as he helped me figure things out, however I did not need to use Chronic. I got the results I wanted with the following code, happy to field comments if there is a better way to write this

        EntityRecommendation entityDate;
        EntityRecommendation entityTime;
        result.TryFindEntity("builtin.datetime.date", out entityDate);
        result.TryFindEntity("builtin.datetime.time", out entityTime);
        if ((entityDate != null) & (entityTime != null))
            entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityDate.Entity + " " + entityTime.Entity });

        else if (entityDate != null)
            entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityDate.Entity });

        else if (entityTime != null)
            entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityTime.Entity });