Search code examples
c#botframeworkazure-language-understanding

Working with LUIS datetimeV2 entities in botframework v4


I'm using the Botframework v4 C# with the dispatch tool to get results from LUIS & QnA Maker. Some of my LUIS results have datetimev2 entities which I'm not quite sure how to process properly.

I'm seeing the resolved datetime value inside the AdditionalProperties, is there a built-in class that I can cast this object to? Are there any samples out there that explain how to work with entities in general within the botframework v4? All docs related to this still seem to be for v3 only.

LUIS Datetime Result


Solution

  • I ended up using these LuisResolutionExtensions to extract DateTime values and LUIS entities in general.

    public static DateTime ProcessDateTimeV2Date(this EntityModel entity)
    {
        if (entity.AdditionalProperties.TryGetValue("resolution", out dynamic resolution))
        {
            var resolutionValues = (IEnumerable<dynamic>) resolution.values;
            var datetimes = resolutionValues.Select(val => DateTime.Parse(val.value.Value));
    
            if (datetimes.Count() > 1)
            {
                // assume the date is in the next 7 days
                var bestGuess = datetimes.Single(dateTime => dateTime > DateTime.Now && (DateTime.Now - dateTime).Days <= 7);
                return bestGuess;
            }
    
            return datetimes.FirstOrDefault();
        }
    
        throw new Exception("ProcessDateTimeV2DateTime");
    }
    
    public static string ProcessRoom(this EntityModel entity)
    {
        if (entity.AdditionalProperties.TryGetValue("resolution", out dynamic resolution))
        {
            var resolutionValues = (IEnumerable<dynamic>) resolution.values;
            return resolutionValues.Select(room => room).FirstOrDefault();
        }
    
        throw new Exception("ProcessRoom");
    }