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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s