c# - 使用预建实体 datetimeV2 捕获日期范围

标签 c# botframework

我正在使用预建实体 datetimeV2 来捕获日期范围。我用它来将它映射到两个不同的话语——本周,特别是两个日期之间。如何将其映射到 FormFlow 模型中?

我想做的是这个-

Search for all flights this week

Search for all flights between today and 7/10/2017

LUIS 实体已准确捕获,但无法将其映射到 FormFlow 模型 - 我的模型包含 fromDate 和 ToDate。如何使用这些属性映射单个匹配的 Luis 实体?

最佳答案

下面的代码说明了我为此提出的最佳解决方案。我仍在寻找更好的解决方案,并希望找到一个。您已将日期时间识别器 nuget 包添加到您的解决方案并添加适当的 using 语句。

using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.DateTime;

请注意,turnContext.Activity.Text 包含 Luis 正在解释的文本,即。 “上周”。

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

我在以下位置发布了此问题的更具体版本:Retrieve complete LUIS DateTimeV2 entity from recognizer

关于c# - 使用预建实体 datetimeV2 捕获日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44954515/

相关文章:

c# - 错误 (502) 网关错误 : When trying to access chatbot deployed on Azure using WebChat channel

botframework - LUIS 使用状态

c# - 在 Microsoft botframework 中回复 "Is Typing"消息

c# - 无法将类型 'Int' 隐式转换为 'T'

c# - SQL Server : INNER JOIN writes out things two times

c# - 为什么将 null 转换为类型以进行方法选择?

javascript - 管理长时间运行的操作node.js

azure - 如何在部署机器人时测试 Azure 插槽

c# - StreamReader.Read 和 StreamReader.ReadBlock 之间的区别

c# - 绑定(bind)到 Nullable<DateTime> 控件属性