示例#1
0
 private Crime CrimeInfo(Crime crime)
 {
     crime.Type        = Input("Type:");
     crime.Time        = _dateParser.Parse("DateTime format (DD-MM-YYYY HH:MM:SS) \nDate and time:");
     crime.Location    = Input("Location:");
     crime.Description = Input("Description:");
     return(crime);
 }
示例#2
0
        public void BasicTest(string text, string type)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], refrenceDate);

            Assert.AreEqual(type, pr.Type.Replace("datetimeV2.", ""));
        }
示例#3
0
        public void BasicTest(string text, DateObject date)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0]);

            Assert.AreEqual(Constants.SYS_DATETIME_TIME, pr.Type);
            Assert.AreEqual(date, ((DTParseResult)pr.Value).FutureValue);
        }
示例#4
0
        public void BasicTest(string text, string type)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], referenceDate);

            Assert.AreEqual(type, pr.Type.Replace("datetimeV2.", ""));
            TestWriter.Write(TestCulture.English, parser, referenceDate, text, pr);
        }
示例#5
0
        public void BasicTest(string text, DateObject futureDate, DateObject pastDate)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], refrenceDay);

            Assert.AreEqual(Constants.SYS_DATETIME_DATE, pr.Type);
            Assert.AreEqual(futureDate, ((DateTimeResolutionResult)pr.Value).FutureValue);
            Assert.AreEqual(pastDate, ((DateTimeResolutionResult)pr.Value).PastValue);
        }
示例#6
0
        public void BasicTest(string text, DateObject date)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0]);

            Assert.AreEqual(Constants.SYS_DATETIME_TIME, pr.Type);
            Assert.AreEqual(date, ((DateTimeResolutionResult)pr.Value).FutureValue);
            TestWriter.Write(TestCulture.English, parser, text, pr);
        }
示例#7
0
        public void BasicTest(string text, DateObject beginDate, DateObject endDate)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], referenceTime);

            Assert.AreEqual(Constants.SYS_DATETIME_TIMEPERIOD, pr.Type);
            Assert.AreEqual(beginDate, ((Tuple <DateObject, DateObject>)((DateTimeResolutionResult)pr.Value).FutureValue).Item1);
            Assert.AreEqual(endDate, ((Tuple <DateObject, DateObject>)((DateTimeResolutionResult)pr.Value).FutureValue).Item2);
        }
        public void BasicTestFuture(string text, int beginDay, int endDay, int month, int year)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], referenceDay);

            Assert.AreEqual(Constants.SYS_DATETIME_DATEPERIOD, pr.Type);
            var beginDate = new DateObject(year, month, beginDay);

            Assert.AreEqual(beginDate,
                            ((Tuple <DateObject, DateObject>)((DTParseResult)pr.Value).FutureValue).Item1);
            var endDate = new DateObject(year, month, endDay);

            Assert.AreEqual(endDate,
                            ((Tuple <DateObject, DateObject>)((DTParseResult)pr.Value).FutureValue).Item2);
        }
示例#9
0
        public static DateTimeResolutionResult ParseDurationWithAgoAndLater(string text,
                                                                            DateObject referenceTime,
                                                                            IExtractor durationExtractor,
                                                                            IDateTimeParser durationParser,
                                                                            IImmutableDictionary <string, string> unitMap,
                                                                            Regex unitRegex,
                                                                            IDateTimeUtilityConfiguration utilityConfiguration,
                                                                            AgoLaterMode mode)
        {
            var ret         = new DateTimeResolutionResult();
            var durationRes = durationExtractor.Extract(text);

            if (durationRes.Count > 0)
            {
                var pr    = durationParser.Parse(durationRes[0], referenceTime);
                var match = unitRegex.Match(text);
                if (match.Success)
                {
                    var afterStr =
                        text.Substring((int)durationRes[0].Start + (int)durationRes[0].Length)
                        .Trim()
                        .ToLowerInvariant();
                    var beforeStr =
                        text.Substring(0, (int)durationRes[0].Start)
                        .Trim()
                        .ToLowerInvariant();
                    var srcUnit = match.Groups["unit"].Value.ToLowerInvariant();

                    if (pr.Value != null)
                    {
                        var durationResult = (DateTimeResolutionResult)pr.Value;
                        var numStr         = durationResult.Timex.Substring(0, durationResult.Timex.Length - 1)
                                             .Replace("P", "")
                                             .Replace("T", "");

                        double number = 0;
                        if (double.TryParse(numStr, out number))
                        {
                            return(GetAgoLaterResult(
                                       pr,
                                       number,
                                       unitMap,
                                       srcUnit,
                                       afterStr,
                                       beforeStr,
                                       referenceTime,
                                       utilityConfiguration,
                                       mode));
                        }
                    }
                }
            }
            return(ret);
        }
示例#10
0
        public void BasicTest(string text, double value, string luisValue)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], new DateObject(2016, 11, 7));

            Assert.AreEqual(Constants.SYS_DATETIME_DURATION, pr.Type);
            Assert.AreEqual(value, ((DateTimeResolutionResult)pr.Value).FutureValue);
            Assert.AreEqual(luisValue, ((DateTimeResolutionResult)pr.Value).Timex);
        }
示例#11
0
        public void BasicTest(string text, string value, string luisValue)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], System.DateTime.Now);

            Assert.AreEqual(Constants.SYS_DATETIME_SET, pr.Type);
            Assert.AreEqual(value, ((DateTimeResolutionResult)pr.Value).FutureValue);
            Assert.AreEqual(luisValue, pr.TimexStr);
        }
