【问题标题】:Getting the query result of LUIS获取LUIS的查询结果
【发布时间】:2018-08-31 08:32:12
【问题描述】:

我创建了一个 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 查询中获取哪个值

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

【问题讨论】:

  • 只想将结果保存在变量中。

标签: c# json azure-language-understanding azure-cognitive-services


【解决方案1】:

一种选择是将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;

还有一个问题,如果在查询中我们有多个实体。 如何获得它?

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

【讨论】:

  • 谢谢,很好的答案,这正是我想要的。
  • 还有一个问题,如果在查询中我们有多个实体。如何获得它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 2018-07-29
  • 2017-11-19
相关资源
最近更新 更多