Search code examples
c#.netbotframeworkbotsazure-language-understanding

How can I get the value of an entity from LuisResult object? (LUIS with Bot Framework .NET)


Below is the JSON when I call my LUIS api endpoint.

  {
        "query": "How do I install citrix?",
        "topScoringIntent": {
            "intent": "Setup Instructions",
            "score": 0.9999997
        },
        "intents": [
            {
                "intent": "Setup Instructions",
                "score": 0.9999997
            },
            {
                "intent": "OS Availability",
                "score": 0.0000021111066
            },
            {
                "intent": "Service Guide",
                "score": 8.18181149e-7
            },
            {
                "intent": "Service Description",
                "score": 5.55555232e-7
            },
            {
                "intent": "None",
                "score": 9e-9
            },
            {
                "intent": "Greeting",
                "score": 1.41666667e-9
            },
            {
                "intent": "Compassion",
                "score": 8.1e-10
            },
            {
                "intent": "Images",
                "score": 8.1e-10
            }
        ],
        "entities": [
            {
                "entity": "citrix",
                "type": "Service",
                "startIndex": 17,
                "endIndex": 22,
                "resolution": {
                    "values": [
                        "Citrix Receiver"
                    ]
                },
                "role": ""
            }
        ],
        "sentimentAnalysis": {
            "label": "positive",
            "score": 0.7695234
        }
    }

I am trying to get the string "Citrix Receiver" from below.

Below is my code

LuisResult result
var strEntity = result.Entities[0].Resolution.Values[0]

but I cannot apply indexing to an expression of type ICollection<object>. It looks as though resolution is defined as a dictionary and upon researching, I have seen other JSON bodies with the resolution having multiple key value pairs. Is it possible the body has changed but the Luis extension in the MS Bot Builder Framework has not?

Thanks.


Solution

  • I had the same problem previously to get a list of resolved entities, I resolved it using the following code:

    result.Entities.First().Resolution.Values.Select(s => JArray.Parse(s.ToString()).Distinct().ToList();
    

    So for you it may be a bit shorter like:

    result.Entities.First().Resolution.Values.First(s => JArray.Parse(s.ToString());