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

            if (extra == null)
            {
                var result = new TimeExtractorChs().Extract(er.Text);
                extra = result[0]?.Data as DateTimeExtra <PeriodType>;
            }

            if (extra != null)
            {
                var parseResult = TimePeriodFunctions.Handle(this.config.TimeParser, extra, referenceTime);
                if (parseResult.Success)
                {
                    parseResult.FutureResolution = new Dictionary <string, string>
                    {
                        {
                            TimeTypeConstants.START_TIME,
                            FormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.FutureValue).Item1)
                        },
                        {
                            TimeTypeConstants.END_TIME,
                            FormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.FutureValue).Item2)
                        }
                    };

                    parseResult.PastResolution = new Dictionary <string, string>
                    {
                        {
                            TimeTypeConstants.START_TIME,
                            FormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.PastValue).Item1)
                        },
                        {
                            TimeTypeConstants.END_TIME,
                            FormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.PastValue).Item2)
                        }
                    };
                }

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

                return(ret);
            }

            return(null);
        }
        public DateTimeParseResult Parse(ExtractResult er, DateObject refDate)
        {
            var referenceTime = refDate;
            var extra         = er.Data as DateTimeExtra <PeriodType>;

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

            if (extra != null)
            {
                // Handle special case like '上午', '下午'
                var parseResult = ParseChineseTimeOfDay(er.Text, referenceTime);

                if (!parseResult.Success)
                {
                    parseResult = TimePeriodFunctions.Handle(this.config.TimeParser, extra, referenceTime, timeFunc);
                }

                if (parseResult.Success)
                {
                    parseResult.FutureResolution = new Dictionary <string, string>
                    {
                        {
                            TimeTypeConstants.START_TIME,
                            DateTimeFormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.FutureValue).Item1)
                        },
                        {
                            TimeTypeConstants.END_TIME,
                            DateTimeFormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.FutureValue).Item2)
                        },
                    };

                    parseResult.PastResolution = new Dictionary <string, string>
                    {
                        {
                            TimeTypeConstants.START_TIME,
                            DateTimeFormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.PastValue).Item1)
                        },
                        {
                            TimeTypeConstants.END_TIME,
                            DateTimeFormatUtil.FormatTime(((Tuple <DateObject, DateObject>)parseResult.PastValue).Item2)
                        },
                    };
                }

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

                return(ret);
            }

            return(null);
        }