Exemplo n.º 1
0
        public DateTimeResolutionResult PackTimeResult(DateTimeExtra <TimeType> extra, TimeResult timeResult, DateObject referenceTime)
        {
            // Find if there is a description
            var noDesc  = true;
            var dayDesc = extra.NamedEntity["daydesc"]?.Value;

            if (!string.IsNullOrEmpty(dayDesc))
            {
                AddDesc(timeResult, dayDesc);
                noDesc = false;
            }

            int hour   = timeResult.Hour > 0 ? timeResult.Hour % Constants.DayHourCount : 0,
                min    = timeResult.Minute > 0 ? timeResult.Minute : 0,
                second = timeResult.Second > 0 ? timeResult.Second : 0,
                day    = referenceTime.Day,
                month  = referenceTime.Month,
                year   = referenceTime.Year;

            var dateTimeResult = new DateTimeResolutionResult();

            var build = new StringBuilder("T");

            if (timeResult.Hour >= 0)
            {
                build.Append(hour.ToString("D2", CultureInfo.InvariantCulture));
            }

            if (timeResult.Minute >= 0)
            {
                build.Append(":" + min.ToString("D2", CultureInfo.InvariantCulture));
            }

            if (timeResult.Second >= 0)
            {
                if (timeResult.Minute < 0)
                {
                    build.Append(":" + min.ToString("D2", CultureInfo.InvariantCulture));
                }

                build.Append(":" + second.ToString("D2", CultureInfo.InvariantCulture));
            }

            if (noDesc && hour <= Constants.HalfDayHourCount)
            {
                // build.Append("ampm");
                dateTimeResult.Comment = Constants.Comment_AmPm;
            }

            dateTimeResult.Timex = build.ToString();

            dateTimeResult.FutureValue = dateTimeResult.PastValue =
                DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second);
            dateTimeResult.Success = true;

            return(dateTimeResult);
        }
Exemplo n.º 2
0
        public TimeResult HandleDigit(DateTimeExtra <TimeType> extra)
        {
            var timeResult = new TimeResult
            {
                Hour   = MatchToValue(extra.NamedEntity[Constants.HourGroupName].Value),
                Minute = MatchToValue(extra.NamedEntity[Constants.MinuteGroupName].Value),
                Second = MatchToValue(extra.NamedEntity[Constants.SecondGroupName].Value),
            };

            return(timeResult);
        }
Exemplo n.º 3
0
        public TimeResult HandleKanji(DateTimeExtra <TimeType> extra)
        {
            var hour    = MatchToValue(extra.NamedEntity[Constants.HourGroupName].Value);
            var quarter = MatchToValue(extra.NamedEntity["quarter"].Value);
            var minute  = MatchToValue(extra.NamedEntity[Constants.MinuteGroupName].Value);
            var second  = MatchToValue(extra.NamedEntity[Constants.SecondGroupName].Value);

            minute = !string.IsNullOrEmpty(extra.NamedEntity["half"].Value) ? 30 : quarter != -1 ? quarter * 15 : minute;

            return(new TimeResult
            {
                Hour = hour,
                Minute = minute,
                Second = second,
            });
        }
Exemplo n.º 4
0
        public TimeResult HandleLess(DateTimeExtra <TimeType> extra)
        {
            var hour    = MatchToValue(extra.NamedEntity[Constants.HourGroupName].Value);
            var quarter = MatchToValue(extra.NamedEntity["quarter"].Value);
            var minute  = !string.IsNullOrEmpty(extra.NamedEntity["half"].Value) ? 30 : quarter != -1 ? quarter * 15 : 0;
            var second  = MatchToValue(extra.NamedEntity[Constants.SecondGroupName].Value);
            var less    = MatchToValue(extra.NamedEntity[Constants.MinuteGroupName].Value);

            var all = (hour * 60) + minute - less;

            if (all < 0)
            {
                all += 1440;
            }

            return(new TimeResult
            {
                Hour = all / 60,
                Minute = all % 60,
                Second = second,
            });
        }
