我正在使用Microsoft Bot Framework v4进行一些相当基本的日期和日期范围识别。
大多数示例在企业模板中使用并使用的LUIS识别器的最直接使用不会返回完整的DateTimeV2结构。下面的示例适用于与日期范围相对应的“上周”。这些结果显示在identifierr.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
如果使用特定的DateTime解析器,则可以获得所需的更详细的开始和结束信息。
[timex, 2018-W43]
[type, daterange]
[start, 2018-10-22]
[end, 2018-10-29]
这些步骤似乎几乎是多余的,因为更通用的方法返回Timex格式的字符串,该字符串包含开始和结束信息,但不是易于使用的格式。
我相信我缺少有关配置LUIS和识别器的基本知识。
下面的代码段是csharp_dotnetcore / 12.nlp-with-luis示例(https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/12.nlp-with-luis)的修改版本
我在luis.ai中添加了一个预建的DateTimeV2实体。
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实体?可能链接识别器?
答案 0 :(得分:0)
为了查看整个API结果,在创建Luis Recognizer时需要设置一个选项。 includeApiResults默认为false,因此将该参数设置为true会使整个API结果包含在luis结果中。
因此,在BotServices.cs中创建Luis服务时,它将如下所示:
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响应。
{{
"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
}
}}