如何从返回RecognizerResult的luis.ai RecognizerAsync方法访问实体得分信息和/或存在情况 [英] How can I access an entities score information and or existence from the luis.ai RecognizerAsync method which returns the RecognizerResult

查看:121
本文介绍了如何从返回RecognizerResult的luis.ai RecognizerAsync方法访问实体得分信息和/或存在情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过azure构建的示例bot具有本文档的基础

The sample bot built through azure has a basis from this documentation

我正在尝试重构对话框的部分功能,我需要访问我认为应该从luis调用返回的json的某些部分.但是,当我尝试从$ instance访问时,出现错误,我无法访问响应的子元素.

I am trying to refactor parts of the functionality of the dialog and I need access to parts of the json that I assume should come back from the luis call. However, when I try to access from $instance I get an error that I cannot access child elements of the response.

这是json的范例:

+       Entities    {{
  "$instance": {
    "To": [
      {
        "startIndex": 10,
        "endIndex": 15,
        "text": "paris",
        "type": "To",
        "score": 0.987954
      }
    ]
  },
  "To": [
    {
      "$instance": {
        "Airport": [
          {
            "startIndex": 10,
            "endIndex": 15,
            "text": "paris",
            "type": "Airport"
          }
        ]
      },
      "Airport": [
        [
          "Paris"
        ]
      ]
    }
  ]
}}  Newtonsoft.Json.Linq.JObject

以下是访问json的代码:

Here is the code accessing the json:

bookingDetails.Origin = recognizerResult.Entities["From"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();

我了解这是如何通过luis.ai调用访问Entity的,但是如何访问json响应的其他部分?

I understand how this accesses the Entity from luis.ai call but how can I access the other parts of the json response?

同样,是否有一种方法可以访问实体是否出于一般目的而返回,例如布尔值响应?

As well, is there a way to access whether or not an entity came back for the intent in general such as a bool valued response?

最后,通过上述方法访问json似乎并不理想.是否有一种更格式化的方法来访问json返回,并且使用新的asp.net core 2+,有没有使用newtonsoft的方法?

Lastly, accessing the json in general via the above method seems not ideal. Is there a more formatted way to access what would be the json return and with the new asp.net core 2+ is there a way to do it without using newtonsoft?

推荐答案

您正确地认为,通过上述方法访问JSON是不理想的.我会使用 QuickType 这样的工具来创建类JSON,或者至少您可以调整的类的基础.然后,您可以使用NewtonSoft JSON库(或其他JSON库,因为您不想使用NewtonSoft)来将JSON反序列化到对象中,并访问该对象的Score属性.

You are correct that accessing the JSON via the above method is not ideal. What I would do it use a tool such as QuickType to create a class from JSON, or at least the basis of a class that you can tweak. You can then use the NewtonSoft JSON library (or an alternative JSON library since you don't want to use NewtonSoft) to Deserialize the JSON into an object and access the Score property of the object.

或者,如您所链接的指南中所示,您可以从GetTopScoringIntent方法调用中获得分数.

Alternatively, as shown in the guide that you linked you can obtain the score from the GetTopScoringIntent method call.

关于检查是否返回了任何实体,您可以插入 LUIS API参考页创建到一个类中,然后检查Entities集合以查看它是否有任何项目-您可以使用LINQs .Any()方法.

Regarding checking if any entities were returned you can plug the JSON shown on the LUIS API reference page under the 200 response into the quicktype tool I linked above to create a class then check against the Entities collection to see if it has any items - you could use LINQs .Any() method for this.

修改

OP最终使用 LUISGen 输出一个类,以反序列化来自LUIS API的响应.

The OP ended up using LUISGen to output a class to deserialize the response from the LUIS API.

  1. 转到LUIS门户.
  2. 导航到相关的LUIS应用程序.
  3. 转到管理"标签.
  4. 在左侧,选择版本"项.
  5. 选中要导出的版本旁边的框.
  6. 从下拉列表中,选择导出为JSON".
  7. 将JSON保存到项目的根目录中.
  8. 在项目的根目录中打开一个命令提示符.
  9. 运行以下命令:

dotnet tool install -g LUISGen

LUISGen <exported-luis-app>.json -cs ClassToGenerate -o

  1. 将生成的文件添加到Visual Studio中的项目.
    • 右键单击项目>添加现有项
  1. Add the generated file to your project in Visual Studio.
    • Right click on the project > Add existing item

var result = recognizer.Recognize<ClassToGenerate>("<user-input-text>", CancellationToken.None);

这篇关于如何从返回RecognizerResult的luis.ai RecognizerAsync方法访问实体得分信息和/或存在情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