Exemplo n.º 5
0
        public DateTimeResolutionResult PackTimeResult(DateTimeExtra <TimeType> extra, TimeResult timeResult, DateObject referenceTime)
        {
            // Find if there is a description
            var noDesc  = true;
            var dayDesc = extra.NamedEntity["daydesc"]?.Value;

            if (!string.IsNullOrEmpty(dayDesc))
            {
                AddDesc(timeResult, dayDesc);
                noDesc = false;
            }

            // Hours > 24 (e.g. 25時 which resolves to the next day) are kept unnormalized in the timex
            // to avoid ambiguity in other entities. For example, "on the 30th at 25" is resolved to
            // "XXXX-XX-30T25" because with "XXXX-XX-30+1T01" it is not known if the day should be "31" or "01".
            int hour   = timeResult.Hour > 0 && timeResult.Hour != Constants.DayHourCount ? timeResult.Hour : 0,
                min    = timeResult.Minute > 0 ? timeResult.Minute : 0,
                second = timeResult.Second > 0 ? timeResult.Second : 0;

            var dateTimeResult = new DateTimeResolutionResult();

            var build = new StringBuilder("T");

            if (timeResult.Hour >= 0)
            {
                build.Append(hour.ToString("D2", CultureInfo.InvariantCulture));
            }

            if (timeResult.Minute >= 0)
            {
                build.Append(":" + min.ToString("D2", CultureInfo.InvariantCulture));
            }

            if (timeResult.Second >= 0)
            {
                if (timeResult.Minute < 0)
                {
                    build.Append(":" + min.ToString("D2", CultureInfo.InvariantCulture));
                }

                build.Append(":" + second.ToString("D2", CultureInfo.InvariantCulture));
            }

            // handle cases with time like 25時 (the hour is normalized in the past/future values)
            if (timeResult.Hour > Constants.DayHourCount)
            {
                hour          = timeResult.Hour - Constants.DayHourCount;
                referenceTime = referenceTime.AddDays(1);
                if (noDesc)
                {
                    dateTimeResult.Comment = Constants.Comment_Am;
                    noDesc = false;
                }
            }

            if (noDesc && hour <= Constants.HalfDayHourCount)
            {
                // build.Append("ampm");
                dateTimeResult.Comment = Constants.Comment_AmPm;
            }

            dateTimeResult.Timex = build.ToString();

            int day = referenceTime.Day, month = referenceTime.Month, year = referenceTime.Year;

            dateTimeResult.FutureValue = dateTimeResult.PastValue =
                DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second);
            dateTimeResult.Success = true;

            return(dateTimeResult);
        }
Exemplo n.º 6
0
        public static DateTimeResolutionResult Handle(IDateTimeParser timeParser, DateTimeExtra <PeriodType> extra, DateObject refTime, TimeFunctions timeFunc)
        {
            // Left is a time
            var        left = extra.NamedEntity["left"];
            TimeResult leftResult, rightResult = null;

            // 下午四点十分到五点十分
            if (extra.Type == PeriodType.FullTime)
            {
                var leftExtract = new ExtractResult
                {
                    Start  = left.Index,
                    Length = left.Length,
                    Text   = left.Value,
                    Type   = Constants.SYS_DATETIME_TIME,
                };
                leftResult = timeParser.Parse(leftExtract, refTime).Data as TimeResult;
            }
            else
            {
                // 下午四到五点
                leftResult = timeFunc.GetShortLeft(left.Value);
            }

            // Right is a time
            var right        = extra.NamedEntity["right"];
            var rightExtract = new ExtractResult
            {
                Start  = right.Index,
                Length = right.Length,
                Text   = right.Value,
                Type   = Constants.SYS_DATETIME_TIME,
            };

            rightResult = timeParser.Parse(rightExtract, refTime).Data as TimeResult;

            var ret = new DateTimeResolutionResult()
            {
                Success = true,
            };

            // the right side doesn't contain desc while the left side does
            if (rightResult.LowBound == -1 && leftResult.LowBound != -1 && rightResult.Hour <= leftResult.LowBound)
            {
                rightResult.Hour += Constants.HalfDayHourCount;
            }

            int day   = refTime.Day,
                month = refTime.Month,
                year  = refTime.Year;

            // determine if the right side time is smaller than the left side, if yes, add one day
            int hour   = leftResult.Hour > 0 ? leftResult.Hour : 0,
                min    = leftResult.Minute > 0 ? leftResult.Minute : 0,
                second = leftResult.Second > 0 ? leftResult.Second : 0;

            var leftTime = DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second);

            hour   = rightResult.Hour > 0 ? rightResult.Hour : 0;
            min    = rightResult.Minute > 0 ? rightResult.Minute : 0;
            second = rightResult.Second > 0 ? rightResult.Second : 0;

            var rightTime = DateObject.MinValue.SafeCreateFromValue(year, month, day, hour, min, second);

            if (rightTime.Hour < leftTime.Hour)
            {
                rightTime = rightTime.AddDays(1);
            }

            ret.FutureValue = ret.PastValue = new Tuple <DateObject, DateObject>(leftTime, rightTime);

            var leftTimex  = BuildTimex(leftResult);
            var rightTimex = BuildTimex(rightResult);

            ret.Timex = $"({leftTimex},{rightTimex},{BuildSpan(leftResult, rightResult)})";
            return(ret);
        }