ab2018
ab2018

Reputation: 21

Getting the query result of LUIS

I have created a LUIS account and did everything that was needed.

I have written the following code and got the result from LUIS.

I need to know how to save the result of my query to a variable, using which I would like to search the database or web.

Below is the code..

static async void MakeRequest(string qz) {

            var client = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);
            var luisAppId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            var endpointKey = "XXXXXXXXXXXX";

            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", endpointKey);

            // The "q" parameter contains the utterance to send to LUIS
            queryString["q"] = qz;

            // These optional request parameters are set to their default values
            queryString["timezoneOffset"] = "0";
            queryString["verbose"] = "false";
            queryString["spellCheck"] = "false";
            queryString["staging"] = "false";

            var endpointUri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;
            var response = await client.GetAsync(endpointUri.);

            var strResponseContent = await response.Content.ReadAsStringAsync();


            // Display the JSON result from LUIS
            Console.WriteLine(strResponseContent.ToString());



        }

And also here is the query result.

{
  "query": "the best resturant in Paris",
  "topScoringIntent": {
    "intent": "city",
    "score": 0.436210483
  },
  "entities": [
    {
      "entity": "paris",
      "type": "city",
      "startIndex": 22,
      "endIndex": 26,
      "score": 0.7153605
    }
  ]
}

Now I want to save this

"entity": "paris",
"type": "city",

to a variable. Kindly guide me as I am completely new to MS LUIS.

example:

string result = "paris" /// which the value should be taken from luis query

string type = "city" /// which the value should be taken from luis query

Upvotes: 2

Views: 897

Answers (1)

Ferdinand Fejskid
Ferdinand Fejskid

Reputation: 537

One option is to reference Newtonsoft.Json NuGet package to your project.

Then you may create two classes (feel free to change the name)

public class LuisExtractionLuisResult
{
    public List<LuisEntity> entities { get; set; }
}

public class LuisEntity
{
    public string entity { get; set; }

    public string type { get; set; }
}

Then one example of use is

var target = JsonConvert.DeserializeObject<LuisExtractionLuisResult>(strResponseContent);

requested values are then retrieved by:

string result = target.entities[0].entity;
string type = target.entities[0].type;

And one more question, if in the query we have more than one entities. how to get that as well?

foreach(LuisEntity oneEntity in target.entities)
{
    string result oneEntity.entity;
    string type = oneEntity.type;
}

Upvotes: 2

Related Questions