从识别器中检索完整的LUIS DateTimeV2实体 [英] Retrieve complete LUIS DateTimeV2 entity from recognizer

查看:85
本文介绍了从识别器中检索完整的LUIS DateTimeV2实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Bot Framework v4进行一些相当基本的日期和日期范围识别.

I'm using the Microsoft Bot Framework v4 to do some fairly basic date and date range recognition.

大多数示例在企业模板中使用并使用的LUIS识别器最直接的用法不会返回完整的DateTimeV2结构.下面的示例适用于与日期范围相对应的上周".这些结果存在于identifierr.result.entities中:

The most straightforward use of the LUIS recognizer that most examples use and is used in the enterprise template doesn't return the complete DateTimeV2 structure. The example below is for "last week" corresponding to a daterange. These results are in recognizer.result.entities:

{
  "$instance": {
    "datetime": [
      {
        "startIndex": 8,
        "endIndex": 17,
        "text": "last week",
        "type": "builtin.datetimeV2.daterange"
      }
    ]
  },
  "datetime": [
    {
      "type": "daterange",
      "timex": [
        "2018-W43"
      ]
    }
  ]
}

请注意,日期范围没有开始/结束标记.这似乎是DateTime2规范的不完整版本. https://docs.microsoft. com/en-us/azure/cognitive-services/LUIS/luis-reference-prebuilt-datetimev2

Notice there are no start / end tags for the date range. This seems like an incomplete version of the DateTime2 specification. https://docs.microsoft.com/en-us/azure/cognitive-services/LUIS/luis-reference-prebuilt-datetimev2

如果使用特定的DateTime解析器,则可以获得所需的更详细的开始和结束信息.

If I use the specific DateTime resolver, I get the more detailed start and end information that I want.

[timex, 2018-W43]
[type, daterange]
[start, 2018-10-22]
[end, 2018-10-29]

这些步骤似乎几乎是多余的,因为更通用的方法返回Timex格式的字符串,该字符串包含开始和结束信息,但不是易于使用的格式.

It seems like these steps are nearly redundant as the more general approach returns the Timex formatted string that contains start and end information but not in an easily used format.

我相信我缺少有关配置LUIS和识别器的基本知识.

I believe I'm missing something fundamental about configuring LUIS and recognizers.

下面的代码段是csharp_dotnetcore/12.nlp-with-luis示例的修改版本(

The code snippet below is modified version of the csharp_dotnetcore / 12.nlp-with-luis example (https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/12.nlp-with-luis)

我在luis.ai中添加了一个预建的DateTimeV2实体.

I added single prebuilt DateTimeV2 entity within luis.ai.

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    IList<Dictionary<string, string>> resolutionValues = null;

    if (turnContext.Activity.Type == ActivityTypes.Message)
    {

        // Use the more generic approach.
        var recognizerResult = await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
        var topIntent = recognizerResult?.GetTopScoringIntent();
        if (topIntent != null && topIntent.HasValue && topIntent.Value.intent != "None")
        {
            await turnContext.SendActivityAsync($"==>LUIS Top Scoring Intent: {topIntent.Value.intent}, Score: {topIntent.Value.score}\n");
            if (recognizerResult.Entities != null)
            {
                await turnContext.SendActivityAsync($"==>LUIS Entities Found: {recognizerResult.Entities.ToString()}\n");
            }
        }
        else
        {
            var msg = @"No LUIS intents were found. Try show me last week.";
            await turnContext.SendActivityAsync(msg);
        }

        // Check LUIS model using specific DateTime Recognizer.
        var culture = Culture.English;
        var r = DateTimeRecognizer.RecognizeDateTime(turnContext.Activity.Text, culture);
        if (r.Count > 0 && r.First().TypeName.StartsWith("datetimeV2"))
        {
            var first = r.First();
            resolutionValues = (IList<Dictionary<string, string>>)first.Resolution["values"];
            var asString = string.Join(";", resolutionValues[0]);
            await turnContext.SendActivityAsync($"==>LUIS: resolutions values: {asString}\n");
            var subType = first.TypeName.Split('.').Last();
            if (subType.Contains("date") && !subType.Contains("range"))
            {
                // a date (or date & time) or multiple
                var moment = resolutionValues.Select(v => DateTime.Parse(v["value"])).FirstOrDefault();
                await turnContext.SendActivityAsync($"==>LUIS DateTime Moment: {moment}\n");
            }
            else if (subType.Contains("date") && subType.Contains("range"))
            {
                // range
                var from = DateTime.Parse(resolutionValues.First()["start"]);
                var to = DateTime.Parse(resolutionValues.First()["end"]);
                await turnContext.SendActivityAsync($"==>LUIS DateTime Range: from: {from} to: {to}\n");
            }
        }
    }
    else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
    {
        // Send a welcome message to the user and tell them what actions they may perform to use this bot
        await SendWelcomeMessageAsync(turnContext, cancellationToken);
    }
    else
    {
        await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken: cancellationToken);
    }
}

是否有直接的方法可以从更通用的识别器中获取更完整的DateTimeV2实体?可能会链接识别器?

Is there a straightforward way to get the more complete DateTimeV2 entity out of the more general recognizer? Possibly chaining recognizers?

推荐答案

为了查看整个API结果,在创建Luis Recognizer时需要设置一个选项. includeApiResults默认为false,因此将该参数设置为true会使整个API结果包含在luis结果中.

In order to see the entire API result there is an option to set when creating the Luis Recognizer. includeApiResults defaults to false, so setting that parameter to true causes the entire API result to be included in the luis result.

因此,在BotServices.cs中创建Luis服务时,它将如下所示:

So, in BotServices.cs when creating the Luis Service it will look like this:

case ServiceTypes.Luis:
     {
        var luis = service as LuisService;
        var luisApp = new LuisApplication(luis.AppId, luis.SubscriptionKey, luis.GetEndpoint());
        LuisServices.Add(service.Id, new TelemetryLuisRecognizer(luisApp, includeApiResults:true));
        break;
    }

因此,现在luis对象中的属性既包含情感分析,也包含整个luis响应.

So now the properties in the luis object contains both the sentiment analysis and the entire luis response.

{{
  "query": "show me orders for last week",
  "alteredQuery": null,
  "topScoringIntent": {
    "intent": "Shopping.CheckOrderStatus",
    "score": 0.619710267
  },
  "intents": null,
  "entities": [
    {
      "entity": "last week",
      "type": "builtin.datetimeV2.daterange",
      "startIndex": 19,
      "endIndex": 27,
      "resolution": {
        "values": [
          {
            "timex": "2018-W43",
            "type": "daterange",
            "start": "2018-10-22",
            "end": "2018-10-29"
          }
        ]
      }
    }
  ],
  "compositeEntities": null,
  "sentimentAnalysis": {
    "label": "negative",
    "score": 0.241115928
  }
}}

这篇关于从识别器中检索完整的LUIS DateTimeV2实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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