Exemplo n.º 1
0
        private DateTimeResolutionResult ParseEachUnit(string text)
        {
            var ret = new DateTimeResolutionResult();

            // handle "daily", "weekly"
            var match = this.config.PeriodicRegex.Match(text);

            if (match.Success)
            {
                // @TODO refactor to pass match
                if (!this.config.GetMatchedDailyTimex(text, out string timex))
                {
                    return(ret);
                }

                ret = SetHandler.ResolveSet(ref ret, timex);

                return(ret);
            }

            // handle "each month"
            var exactMatch = this.config.EachUnitRegex.MatchExact(text, trim: true);

            if (exactMatch.Success)
            {
                var sourceUnit = exactMatch.Groups["unit"].Value;
                if (string.IsNullOrEmpty(sourceUnit))
                {
                    sourceUnit = exactMatch.Groups["specialUnit"].Value;
                }

                if (!string.IsNullOrEmpty(sourceUnit) && this.config.UnitMap.ContainsKey(sourceUnit))
                {
                    // @TODO refactor to pass match
                    if (!this.config.GetMatchedUnitTimex(sourceUnit, out string timex))
                    {
                        return(ret);
                    }

                    // "handle every other month"
                    if (exactMatch.Groups["other"].Success)
                    {
                        timex = timex.Replace("1", "2");
                    }

                    ret = SetHandler.ResolveSet(ref ret, timex);
                }
            }

            return(ret);
        }
Exemplo n.º 2
0
        private DateTimeResolutionResult ParseEach(IDateTimeExtractor extractor, IDateTimeParser parser, string text, DateObject refDate)
        {
            var ret = new DateTimeResolutionResult();

            List <ExtractResult> ers = null;
            var success = false;

            // remove key words of set type from text
            var match = config.SetEachRegex.Match(text);

            if (match.Success)
            {
                var trimmedText = text.Remove(match.Index, match.Length);

                ers = extractor.Extract(trimmedText, refDate);
                if (ers.Count == 1 && ers.First().Length == trimmedText.Length)
                {
                    success = true;
                }
            }

            // remove suffix 's' and "on" if existed and re-try
            match = this.config.SetWeekDayRegex.Match(text);
            if (match.Success)
            {
                var trimmedText = text.Remove(match.Index, match.Length);

                trimmedText = trimmedText.Insert(match.Index, config.WeekDayGroupMatchString(match));

                ers = extractor.Extract(trimmedText, refDate);
                if (ers.Count == 1 && ers.First().Length == trimmedText.Length)
                {
                    success = true;
                }
            }

            if (success)
            {
                var pr = parser.Parse(ers[0], refDate);

                ret = SetHandler.ResolveSet(ref ret, pr.TimexStr);
            }

            return(ret);
        }
Exemplo n.º 3
0
        private DateTimeResolutionResult ParseEachUnit(string text)
        {
            var ret = new DateTimeResolutionResult();

            // handle "each month"
            var match = this.config.EachUnitRegex.MatchExact(text, trim: true);

            if (match.Success)
            {
                var sourceUnit = match.Groups["unit"].Value;
                if (!string.IsNullOrEmpty(sourceUnit) && this.config.UnitMap.ContainsKey(sourceUnit))
                {
                    if (this.config.GetMatchedUnitTimex(sourceUnit, out string timexStr))
                    {
                        ret = SetHandler.ResolveSet(ref ret, timexStr);
                    }
                }
            }

            return(ret);
        }
Exemplo n.º 4
0
        private DateTimeResolutionResult ParseEachDuration(string text, DateObject refDate)
        {
            var ret = new DateTimeResolutionResult();

            var ers = this.config.DurationExtractor.Extract(text, refDate);

            if (ers.Count != 1 || string.IsNullOrWhiteSpace(text.Substring(ers[0].Start + ers[0].Length ?? 0)))
            {
                return(ret);
            }

            var afterStr = text.Substring(ers[0].Start + ers[0].Length ?? 0);

            if (this.config.EachPrefixRegex.IsMatch(afterStr))
            {
                var pr = this.config.DurationParser.Parse(ers[0], DateObject.Now);
                ret = SetHandler.ResolveSet(ref ret, pr.TimexStr);
                return(ret);
            }

            return(ret);
        }
Exemplo n.º 5
0
        private DateTimeResolutionResult ParserTimeEveryday(string text, DateObject refDate)
        {
            var ret = new DateTimeResolutionResult();

            var ers = this.config.TimeExtractor.Extract(text, refDate);

            if (ers.Count != 1)
            {
                return(ret);
            }

            var afterStr = text.Replace(ers[0].Text, string.Empty);
            var match    = this.config.EachDayRegex.Match(afterStr);

            if (match.Success)
            {
                var pr = this.config.TimeParser.Parse(ers[0], DateObject.Now);

                ret = SetHandler.ResolveSet(ref ret, pr.TimexStr);
            }

            return(ret);
        }
Exemplo n.º 6
0
        private DateTimeResolutionResult ParseEach(IDateTimeExtractor extractor, IDateTimeParser parser, string text, DateObject refDate)
        {
            var ret     = new DateTimeResolutionResult();
            var ers     = extractor.Extract(text, refDate);
            var success = false;

            foreach (var er in ers)
            {
                var beforeStr = text.Substring(0, er.Start ?? 0);
                var match     = this.config.EachPrefixRegex.Match(beforeStr);

                if (match.Success && match.Length + er.Length == text.Length)
                {
                    success = true;
                }
                else if (er.Type == Constants.SYS_DATETIME_TIME || er.Type == Constants.SYS_DATETIME_DATE)
                {
                    // Cases like "every day at 2pm" or "every year on April 15th"
                    var eachRegex = er.Type == Constants.SYS_DATETIME_TIME ? this.config.EachDayRegex : this.config.EachDateUnitRegex;
                    match = eachRegex.Match(beforeStr);
                    if (match.Success && match.Length + er.Length == text.Length)
                    {
                        success = true;
                    }
                }

                if (success)
                {
                    var pr = parser.Parse(er, refDate);
                    ret = SetHandler.ResolveSet(ref ret, pr.TimexStr);
                    break;
                }
            }

            return(ret);
        }