获取LUIS的查询结果

Getting the query result of LUIS

我已经创建了一个 LUIS 帐户并完成了所需的一切。

我写了下面的代码,并从 LUIS 得到了结果。

我需要知道如何将我的查询结果保存到一个变量中,我想使用它来搜索数据库或网络。

下面是代码..

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());



        }

这里也是查询结果

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

现在我要保存这个

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

到一个变量。请指导我,因为我是 MS LUIS 的新手。

示例:

string result = "paris" /// 应该从 luis query

中获取值

string type = "city" /// 应该从 luis 查询中获取值

一种选择是将 Newtonsoft.Json NuGet 包引用到您的项目。

那你可以创建两个类(名字随意改)

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

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

    public string type { get; set; }
}

那么一个使用例子就是

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

然后通过以下方式检索请求的值:

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;
}