Example #1
0
        public DateTimeParseResult Parse(ExtractResult er, DateObject refDate)
        {
            var referenceTime = refDate;
            var extra         = er.Data as DateTimeExtra <TimeType>;

            if (extra == null)
            {
                var result = TimeExtractor.Extract(er.Text, refDate);
                extra = result[0]?.Data as DateTimeExtra <TimeType>;
            }

            if (extra != null)
            {
                var timeResult  = FunctionMap[extra.Type](extra);
                var parseResult = TimeFunctions.PackTimeResult(extra, timeResult, referenceTime);
                if (parseResult.Success)
                {
                    parseResult.FutureResolution = new Dictionary <string, string>
                    {
                        { TimeTypeConstants.TIME, DateTimeFormatUtil.FormatTime((DateObject)parseResult.FutureValue) }
                    };

                    parseResult.PastResolution = new Dictionary <string, string>
                    {
                        { TimeTypeConstants.TIME, DateTimeFormatUtil.FormatTime((DateObject)parseResult.PastValue) }
                    };
                }

                var ret = new DateTimeParseResult
                {
                    Start         = er.Start,
                    Text          = er.Text,
                    Type          = er.Type,
                    Length        = er.Length,
                    Value         = parseResult,
                    Data          = timeResult,
                    ResolutionStr = "",
                    TimexStr      = parseResult.Timex
                };

                return(ret);
            }

            return(null);
        }
        public static DateTimeResolutionResult Handle(IDateTimeParser timeParser, DateTimeExtra <PeriodType> extra, DateObject refTime)
        {
            //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 = TimeFunctions.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);
        }