示例#12
0
 private void Initialize(string scheduleString)
 {
     parser.Parse(scheduleString);
     Years        = sequence.GenerateSequence(parser.Year, MinYear, MaxYear);
     Months       = sequence.GenerateSequence(parser.Month, FirstMonth, LastMonth);
     DaysOfWeek   = sequence.GenerateSequence(parser.DayOfWeek, FirstDayOfWeek, LastDayOfWeek);
     Hours        = sequence.GenerateSequence(parser.Hour, MinHour, MaxHour);
     Minutes      = sequence.GenerateSequence(parser.Minute, MinMinute, MaxMinute);
     Seconds      = sequence.GenerateSequence(parser.Second, MinSecond, MaxSecond);
     Milliseconds = sequence.GenerateSequence(parser.Millisecond, MinMillisecond, MaxMillisecond);
 }
示例#13
0
        public void BasicTest(string text, double value, string luisValue)
        {
            var er = extractor.Extract(text);

            Assert.AreEqual(1, er.Count);
            var pr = parser.Parse(er[0], referenceDate);

            Assert.AreEqual(Constants.SYS_DATETIME_DURATION, pr.Type);
            Assert.AreEqual(value, ((DateTimeResolutionResult)pr.Value).FutureValue);
            Assert.AreEqual(luisValue, ((DateTimeResolutionResult)pr.Value).Timex);
            TestWriter.Write(TestCulture.English, parser, referenceDate, text, pr);
        }
示例#14
0
        public static DateTimeResolutionResult ParseDurationWithAgoAndLater(string text,
                                                                            DateObject referenceTime,
                                                                            IDateTimeExtractor durationExtractor,
                                                                            IDateTimeParser durationParser,
                                                                            IImmutableDictionary <string, string> unitMap,
                                                                            Regex unitRegex,
                                                                            IDateTimeUtilityConfiguration utilityConfiguration)
        {
            var ret         = new DateTimeResolutionResult();
            var durationRes = durationExtractor.Extract(text, referenceTime);

            if (durationRes.Count > 0)
            {
                var pr      = durationParser.Parse(durationRes[0], referenceTime);
                var matches = unitRegex.Matches(text);
                if (matches.Count > 0)
                {
                    var afterStr =
                        text.Substring((int)durationRes[0].Start + (int)durationRes[0].Length)
                        .Trim()
                        .ToLowerInvariant();
                    var beforeStr =
                        text.Substring(0, (int)durationRes[0].Start)
                        .Trim()
                        .ToLowerInvariant();

                    var mode = AgoLaterMode.Date;
                    if (pr.TimexStr.Contains("T"))
                    {
                        mode = AgoLaterMode.DateTime;
                    }

                    if (pr.Value != null)
                    {
                        return(GetAgoLaterResult(
                                   pr,
                                   afterStr,
                                   beforeStr,
                                   referenceTime,
                                   utilityConfiguration,
                                   mode));
                    }
                }
            }
            return(ret);
        }
示例#15
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 = ResolveSet(ref ret, pr.TimexStr);
            }

            return(ret);
        }
示例#16
0
        private DateTimeResolutionResult ParseEach(IExtractor extractor, IDateTimeParser parser, string text)
        {
            var ret = new DateTimeResolutionResult();
            List <ExtractResult> ers = null;
            // remove key words of set type from text
            var match   = config.SetEachRegex.Match(text);
            var success = false;

            if (match.Success)
            {
                var trimedText = text.Remove(match.Index, match.Length);
                ers = extractor.Extract(trimedText);
                if (ers.Count == 1 && ers.First().Length == trimedText.Length)
                {
                    success = true;
                }
            }

            // remove suffix 's' and "on" if existed and re-try
            match = this.config.SetWeekDayRegex.Match(text);
            if (match.Success)
            {
                var trimedText = text.Remove(match.Index, match.Length);
                trimedText = trimedText.Insert(match.Index, match.Groups["weekday"].ToString());
                ers        = extractor.Extract(trimedText);
                if (ers.Count == 1 && ers.First().Length == trimedText.Length)
                {
                    success = true;
                }
            }

            if (success)
            {
                var pr = parser.Parse(ers[0], DateObject.Now);
                ret.Timex       = pr.TimexStr;
                ret.FutureValue = ret.PastValue = "Set: " + ret.Timex;
                ret.Success     = true;
                return(ret);
            }

            return(ret);
        }
示例#17
0
        private DateTimeResolutionResult ParseEach(IExtractor extractor, IDateTimeParser parser, string text)
        {
            var ret = new DateTimeResolutionResult();
            var ers = extractor.Extract(text);

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

            var beforeStr = text.Substring(0, ers[0].Start ?? 0);
            var match     = this.config.EachPrefixRegex.Match(beforeStr);

            if (match.Success)
            {
                var pr = parser.Parse(ers[0], DateObject.Now);
                ret.Timex       = pr.TimexStr;
                ret.FutureValue = ret.PastValue = "Set: " + ret.Timex;
                ret.Success     = true;
                return(ret);
            }
            return(ret);
        }
示例#18
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);
        }
示例#19
0
        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);
        }
        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
            };

            //右侧没有Desc,左侧有
            if (rightResult.LowBound == -1 && leftResult.LowBound != -1 && rightResult.Hour <= leftResult.LowBound)
            {
                rightResult.Hour += 12;
            }
            int day   = refTime.Day,
                month = refTime.Month,
                year  = refTime.Year;
            //判断右侧是否比小测的小,如果比左侧的小,则增加一天
            int hour   = leftResult.Hour > 0 ? leftResult.Hour : 0,
                min    = leftResult.Minute > 0 ? leftResult.Minute : 0,
                second = leftResult.Second > 0 ? leftResult.Second : 0;

            var leftTime = new DateObject(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 = new DateObject(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);
        }