コード例 #1
0
        public static DateTime? FindStartDate(this LuisResult result, UserState userState)
        {
            //
            // We special-case "now" since it most likely overrules anything else we might see.
            //
            if (result.IsStartTimeNow())
            {
                var localNow = userState.ConvertToLocalTime(DateTime.UtcNow);

                return localNow.Date;
            }

            TimeSpan maxDaysInFuture = TimeSpan.FromDays(30);   // Limit how far in the future we can recognize

            EntityRecommendation builtinDate = null;
            result.TryFindEntity(EntityBuiltinDate, out builtinDate);

            var parser = new Chronic.Parser(
                new Chronic.Options
                {
                    Context = Chronic.Pointer.Type.Future,
                    Clock = () =>
                    {
                        var utcNow = DateTime.UtcNow;
                        var tzOffset = userState.LocalOffsetForDate(utcNow);

                        return new DateTime((utcNow + tzOffset).Ticks, DateTimeKind.Unspecified);
                    }
                });

            if (builtinDate != null && builtinDate.Resolution.ContainsKey("date"))
            {
                //
                // Give DateTime a crack at parsing it first. This handles cases like MM/DD which Chronic
                // can't handle, for some reason.
                //
                DateTime date;

                if (DateTime.TryParse(builtinDate.Entity, out date))
                {
                    date = date.Date;
                    // Only accept dates in the reasonably near future.
                    if (date >= DateTime.Now.Date && date <= DateTime.Now.Date + maxDaysInFuture)
                    {
                        return date;
                    }
                }

                var span = parser.Parse(builtinDate.Entity);

                if (span != null)
                {
                    var when = span.Start ?? span.End;
                    return when.Value.Date;
                }
            }

            foreach (var startDate in result.Entities.Where(e => e.Type == EntityStart))
            {
                var span = parser.Parse(startDate.Entity);

                if (span != null)
                {
                    var when = span.Start ?? span.End;

                    if (when.Value.HasDate())
                    {
                        return when.Value.Date;
                    }
                }
            }

            return null;
        }
コード例 #2
0
        public static DateTime? FindStartTime(this LuisResult result, UserState userState)
        {
            //
            // We special-case "now" since it most likely overrules anything else we might see.
            // We snap the time to the "best" nearby time-slot (15-minute intervals).
            //
            if (result.IsStartTimeNow())
            {
                return userState.LocalTime().ToNearestTimeSlot();
            }

            EntityRecommendation builtinTime = null;
            result.TryFindEntity(EntityBuiltinTime, out builtinTime);

            var parser = new Chronic.Parser(
                new Chronic.Options
                {
                    Context = Chronic.Pointer.Type.Future,
                    Clock = () =>
                    {
                        var utcNow = DateTime.UtcNow;
                        var tzOffset = userState.LocalOffsetForDate(utcNow);

                        return new DateTime((utcNow + tzOffset).Ticks, DateTimeKind.Unspecified);
                    }
                });

            if (builtinTime != null && builtinTime.Resolution.ContainsKey("time"))
            {
                var span = parser.Parse(builtinTime.Entity);

                if (span != null)
                {
                    var when = span.Start ?? span.End;

                    if (when.Value.HasTime())
                    {
                        return when.Value;
                    }
                }
            }

            foreach (var startDate in result.Entities.Where(e => e.Type == EntityStart))
            {
                var span = parser.Parse(startDate.Entity.Replace("at ", string.Empty));

                if (span != null)
                {
                    var when = span.Start ?? span.End;

                    if (when.Value.HasTime())
                    {
                        return DateTime.MinValue + when.Value.TimeOfDay;
                    }
                }
            }

            return null;
        }