Extracting an Entity from LUIS in Bot Framework

Recent changes to the supporting LUIS libraries in the Bot Framework have puzzled me. I find that whilst getting the Top Intent from the RecognizerResult recognizerResult?.GetTopScoringIntent(); is easy, getting hold of an Entity seems very strange. I’m not sure if it’s my SDK, LUIS models or what, but this is the code I’m currently having to use…and I confess I still find it all very odd;

public static class RecognizerResultExtensions
{
    public static T GetEntity<T>(this RecognizerResult luisResult, string entityKey)
    {
        if (luisResult != null)
        {
            var data = luisResult.Entities as IDictionary<string, JToken>;

            if (data.TryGetValue(entityKey, out JToken value))
            {
                // wuuuh?
                return value.First.First.Value<T>();
            }
        }

        return default(T);
    }
}

Hope it helps

Advertisements