【问题标题】:Getting LUIS DateTime Entity into FormFlow将 LUIS DateTime 实体放入 FormFlow
【发布时间】:2016-08-15 19:06:34
【问题描述】:

我有一个 C# 聊天机器人,它接收用户消息,并使用 LUIS 决定用户的意图。当找到特定意图时,它会启动 FormFlow。我已经能够使用 LUIS 实体从用户初始消息中成功填写表单中的字段。但是我被困在日期和时间实体上。当 LUIS 提供实体时,它将它们作为 2 个单独的实体(builtin.datetime.time 和 builtin.datetime.time)发送,但我需要将这些保存在一个表单字段 (DateTime) 下。 如何将实体时间和日期保存到日期时间字段?

我目前只知道如何只保存一个字段(保存时间并默认为今天的日期,或保存日期并默认为上午 12 点)。

这是我目前将日期实体保存到表单字段的方式

        EntityRecommendation entityDateTime;
        result.TryFindEntity("builtin.datetime.date", out entityDateTime);
        if (entityDateTime != null)
            entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityDateTime.Entity });

【问题讨论】:

    标签: c# datetime botframework azure-language-understanding formflow


    【解决方案1】:

    您可以使用Chronic Parser(在官方botbuilder github源代码中的一些示例中使用它)

    网址:https://github.com/robertwilczynski/nChronic

    要将日期和时间合并为一个日期时间实体,请参见下面的示例代码

    EntityRecommendation time;
    EntityRecommendation date;
    
    var timeFound = result.TryFindEntity(EntityConstant.EntityBuiltInTime, out time);
    if (result.TryFindEntity(EntityConstant.EntityBuiltInDate, out date))
    {
        return timeFound ? (date.Entity + " " + time.Entity).Parse() : date.Entity.Parse();
    }
    

    ChronicParserExtension.cs

    public static Tuple<DateTime, DateTime> Parse(this string input)
    {
        var parser = new Parser(new Options
        {
            FirstDayOfWeek = DayOfWeek.Monday
        });
    
        var span = parser.Parse(input);
    
        if (span.Start != null && span.End != null)
        {
            return Tuple.Create(span.Start.Value, span.End.Value);        
        }
    
        return null;
    }
    

    希望对你有帮助。

    【讨论】:

    • 找不到 Parse 的定义,我应该使用特定指令吗?
    • 您是否将nChronic 添加到您的项目中?你也需要检查导入
    • 我在想 BotBuilder V3.0 模板已经在项目中包含了 Chronic
    • @Wolfer:我只是想确认一下,请尝试var test = new Chronic.Parser().Parse("test");看看是否有任何编译错误。
    • @Wolfer:你添加了 ChronicParserExtension 类吗?
    【解决方案2】:

    非常感谢@kienct89,因为他帮助我解决了问题,但是我不需要使用 Chronic。 我用下面的代码得到了我想要的结果,如果有更好的方法来写这个,很高兴为 cmets 提供字段

            EntityRecommendation entityDate;
            EntityRecommendation entityTime;
            result.TryFindEntity("builtin.datetime.date", out entityDate);
            result.TryFindEntity("builtin.datetime.time", out entityTime);
            if ((entityDate != null) & (entityTime != null))
                entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityDate.Entity + " " + entityTime.Entity });
    
            else if (entityDate != null)
                entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityDate.Entity });
    
            else if (entityTime != null)
                entities.Add(new EntityRecommendation(type: "DateTime") { Entity = entityTime.Entity });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多