diff --git a/.NET/Microsoft.Recognizers.Definitions/English/DateTimeDefinitions.cs b/.NET/Microsoft.Recognizers.Definitions/English/DateTimeDefinitions.cs index 9b656904e9..ee3668c706 100644 --- a/.NET/Microsoft.Recognizers.Definitions/English/DateTimeDefinitions.cs +++ b/.NET/Microsoft.Recognizers.Definitions/English/DateTimeDefinitions.cs @@ -124,7 +124,7 @@ public static class DateTimeDefinitions public const string MidafternoonRegex = @"(?midafternoon|mid-afternoon|mid afternoon)"; public const string MiddayRegex = @"(?midday|mid-day|mid day|((12\s)?noon))"; public static readonly string MidTimeRegex = $@"(?({MidnightRegex}|{MidmorningRegex}|{MidafternoonRegex}|{MiddayRegex}))"; - public static readonly string AtRegex = $@"\b(((?<=\bat\s+)({WrittenTimeRegex}|{HourNumRegex}|{BaseDateTime.HourRegex}|{MidTimeRegex}))|{MidTimeRegex})\b"; + public static readonly string AtRegex = $@"\b(((?<=\bat\s+)({WrittenTimeRegex}|{HourNumRegex}|{BaseDateTime.HourRegex}(\s*((?a)|(?p)))?|{MidTimeRegex}))|{MidTimeRegex})\b"; public static readonly string IshRegex = $@"\b({BaseDateTime.HourRegex}(-|——)?ish|noonish|noon)\b"; public const string TimeUnitRegex = @"([^A-Za-z]{1,}|\b)(?hours|hour|hrs|hr|h|minutes|minute|mins|min|seconds|second|secs|sec)\b"; public const string RestrictedTimeUnitRegex = @"(?hour|minute)\b"; diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimeParserChs.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimeParserChs.cs index e7431d5efe..6d115214d1 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimeParserChs.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimeParserChs.cs @@ -162,13 +162,13 @@ private DateTimeResolutionResult MergeDateAndTime(string text, DateObject refere var sec = time.Second; // handle morning, afternoon - if (SimplePmRegex.IsMatch(text) && hour < 12) + if (SimplePmRegex.IsMatch(text) && hour < Constants.HalfDayHourCount) { - hour += 12; + hour += Constants.HalfDayHourCount; } - else if (SimpleAmRegex.IsMatch(text) && hour >= 12) + else if (SimpleAmRegex.IsMatch(text) && hour >= Constants.HalfDayHourCount) { - hour -= 12; + hour -= Constants.HalfDayHourCount; } var timeStr = pr2.TimexStr; @@ -181,7 +181,7 @@ private DateTimeResolutionResult MergeDateAndTime(string text, DateObject refere var val = (DateTimeResolutionResult) pr2.Value; - if (hour <= 12 && !SimplePmRegex.IsMatch(text) && !SimpleAmRegex.IsMatch(text) && + if (hour <= Constants.HalfDayHourCount && !SimplePmRegex.IsMatch(text) && !SimpleAmRegex.IsMatch(text) && !string.IsNullOrEmpty(val.Comment)) { //ret.Timex += "ampm"; @@ -228,38 +228,38 @@ private DateTimeResolutionResult ParseTimeOfToday(string text, DateObject refere switch (matchStr) { case "今晚": - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - hour += 12; + hour += Constants.HalfDayHourCount; } break; case "今早": case "今晨": - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - hour -= 12; + hour -= Constants.HalfDayHourCount; } break; case "明晚": swift = 1; - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - hour += 12; + hour += Constants.HalfDayHourCount; } break; case "明早": case "明晨": swift = 1; - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - hour -= 12; + hour -= Constants.HalfDayHourCount; } break; case "昨晚": swift = -1; - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - hour += 12; + hour += Constants.HalfDayHourCount; } break; default: diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimePeriodParserChs.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimePeriodParserChs.cs index e9393fe34b..21b93b8bd5 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimePeriodParserChs.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/DateTimePeriodParserChs.cs @@ -299,7 +299,7 @@ private DateTimeResolutionResult MergeTwoTimePoints(string text, DateObject refe if (rightResult.Comment != null && rightResult.Comment.Equals(Constants.Comment_AmPm) && leftResult.Comment == null && rightTime < leftTime) { - rightTime = rightTime.AddHours(12); + rightTime = rightTime.AddHours(Constants.HalfDayHourCount); } if (rightTime < leftTime) @@ -356,7 +356,7 @@ private DateTimeResolutionResult ParseSpecificNight(string text, DateObject refe swift = 0; timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; break; case "明晚": swift = 1; @@ -369,7 +369,7 @@ private DateTimeResolutionResult ParseSpecificNight(string text, DateObject refe swift = 1; timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; break; case "昨晚": swift = -1; @@ -398,12 +398,12 @@ private DateTimeResolutionResult ParseSpecificNight(string text, DateObject refe { timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (AFRegex.IsMatch(trimedText)) { timeStr = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (EVRegex.IsMatch(trimedText)) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimeParserChs.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimeParserChs.cs index a663bf204b..594fbf0164 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimeParserChs.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimeParserChs.cs @@ -95,11 +95,11 @@ public static class TimeFunctions public static TimeResult HandleLess(DateTimeExtra extra) { - var hour = MatchToValue(extra.NamedEntity["hour"].Value); + 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["sec"].Value); - var less = MatchToValue(extra.NamedEntity["min"].Value); + 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) @@ -117,10 +117,10 @@ public static TimeResult HandleLess(DateTimeExtra extra) public static TimeResult HandleChinese(DateTimeExtra extra) { - var hour = MatchToValue(extra.NamedEntity["hour"].Value); + var hour = MatchToValue(extra.NamedEntity[Constants.HourGroupName].Value); var quarter = MatchToValue(extra.NamedEntity["quarter"].Value); - var minute = MatchToValue(extra.NamedEntity["min"].Value); - var second = MatchToValue(extra.NamedEntity["sec"].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 @@ -135,9 +135,9 @@ public static TimeResult HandleDigit(DateTimeExtra extra) { var timeResult = new TimeResult { - Hour = MatchToValue(extra.NamedEntity["hour"].Value), - Minute = MatchToValue(extra.NamedEntity["min"].Value), - Second = MatchToValue(extra.NamedEntity["sec"].Value) + Hour = MatchToValue(extra.NamedEntity[Constants.HourGroupName].Value), + Minute = MatchToValue(extra.NamedEntity[Constants.MinuteGroupName].Value), + Second = MatchToValue(extra.NamedEntity[Constants.SecondGroupName].Value) }; return timeResult; } @@ -241,7 +241,7 @@ public static void AddDesc(TimeResult result, string dayDesc) if (LowBoundDesc.ContainsKey(dayDesc) && result.Hour < LowBoundDesc[dayDesc]) { - result.Hour += 12; + result.Hour += Constants.HalfDayHourCount; result.LowBound = LowBoundDesc[dayDesc]; } else diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimePeriodParserChs.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimePeriodParserChs.cs index 630940929a..3dfa7e83e6 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimePeriodParserChs.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Chinese/Parsers/TimePeriodParserChs.cs @@ -129,7 +129,7 @@ public static DateTimeResolutionResult Handle(IDateTimeParser timeParser, DateTi //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 += 12; + rightResult.Hour += Constants.HalfDayHourCount; } int day = refTime.Day, diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Constants.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Constants.cs index 16c8bef1c0..096b1dbf78 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Constants.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Constants.cs @@ -104,6 +104,17 @@ public static class Constants public const string DefaultLanguageFallback_MDY = "MDY"; public const string DefaultLanguageFallback_DMY = "DMY"; - public const string NextGroupNameForWithinRegex = "next"; + // Groups' names for named groups in regexes + public const string NextGroupName = "next"; + public const string AmGroupName = "am"; + public const string PmGroupName = "pm"; + public const string ImplicitAmGroupName = "iam"; + public const string ImplicitPmGroupName = "ipm"; + public const string PrefixGroupName = "prefix"; + public const string SuffixGroupName = "suffix"; + public const string DescGroupName = "desc"; + public const string SecondGroupName = "sec"; + public const string MinuteGroupName = "min"; + public const string HourGroupName = "hour"; } } \ No newline at end of file diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimeParserConfiguration.cs index 13ffb8dbc6..62714d4bda 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimeParserConfiguration.cs @@ -1,153 +1,153 @@ -using System.Collections.Immutable; -using System.Text.RegularExpressions; - -using Microsoft.Recognizers.Text.DateTime.Utilities; -using Microsoft.Recognizers.Definitions.English; -using Microsoft.Recognizers.Text.Number; - -namespace Microsoft.Recognizers.Text.DateTime.English -{ - public class EnglishDateTimeParserConfiguration : BaseOptionsConfiguration, IDateTimeParserConfiguration - { - public string TokenBeforeDate { get; } - +using System.Collections.Immutable; +using System.Text.RegularExpressions; + +using Microsoft.Recognizers.Text.DateTime.Utilities; +using Microsoft.Recognizers.Definitions.English; +using Microsoft.Recognizers.Text.Number; + +namespace Microsoft.Recognizers.Text.DateTime.English +{ + public class EnglishDateTimeParserConfiguration : BaseOptionsConfiguration, IDateTimeParserConfiguration + { + public string TokenBeforeDate { get; } + public string TokenBeforeTime { get; } - - public IDateTimeExtractor DateExtractor { get; } - - public IDateTimeExtractor TimeExtractor { get; } - - public IDateTimeParser DateParser { get; } - - public IDateTimeParser TimeParser { get; } - - public IExtractor CardinalExtractor { get; } - - public IExtractor IntegerExtractor { get; } - - public IParser NumberParser { get; } - - public IDateTimeExtractor DurationExtractor { get; } - - public IDateTimeParser DurationParser { get; } - - public IImmutableDictionary UnitMap { get; } - - public Regex NowRegex { get; } - - public Regex AMTimeRegex { get; } - - public Regex PMTimeRegex { get; } - - public Regex SimpleTimeOfTodayAfterRegex { get; } - - public Regex SimpleTimeOfTodayBeforeRegex { get; } - - public Regex SpecificTimeOfDayRegex { get; } - - public Regex TheEndOfRegex { get; } - - public Regex UnitRegex { get; } - - public Regex DateNumberConnectorRegex { get; } - - public Regex PrepositionRegex { get; } - - public Regex ConnectorRegex { get; } - - public IImmutableDictionary Numbers { get; } - - public IDateTimeUtilityConfiguration UtilityConfiguration { get; } - + + public IDateTimeExtractor DateExtractor { get; } + + public IDateTimeExtractor TimeExtractor { get; } + + public IDateTimeParser DateParser { get; } + + public IDateTimeParser TimeParser { get; } + + public IExtractor CardinalExtractor { get; } + + public IExtractor IntegerExtractor { get; } + + public IParser NumberParser { get; } + + public IDateTimeExtractor DurationExtractor { get; } + + public IDateTimeParser DurationParser { get; } + + public IImmutableDictionary UnitMap { get; } + + public Regex NowRegex { get; } + + public Regex AMTimeRegex { get; } + + public Regex PMTimeRegex { get; } + + public Regex SimpleTimeOfTodayAfterRegex { get; } + + public Regex SimpleTimeOfTodayBeforeRegex { get; } + + public Regex SpecificTimeOfDayRegex { get; } + + public Regex TheEndOfRegex { get; } + + public Regex UnitRegex { get; } + + public Regex DateNumberConnectorRegex { get; } + + public Regex PrepositionRegex { get; } + + public Regex ConnectorRegex { get; } + + public IImmutableDictionary Numbers { get; } + + public IDateTimeUtilityConfiguration UtilityConfiguration { get; } + public EnglishDateTimeParserConfiguration(ICommonDateTimeParserConfiguration config) : base(config.Options) - { - - TokenBeforeDate = DateTimeDefinitions.TokenBeforeDate; - TokenBeforeTime = DateTimeDefinitions.TokenBeforeTime; - - DateExtractor = config.DateExtractor; - TimeExtractor = config.TimeExtractor; - DateParser = config.DateParser; - TimeParser = config.TimeParser; - + { + + TokenBeforeDate = DateTimeDefinitions.TokenBeforeDate; + TokenBeforeTime = DateTimeDefinitions.TokenBeforeTime; + + DateExtractor = config.DateExtractor; + TimeExtractor = config.TimeExtractor; + DateParser = config.DateParser; + TimeParser = config.TimeParser; + NowRegex = EnglishDateTimeExtractorConfiguration.NowRegex; - AMTimeRegex = new Regex(DateTimeDefinitions.AMTimeRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline); - PMTimeRegex = new Regex(DateTimeDefinitions.PMTimeRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline); - - SimpleTimeOfTodayAfterRegex = EnglishDateTimeExtractorConfiguration.SimpleTimeOfTodayAfterRegex; - SimpleTimeOfTodayBeforeRegex = EnglishDateTimeExtractorConfiguration.SimpleTimeOfTodayBeforeRegex; - SpecificTimeOfDayRegex = EnglishDateTimeExtractorConfiguration.SpecificTimeOfDayRegex; - TheEndOfRegex = EnglishDateTimeExtractorConfiguration.TheEndOfRegex; + AMTimeRegex = new Regex(DateTimeDefinitions.AMTimeRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline); + PMTimeRegex = new Regex(DateTimeDefinitions.PMTimeRegex, RegexOptions.IgnoreCase | RegexOptions.Singleline); + + SimpleTimeOfTodayAfterRegex = EnglishDateTimeExtractorConfiguration.SimpleTimeOfTodayAfterRegex; + SimpleTimeOfTodayBeforeRegex = EnglishDateTimeExtractorConfiguration.SimpleTimeOfTodayBeforeRegex; + SpecificTimeOfDayRegex = EnglishDateTimeExtractorConfiguration.SpecificTimeOfDayRegex; + TheEndOfRegex = EnglishDateTimeExtractorConfiguration.TheEndOfRegex; UnitRegex = EnglishTimeExtractorConfiguration.TimeUnitRegex; DateNumberConnectorRegex = EnglishDateTimeExtractorConfiguration.DateNumberConnectorRegex; - Numbers = config.Numbers; - CardinalExtractor = config.CardinalExtractor; - IntegerExtractor = config.IntegerExtractor; - NumberParser = config.NumberParser; - DurationExtractor = config.DurationExtractor; - DurationParser = config.DurationParser; - UnitMap = config.UnitMap; - UtilityConfiguration = config.UtilityConfiguration; - } - - public int GetHour(string text, int hour) - { - var trimedText = text.Trim().ToLowerInvariant(); - int result = hour; - if (trimedText.EndsWith("morning") && hour >= 12) - { - result -= 12; - } - else if (!trimedText.EndsWith("morning") && hour < 12) - { - result += 12; - } - return result; - } - - public bool GetMatchedNowTimex(string text, out string timex) - { - var trimedText = text.Trim().ToLowerInvariant(); - if (trimedText.EndsWith("now")) - { - timex = "PRESENT_REF"; - } - else if (trimedText.Equals("recently") || trimedText.Equals("previously")) - { - timex = "PAST_REF"; - } - else if (trimedText.Equals("as soon as possible") || trimedText.Equals("asap")) - { - timex = "FUTURE_REF"; - } - else - { - timex = null; - return false; - } - - return true; - } - - public int GetSwiftDay(string text) - { - var trimedText = text.Trim().ToLowerInvariant(); - - var swift = 0; - if (trimedText.StartsWith("next")) - { - swift = 1; - } - else if (trimedText.StartsWith("last")) - { - swift = -1; - } - - return swift; - } - - public bool ContainsAmbiguousToken(string text, string matchedText) => false; - } -} + Numbers = config.Numbers; + CardinalExtractor = config.CardinalExtractor; + IntegerExtractor = config.IntegerExtractor; + NumberParser = config.NumberParser; + DurationExtractor = config.DurationExtractor; + DurationParser = config.DurationParser; + UnitMap = config.UnitMap; + UtilityConfiguration = config.UtilityConfiguration; + } + + public int GetHour(string text, int hour) + { + var trimedText = text.Trim().ToLowerInvariant(); + int result = hour; + if (trimedText.EndsWith("morning") && hour >= Constants.HalfDayHourCount) + { + result -= Constants.HalfDayHourCount; + } + else if (!trimedText.EndsWith("morning") && hour < Constants.HalfDayHourCount) + { + result += Constants.HalfDayHourCount; + } + return result; + } + + public bool GetMatchedNowTimex(string text, out string timex) + { + var trimedText = text.Trim().ToLowerInvariant(); + if (trimedText.EndsWith("now")) + { + timex = "PRESENT_REF"; + } + else if (trimedText.Equals("recently") || trimedText.Equals("previously")) + { + timex = "PAST_REF"; + } + else if (trimedText.Equals("as soon as possible") || trimedText.Equals("asap")) + { + timex = "FUTURE_REF"; + } + else + { + timex = null; + return false; + } + + return true; + } + + public int GetSwiftDay(string text) + { + var trimedText = text.Trim().ToLowerInvariant(); + + var swift = 0; + if (trimedText.StartsWith("next")) + { + swift = 1; + } + else if (trimedText.StartsWith("last")) + { + swift = -1; + } + + return swift; + } + + public bool ContainsAmbiguousToken(string text, string matchedText) => false; + } +} diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimePeriodParserConfiguration.cs index 0944b49474..82a85aa0a0 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishDateTimePeriodParserConfiguration.cs @@ -135,12 +135,12 @@ public bool GetMatchedTimeRange(string text, out string timeStr, out int beginHo { timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (AfternoonStartEndRegex.IsMatch(trimedText)) { timeStr = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (EveningStartEndRegex.IsMatch(trimedText)) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimeParserConfiguration.cs index 1daa702f90..a2e95312f0 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimeParserConfiguration.cs @@ -104,12 +104,12 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha var oclockStr = match.Groups["oclock"].Value; if (string.IsNullOrEmpty(oclockStr)) { - var amStr = match.Groups["am"].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; if (!string.IsNullOrEmpty(amStr)) { - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - deltaHour = -12; + deltaHour = -Constants.HalfDayHourCount; } else { @@ -117,21 +117,21 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha } } - var pmStr = match.Groups["pm"].Value; + var pmStr = match.Groups[Constants.PmGroupName].Value; if (!string.IsNullOrEmpty(pmStr)) { - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - deltaHour = 12; + deltaHour = Constants.HalfDayHourCount; } if (LunchRegex.IsMatch(pmStr)) { // for hour>=10, <12 - if (hour >=10 && hour <=12) + if (hour >=10 && hour <=Constants.HalfDayHourCount) { deltaHour = 0; - if (hour == 12) + if (hour == Constants.HalfDayHourCount) { hasPm = true; } @@ -148,9 +148,9 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha else if (NightRegex.IsMatch(pmStr)) { //For hour <=3 or ==12, we treat it as am, for example 1 in the night (midnight) == 1am - if (hour <= 3 || hour == 12) + if (hour <= 3 || hour == Constants.HalfDayHourCount) { - if (hour == 12) + if (hour == Constants.HalfDayHourCount) { hour = 0; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimePeriodParserConfiguration.cs index accb741677..c55f2e5fe0 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/EnglishTimePeriodParserConfiguration.cs @@ -63,12 +63,12 @@ public bool GetMatchedTimexRange(string text, out string timex, out int beginHou { timex = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.EndsWith("afternoon")) { timex = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("evening")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/TimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/TimeParser.cs index de5892a9f7..41a6cf3284 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/TimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/English/Parsers/TimeParser.cs @@ -25,8 +25,8 @@ private DateTimeResolutionResult ParseIsh(string text, DateObject referenceTime) var match = EnglishTimeExtractorConfiguration.IshRegex.Match(trimedText); if (match.Success && match.Length == trimedText.Length) { - var hourStr = match.Groups["hour"].Value; - var hour = 12; + var hourStr = match.Groups[Constants.HourGroupName].Value; + var hour = Constants.HalfDayHourCount; if (!string.IsNullOrEmpty(hourStr)) { hour = int.Parse(hourStr); diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDatePeriodExtractor.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDatePeriodExtractor.cs index 816b7c90e3..236eb4a6ea 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDatePeriodExtractor.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDatePeriodExtractor.cs @@ -234,7 +234,7 @@ private List SingleTimePointWithPatterns(string text, DateObject referenc var match = this.config.WithinNextPrefixRegex.Match(beforeString); if (match.Success) { - var isNext = !string.IsNullOrEmpty(match.Groups[Constants.NextGroupNameForWithinRegex].Value); + var isNext = !string.IsNullOrEmpty(match.Groups[Constants.NextGroupName].Value); // For "within" case // Cases like "within the next 5 days before today" is not acceptable diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDateTimeExtractor.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDateTimeExtractor.cs index 510f15f60b..dd04e01c30 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDateTimeExtractor.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseDateTimeExtractor.cs @@ -80,12 +80,14 @@ public List MergeDateAndTime(string text, DateObject reference) var numErs = new List(); for (var idx = 0; idx < timeNumMatches.Count; idx++) { - var match = timeNumMatches[idx]; - var node = new ExtractResult(); - node.Start = match.Index; - node.Length = match.Length; - node.Text = match.Value; - node.Type = Number.Constants.SYS_NUM_INTEGER; + var match = timeNumMatches[idx]; + var node = new ExtractResult + { + Start = match.Index, + Length = match.Length, + Text = match.Value, + Type = Number.Constants.SYS_NUM_INTEGER + }; numErs.Add(node); } ers.AddRange(numErs); diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseSetExtractor.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseSetExtractor.cs index 2496bfd13c..e4e9b91fec 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseSetExtractor.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseSetExtractor.cs @@ -1,115 +1,115 @@ -using System.Collections.Generic; -using System.Text.RegularExpressions; -using DateObject = System.DateTime; - -using Microsoft.Recognizers.Text.Number; - -namespace Microsoft.Recognizers.Text.DateTime -{ - public class BaseSetExtractor : IDateTimeExtractor - { - public static readonly string ExtractorName = Constants.SYS_DATETIME_SET; - - private readonly ISetExtractorConfiguration config; - - public BaseSetExtractor(ISetExtractorConfiguration config) - { - this.config = config; - } - +using System.Collections.Generic; +using System.Text.RegularExpressions; +using DateObject = System.DateTime; + +using Microsoft.Recognizers.Text.Number; + +namespace Microsoft.Recognizers.Text.DateTime +{ + public class BaseSetExtractor : IDateTimeExtractor + { + public static readonly string ExtractorName = Constants.SYS_DATETIME_SET; + + private readonly ISetExtractorConfiguration config; + + public BaseSetExtractor(ISetExtractorConfiguration config) + { + this.config = config; + } + public List Extract(string text) { return Extract(text, DateObject.Now); - } - - public List Extract(string text, DateObject reference) - { - var tokens = new List(); - tokens.AddRange(MatchEachUnit(text)); - tokens.AddRange(MatchEachDuration(text, reference)); - tokens.AddRange(TimeEveryday(text, reference)); - tokens.AddRange(MatchEach(config.DateExtractor, text, reference)); - tokens.AddRange(MatchEach(config.TimeExtractor, text, reference)); - tokens.AddRange(MatchEach(config.DateTimeExtractor, text, reference)); - tokens.AddRange(MatchEach(config.DatePeriodExtractor, text, reference)); - tokens.AddRange(MatchEach(config.TimePeriodExtractor, text, reference)); - tokens.AddRange(MatchEach(config.DateTimePeriodExtractor, text, reference)); - - return Token.MergeAllTokens(tokens, text, ExtractorName); - } - - public List MatchEachDuration(string text, DateObject reference) - { - var ret = new List(); - - var ers = this.config.DurationExtractor.Extract(text, reference); - foreach (var er in ers) - { - // "each last summer" doesn't make sense - if (this.config.LastRegex.IsMatch(er.Text)) - { - continue; - } - - var beforeStr = text.Substring(0, er.Start ?? 0); - var match = this.config.EachPrefixRegex.Match(beforeStr); - if (match.Success) - { - ret.Add(new Token(match.Index, er.Start + er.Length ?? 0)); - } - } - - return ret; - } - - public List MatchEachUnit(string text) - { - var ret = new List(); - // handle "daily", "monthly" - var matches = this.config.PeriodicRegex.Matches(text); - foreach (Match match in matches) - { - ret.Add(new Token(match.Index, match.Index + match.Length)); - } - - // handle "each month" - matches = this.config.EachUnitRegex.Matches(text); - foreach (Match match in matches) - { - ret.Add(new Token(match.Index, match.Index + match.Length)); - } - - return ret; - } - - public virtual List TimeEveryday(string text, DateObject reference) - { - var ret = new List(); - var ers = this.config.TimeExtractor.Extract(text, reference); - foreach (var er in ers) - { - var afterStr = text.Substring(er.Start + er.Length ?? 0); - if (string.IsNullOrEmpty(afterStr) && this.config.BeforeEachDayRegex != null) - { - var beforeStr = text.Substring(0, er.Start ?? 0); - var beforeMatch = this.config.BeforeEachDayRegex.Match(beforeStr); - if (beforeMatch.Success) - { - ret.Add(new Token(beforeMatch.Index, (er.Start + er.Length ?? 0))); - } - } - else - { - var match = this.config.EachDayRegex.Match(afterStr); - if (match.Success) - { - ret.Add(new Token(er.Start ?? 0, (er.Start + er.Length ?? 0) + match.Length)); - } - } - } - return ret; - } - + } + + public List Extract(string text, DateObject reference) + { + var tokens = new List(); + tokens.AddRange(MatchEachUnit(text)); + tokens.AddRange(MatchEachDuration(text, reference)); + tokens.AddRange(TimeEveryday(text, reference)); + tokens.AddRange(MatchEach(config.DateExtractor, text, reference)); + tokens.AddRange(MatchEach(config.TimeExtractor, text, reference)); + tokens.AddRange(MatchEach(config.DateTimeExtractor, text, reference)); + tokens.AddRange(MatchEach(config.DatePeriodExtractor, text, reference)); + tokens.AddRange(MatchEach(config.TimePeriodExtractor, text, reference)); + tokens.AddRange(MatchEach(config.DateTimePeriodExtractor, text, reference)); + + return Token.MergeAllTokens(tokens, text, ExtractorName); + } + + public List MatchEachDuration(string text, DateObject reference) + { + var ret = new List(); + + var ers = this.config.DurationExtractor.Extract(text, reference); + foreach (var er in ers) + { + // "each last summer" doesn't make sense + if (this.config.LastRegex.IsMatch(er.Text)) + { + continue; + } + + var beforeStr = text.Substring(0, er.Start ?? 0); + var match = this.config.EachPrefixRegex.Match(beforeStr); + if (match.Success) + { + ret.Add(new Token(match.Index, er.Start + er.Length ?? 0)); + } + } + + return ret; + } + + public List MatchEachUnit(string text) + { + var ret = new List(); + // handle "daily", "monthly" + var matches = this.config.PeriodicRegex.Matches(text); + foreach (Match match in matches) + { + ret.Add(new Token(match.Index, match.Index + match.Length)); + } + + // handle "each month" + matches = this.config.EachUnitRegex.Matches(text); + foreach (Match match in matches) + { + ret.Add(new Token(match.Index, match.Index + match.Length)); + } + + return ret; + } + + public virtual List TimeEveryday(string text, DateObject reference) + { + var ret = new List(); + var ers = this.config.TimeExtractor.Extract(text, reference); + foreach (var er in ers) + { + var afterStr = text.Substring(er.Start + er.Length ?? 0); + if (string.IsNullOrEmpty(afterStr) && this.config.BeforeEachDayRegex != null) + { + var beforeStr = text.Substring(0, er.Start ?? 0); + var beforeMatch = this.config.BeforeEachDayRegex.Match(beforeStr); + if (beforeMatch.Success) + { + ret.Add(new Token(beforeMatch.Index, (er.Start + er.Length ?? 0))); + } + } + else + { + var match = this.config.EachDayRegex.Match(afterStr); + if (match.Success) + { + ret.Add(new Token(er.Start ?? 0, (er.Start + er.Length ?? 0) + match.Length)); + } + } + } + return ret; + } + public List MatchEach(IDateTimeExtractor extractor, string text, DateObject reference) { var ret = new List(); @@ -145,9 +145,9 @@ public List MatchEach(IDateTimeExtractor extractor, string text, DateObje if (er.Start <= match.Index && er.Text.Contains(match.Groups["weekday"].Value)) { var len = (er.Length ?? 0) + 1; - if (match.Groups["prefix"].ToString() != string.Empty) + if (match.Groups[Constants.PrefixGroupName].ToString() != string.Empty) { - len += match.Groups["prefix"].ToString().Length; + len += match.Groups[Constants.PrefixGroupName].ToString().Length; } ret.Add(new Token(er.Start ?? 0, er.Start + len ?? 0)); } @@ -156,6 +156,6 @@ public List MatchEach(IDateTimeExtractor extractor, string text, DateObje } return ret; - } - } -} + } + } +} diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseTimePeriodExtractor.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseTimePeriodExtractor.cs index 52ea59eb17..03ca998684 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseTimePeriodExtractor.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Extractors/BaseTimePeriodExtractor.cs @@ -43,7 +43,7 @@ private List MatchSimpleCases(string text) foreach (Match match in matches) { // Cases like "from 10:30 to 11", don't necessarily need "am/pm" - if (match.Groups["min"].Success || match.Groups["sec"].Success) + if (match.Groups[Constants.MinuteGroupName].Success || match.Groups[Constants.SecondGroupName].Success) { // Cases like "from 3:30 to 4" should be supported // Cases like "from 3:30 to 4 on 1/1/2015" should be supported @@ -74,12 +74,12 @@ private List MatchSimpleCases(string text) } else { - // Is there "pm" or "am" ? - var pmStr = match.Groups["pm"].Value; - var amStr = match.Groups["am"].Value; - var descStr = match.Groups["desc"].Value; + // Is there Constants.PmGroupName or Constants.AmGroupName ? + var pmStr = match.Groups[Constants.PmGroupName].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; + var descStr = match.Groups[Constants.DescGroupName].Value; - // Check "pm", "am" + // Check Constants.PmGroupName, Constants.AmGroupName if (!string.IsNullOrEmpty(pmStr) || !string.IsNullOrEmpty(amStr) || !string.IsNullOrEmpty(descStr)) { ret.Add(new Token(match.Index, match.Index + match.Length)); diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimeParserConfiguration.cs index 3a73b080a9..40b6582e01 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimeParserConfiguration.cs @@ -90,13 +90,13 @@ public int GetHour(string text, int hour) { var trimedText = text.Trim().ToLowerInvariant(); int result = hour; - if (trimedText.EndsWith("matin") && hour >= 12) + if (trimedText.EndsWith("matin") && hour >= Constants.HalfDayHourCount) { - result -= 12; + result -= Constants.HalfDayHourCount; } - else if (!trimedText.EndsWith("matin") && hour < 12) + else if (!trimedText.EndsWith("matin") && hour < Constants.HalfDayHourCount) { - result += 12; + result += Constants.HalfDayHourCount; } return result; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimePeriodParserConfiguration.cs index d8b2021460..cb552de8bf 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchDateTimePeriodParserConfiguration.cs @@ -134,12 +134,12 @@ public bool GetMatchedTimeRange(string text, out string timeStr, out int beginHo { timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (AfternoonStartEndRegex.IsMatch(trimedText)) { timeStr = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (EveningStartEndRegex.IsMatch(trimedText)) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimeParserConfiguration.cs index c8dbdf8e27..cf091d7788 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimeParserConfiguration.cs @@ -87,22 +87,22 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha var oclockStr = match.Groups["heures"].Value; if (string.IsNullOrEmpty(oclockStr)) { - var amStr = match.Groups["am"].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; if (!string.IsNullOrEmpty(amStr)) { - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - deltaHour = -12; + deltaHour = -Constants.HalfDayHourCount; } hasAm = true; } - var pmStr = match.Groups["pm"].Value; + var pmStr = match.Groups[Constants.PmGroupName].Value; if (!string.IsNullOrEmpty(pmStr)) { - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - deltaHour = 12; + deltaHour = Constants.HalfDayHourCount; } hasPm = true; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimePeriodParserConfiguration.cs index 51928ded0c..a2e96897a1 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/FrenchTimePeriodParserConfiguration.cs @@ -64,13 +64,13 @@ public bool GetMatchedTimexRange(string text, out string timex, out int beginHou { timex = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.EndsWith("apres-midi")||trimedText.EndsWith("apres midi") || trimedText.EndsWith("après midi") || trimedText.EndsWith("après-midi")) { timex = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("soir") || trimedText.EndsWith("soiree") || trimedText.EndsWith("soirée")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/TimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/TimeParser.cs index c030150a2b..c0aa118300 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/TimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/French/Parsers/TimeParser.cs @@ -26,8 +26,8 @@ private DateTimeResolutionResult ParseIsh(string text, DateObject referenceTime) var match = FrenchTimeExtractorConfiguration.IshRegex.Match(trimedText); if (match.Success && match.Length == trimedText.Length) { - var hourStr = match.Groups["hour"].Value; - var hour = 12; + var hourStr = match.Groups[Constants.HourGroupName].Value; + var hour = Constants.HalfDayHourCount; if (!string.IsNullOrEmpty(hourStr)) { hour = int.Parse(hourStr); diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimeParserConfiguration.cs index e2aa8fa94f..1229f2ee89 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimeParserConfiguration.cs @@ -91,13 +91,13 @@ public int GetHour(string text, int hour) { var trimedText = text.Trim().ToLowerInvariant(); int result = hour; - if ((trimedText.EndsWith("morgen") || trimedText.EndsWith("morgens")) && hour >= 12) + if ((trimedText.EndsWith("morgen") || trimedText.EndsWith("morgens")) && hour >= Constants.HalfDayHourCount) { - result -= 12; + result -= Constants.HalfDayHourCount; } - else if (!(trimedText.EndsWith("morgen") || trimedText.EndsWith("morgens")) && hour < 12) + else if (!(trimedText.EndsWith("morgen") || trimedText.EndsWith("morgens")) && hour < Constants.HalfDayHourCount) { - result += 12; + result += Constants.HalfDayHourCount; } return result; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimePeriodParserConfiguration.cs index ee05445c7b..f576b33396 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanDateTimePeriodParserConfiguration.cs @@ -136,12 +136,12 @@ public bool GetMatchedTimeRange(string text, out string timeStr, out int beginHo { timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (AfternoonStartEndRegex.IsMatch(trimedText)) { timeStr = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (EveningStartEndRegex.IsMatch(trimedText)) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimeParserConfiguration.cs index cc8dbcd620..05795c271b 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimeParserConfiguration.cs @@ -104,12 +104,12 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha var oclockStr = match.Groups["oclock"].Value; if (string.IsNullOrEmpty(oclockStr)) { - var amStr = match.Groups["am"].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; if (!string.IsNullOrEmpty(amStr)) { - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - deltaHour = -12; + deltaHour = -Constants.HalfDayHourCount; } else { @@ -117,21 +117,21 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha } } - var pmStr = match.Groups["pm"].Value; + var pmStr = match.Groups[Constants.PmGroupName].Value; if (!string.IsNullOrEmpty(pmStr)) { - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - deltaHour = 12; + deltaHour = Constants.HalfDayHourCount; } if (LunchRegex.IsMatch(pmStr)) { // for hour>=10, <12 - if (hour >=10 && hour <=12) + if (hour >=10 && hour <=Constants.HalfDayHourCount) { deltaHour = 0; - if (hour == 12) + if (hour == Constants.HalfDayHourCount) { hasPm = true; } @@ -148,9 +148,9 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha else if (NightRegex.IsMatch(pmStr)) { //For hour <=3 or ==12, we treat it as am, for example 1 in the night (midnight) == 1am - if (hour <= 3 || hour == 12) + if (hour <= 3 || hour == Constants.HalfDayHourCount) { - if (hour == 12) + if (hour == Constants.HalfDayHourCount) { hour = 0; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimePeriodParserConfiguration.cs index d5aabe72e7..80bbbfeffb 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/GermanTimePeriodParserConfiguration.cs @@ -63,12 +63,12 @@ public bool GetMatchedTimexRange(string text, out string timex, out int beginHou { timex = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.EndsWith("nachmittag")) { timex = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("abend")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/TimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/TimeParser.cs index 05dafeb343..81a21134fd 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/TimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/German/Parsers/TimeParser.cs @@ -25,8 +25,8 @@ private DateTimeResolutionResult ParseIsh(string text, DateObject referenceTime) var match = GermanTimeExtractorConfiguration.IshRegex.Match(trimedText); if (match.Success && match.Length == trimedText.Length) { - var hourStr = match.Groups["hour"].Value; - var hour = 12; + var hourStr = match.Groups[Constants.HourGroupName].Value; + var hour = Constants.HalfDayHourCount; if (!string.IsNullOrEmpty(hourStr)) { hour = int.Parse(hourStr); diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDatePeriodParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDatePeriodParser.cs index fe3688a9a3..2af5a3194a 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDatePeriodParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDatePeriodParser.cs @@ -653,19 +653,19 @@ private DateTimeResolutionResult ParseOneWordPeriod(string text, DateObject refe if (match.Groups["EarlyPrefix"].Success) { earlyPrefix = true; - trimedText = match.Groups["suffix"].ToString(); + trimedText = match.Groups[Constants.SuffixGroupName].ToString(); ret.Mod = Constants.EARLY_MOD; } else if (match.Groups["LatePrefix"].Success) { latePrefix = true; - trimedText = match.Groups["suffix"].ToString(); + trimedText = match.Groups[Constants.SuffixGroupName].ToString(); ret.Mod = Constants.LATE_MOD; } else if (match.Groups["MidPrefix"].Success) { midPrefix = true; - trimedText = match.Groups["suffix"].ToString(); + trimedText = match.Groups[Constants.SuffixGroupName].ToString(); ret.Mod = Constants.MID_MOD; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimeParser.cs index a3d5e05283..92dafc5089 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimeParser.cs @@ -197,13 +197,13 @@ private DateTimeResolutionResult MergeDateAndTime(string text, DateObject refere var sec = time.Second; // Handle morning, afternoon - if (this.config.PMTimeRegex.IsMatch(text) && hour < 12) + if (this.config.PMTimeRegex.IsMatch(text) && hour < Constants.HalfDayHourCount) { - hour += 12; + hour += Constants.HalfDayHourCount; } - else if (this.config.AMTimeRegex.IsMatch(text) && hour >= 12) + else if (this.config.AMTimeRegex.IsMatch(text) && hour >= Constants.HalfDayHourCount) { - hour -= 12; + hour -= Constants.HalfDayHourCount; } var timeStr = pr2.TimexStr; @@ -215,7 +215,7 @@ private DateTimeResolutionResult MergeDateAndTime(string text, DateObject refere ret.Timex = pr1.TimexStr + timeStr; var val = (DateTimeResolutionResult) pr2.Value; - if (hour <= 12 && !this.config.PMTimeRegex.IsMatch(text) && !this.config.AMTimeRegex.IsMatch(text) && + if (hour <= Constants.HalfDayHourCount && !this.config.PMTimeRegex.IsMatch(text) && !this.config.AMTimeRegex.IsMatch(text) && !string.IsNullOrEmpty(val.Comment)) { ret.Comment = Constants.Comment_AmPm; @@ -257,7 +257,7 @@ private DateTimeResolutionResult ParseTimeOfToday(string text, DateObject refere if (wholeMatch.Success && wholeMatch.Length == trimedText.Length) { - var hourStr = wholeMatch.Groups["hour"].Value; + var hourStr = wholeMatch.Groups[Constants.HourGroupName].Value; if (string.IsNullOrEmpty(hourStr)) { hourStr = wholeMatch.Groups["hournum"].Value.ToLower(); diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimePeriodParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimePeriodParser.cs index 4e4a604eb4..db388e44c3 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimePeriodParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDateTimePeriodParser.cs @@ -374,7 +374,7 @@ private DateTimeResolutionResult MergeDateAndTimePeriods(string text, DateObject // AmPm comment is used for later SetParserResult to judge whether this parse result should have two parsing results // Cases like "from 10:30 to 11 on 1/1/2015" should have AmPm comment, as it can be parsed to "10:30am to 11am" and also be parsed to "10:30pm to 11pm" // Cases like "from 10:30 to 3 on 1/1/2015" should not have AmPm comment - if (beginTime.Hour < 12 && endTime.Hour < 12) + if (beginTime.Hour < Constants.HalfDayHourCount && endTime.Hour < Constants.HalfDayHourCount) { ret.Comment = Constants.Comment_AmPm; } @@ -408,13 +408,13 @@ private DateTimeResolutionResult ParseSimpleCases(string text, DateObject refere if (match.Success && (match.Index == 0 || match.Index + match.Length == trimedText.Length)) { - // This "from .. to .." pattern is valid if followed by a Date OR "pm" + // This "from .. to .." pattern is valid if followed by a Date OR Constants.PmGroupName var hasAm = false; var hasPm = false; string dateStr; // Get hours - var hourGroup = match.Groups["hour"]; + var hourGroup = match.Groups[Constants.HourGroupName]; var hourStr = hourGroup.Captures[0].Value; int beginHour; @@ -468,50 +468,50 @@ private DateTimeResolutionResult ParseSimpleCases(string text, DateObject refere return ret; } - // Parse "pm" - var pmStr = match.Groups["pm"].Value; - var amStr = match.Groups["am"].Value; - var descStr = match.Groups["desc"].Value; + // Parse Constants.PmGroupName + var pmStr = match.Groups[Constants.PmGroupName].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; + var descStr = match.Groups[Constants.DescGroupName].Value; if (!string.IsNullOrEmpty(amStr) || !string.IsNullOrEmpty(descStr) && descStr.StartsWith("a")) { - if (beginHour >= 12) + if (beginHour >= Constants.HalfDayHourCount) { - beginHour -= 12; + beginHour -= Constants.HalfDayHourCount; } - if (endHour >= 12) + if (endHour >= Constants.HalfDayHourCount) { - endHour -= 12; + endHour -= Constants.HalfDayHourCount; } hasAm = true; } else if (!string.IsNullOrEmpty(pmStr) || !string.IsNullOrEmpty(descStr) && descStr.StartsWith("p")) { - if (beginHour < 12) + if (beginHour < Constants.HalfDayHourCount) { - beginHour += 12; + beginHour += Constants.HalfDayHourCount; } - if (endHour < 12) + if (endHour < Constants.HalfDayHourCount) { - endHour += 12; + endHour += Constants.HalfDayHourCount; } hasPm = true; } - if (!hasAm && !hasPm && beginHour <= 12 && endHour <= 12) + if (!hasAm && !hasPm && beginHour <= Constants.HalfDayHourCount && endHour <= Constants.HalfDayHourCount) { if (beginHour > endHour) { - if (beginHour == 12) + if (beginHour == Constants.HalfDayHourCount) { beginHour = 0; } else { - endHour += 12; + endHour += Constants.HalfDayHourCount; } } ret.Comment = Constants.Comment_AmPm; diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDurationParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDurationParser.cs index 9c61df8b6a..73eb49db70 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDurationParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseDurationParser.cs @@ -151,7 +151,7 @@ private DateTimeResolutionResult ParseNumberSpaceUnit(string text) if (match.Success) { srcUnit = match.Groups["unit"].Value.ToLower(); - suffixStr = match.Groups["suffix"].Value.ToLower(); + suffixStr = match.Groups[Constants.SuffixGroupName].Value.ToLower(); } if (this.config.UnitMap.ContainsKey(srcUnit)) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs index 37742cd0ba..25c57b81fe 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseMergedDateTimeParser.cs @@ -574,7 +574,7 @@ internal void ResolveAmpm(Dictionary resolutionDic, string keyNa if (resolution.ContainsKey(DateTimeResolutionKey.START)) { var start = Convert.ToDateTime(resolution[DateTimeResolutionKey.START]); - start = start.Hour == 12 ? start.AddHours(-12) : start.AddHours(12); + start = start.Hour == Constants.HalfDayHourCount ? start.AddHours(-Constants.HalfDayHourCount) : start.AddHours(Constants.HalfDayHourCount); resolutionPm[DateTimeResolutionKey.START] = FormatUtil.FormatDateTime(start); } @@ -582,7 +582,7 @@ internal void ResolveAmpm(Dictionary resolutionDic, string keyNa if (resolution.ContainsKey(DateTimeResolutionKey.END)) { var end = Convert.ToDateTime(resolution[DateTimeResolutionKey.END]); - end = end.Hour == 12 ? end.AddHours(-12) : end.AddHours(12); + end = end.Hour == Constants.HalfDayHourCount ? end.AddHours(-Constants.HalfDayHourCount) : end.AddHours(Constants.HalfDayHourCount); resolutionPm[DateTimeResolutionKey.END] = FormatUtil.FormatDateTime(end); } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeParser.cs index 903612260f..bd9828bc4d 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeParser.cs @@ -117,7 +117,7 @@ private DateTimeResolutionResult ParseBasicRegexMatch(string text, DateObject re hour = 0; } - if (hour <= 12 && hour != 0) + if (hour <= Constants.HalfDayHourCount && hour != 0) { ret.Comment = Constants.Comment_AmPm; } @@ -203,7 +203,7 @@ private DateTimeResolutionResult Match2Time(Match match, DateObject referenceTim } else if (!string.IsNullOrEmpty(match.Groups["midday"].Value)) { - hour = 12; + hour = Constants.HalfDayHourCount; min = 0; second = 0; } @@ -211,7 +211,7 @@ private DateTimeResolutionResult Match2Time(Match match, DateObject referenceTim else { // get hour - var hourStr = match.Groups["hour"].Value; + var hourStr = match.Groups[Constants.HourGroupName].Value; if (string.IsNullOrEmpty(hourStr)) { hourStr = match.Groups["hournum"].Value.ToLower(); @@ -232,7 +232,7 @@ private DateTimeResolutionResult Match2Time(Match match, DateObject referenceTim } // get minute - var minStr = match.Groups["min"].Value.ToLower(); + var minStr = match.Groups[Constants.MinuteGroupName].Value.ToLower(); if (string.IsNullOrEmpty(minStr)) { minStr = match.Groups["minnum"].Value; @@ -256,7 +256,7 @@ private DateTimeResolutionResult Match2Time(Match match, DateObject referenceTim } // get second - var secStr = match.Groups["sec"].Value.ToLower(); + var secStr = match.Groups[Constants.SecondGroupName].Value.ToLower(); if (!string.IsNullOrEmpty(secStr)) { second = int.Parse(secStr); @@ -264,42 +264,42 @@ private DateTimeResolutionResult Match2Time(Match match, DateObject referenceTim } } - //adjust by desc string - var descStr = match.Groups["desc"].Value.ToLower(); - if (!string.IsNullOrEmpty(descStr)) + // Adjust by desc string + var descStr = match.Groups[Constants.DescGroupName].Value.ToLower(); + + // ampm is a special case in which at 6ampm = at 6 + if (config.UtilityConfiguration.AmDescRegex.Match(descStr).Success + || config.UtilityConfiguration.AmPmDescRegex.Match(descStr).Success + || match.Groups[Constants.ImplicitAmGroupName].Success) { - //ampm is a special case in which at 6ampm = at 6 - if (config.UtilityConfiguration.AmDescRegex.Match(descStr.ToLower()).Success - || config.UtilityConfiguration.AmPmDescRegex.Match(descStr.ToLower()).Success) + if (hour >= Constants.HalfDayHourCount) { - if (hour >= 12) - { - hour -= 12; - } - if (!config.UtilityConfiguration.AmPmDescRegex.Match(descStr.ToLower()).Success) - { - hasAm = true; - } + hour -= Constants.HalfDayHourCount; } - else if (config.UtilityConfiguration.PmDescRegex.Match(descStr.ToLower()).Success) + if (!config.UtilityConfiguration.AmPmDescRegex.Match(descStr).Success) { - if (hour < 12) - { - hour += 12; - } - hasPm = true; + hasAm = true; + } + } + else if (config.UtilityConfiguration.PmDescRegex.Match(descStr).Success + || match.Groups[Constants.ImplicitPmGroupName].Success) + { + if (hour < Constants.HalfDayHourCount) + { + hour += Constants.HalfDayHourCount; } + hasPm = true; } // adjust min by prefix - var timePrefix = match.Groups["prefix"].Value.ToLower(); + var timePrefix = match.Groups[Constants.PrefixGroupName].Value.ToLower(); if (!string.IsNullOrEmpty(timePrefix)) { this.config.AdjustByPrefix(timePrefix, ref hour, ref min, ref hasMin); } // adjust hour by suffix - var timeSuffix = match.Groups["suffix"].Value.ToLower(); + var timeSuffix = match.Groups[Constants.SuffixGroupName].Value.ToLower(); if (!string.IsNullOrEmpty(timeSuffix)) { this.config.AdjustBySuffix(timeSuffix, ref hour, ref min, ref hasMin, ref hasAm, ref hasPm); @@ -321,7 +321,7 @@ private DateTimeResolutionResult Match2Time(Match match, DateObject referenceTim ret.Timex += ":" + second.ToString("D2"); } - if (hour <= 12 && !hasPm && !hasAm && !hasMid) + if (hour <= Constants.HalfDayHourCount && !hasPm && !hasAm && !hasMid) { ret.Comment = Constants.Comment_AmPm; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimePeriodParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimePeriodParser.cs index 782abf1aa5..4bfb79d381 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimePeriodParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimePeriodParser.cs @@ -114,11 +114,11 @@ private DateTimeResolutionResult ParsePureNumCases(string text, DateObject refer if (match.Success && match.Index == 0) { - // this "from .. to .." pattern is valid if followed by a Date OR "pm" + // this "from .. to .." pattern is valid if followed by a Date OR Constants.PmGroupName var isValid = false; // get hours - var hourGroup = match.Groups["hour"]; + var hourGroup = match.Groups[Constants.HourGroupName]; var hourStr = hourGroup.Captures[0].Value; var afterHourIndex = hourGroup.Captures[0].Index + hourGroup.Captures[0].Length; @@ -141,12 +141,12 @@ private DateTimeResolutionResult ParsePureNumCases(string text, DateObject refer endHour = int.Parse(hourStr); } - // parse "pm" + // parse Constants.PmGroupName var leftDesc = match.Groups["leftDesc"].Value; var rightDesc = match.Groups["rightDesc"].Value; - var pmStr = match.Groups["pm"].Value; - var amStr = match.Groups["am"].Value; - var descStr = match.Groups["desc"].Value; + var pmStr = match.Groups[Constants.PmGroupName].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; + var descStr = match.Groups[Constants.DescGroupName].Value; // The "ampm" only occurs in time, we don't have to consider it here if (string.IsNullOrEmpty(leftDesc)) @@ -159,20 +159,20 @@ private DateTimeResolutionResult ParsePureNumCases(string text, DateObject refer if (!string.IsNullOrEmpty(amStr) || rightAmValid) { - if (endHour >= 12) + if (endHour >= Constants.HalfDayHourCount) { - endHour -= 12; + endHour -= Constants.HalfDayHourCount; } - if (beginHour >= 12 && beginHour - 12 < endHour) + if (beginHour >= Constants.HalfDayHourCount && beginHour - Constants.HalfDayHourCount < endHour) { - beginHour -= 12; + beginHour -= Constants.HalfDayHourCount; } // Resolve case like "11 to 3am" - if (beginHour < 12 && beginHour > endHour) + if (beginHour < Constants.HalfDayHourCount && beginHour > endHour) { - beginHour += 12; + beginHour += Constants.HalfDayHourCount; } isValid = true; @@ -181,15 +181,15 @@ private DateTimeResolutionResult ParsePureNumCases(string text, DateObject refer else if (!string.IsNullOrEmpty(pmStr) || rightPmValid) { - if (endHour < 12) + if (endHour < Constants.HalfDayHourCount) { - endHour += 12; + endHour += Constants.HalfDayHourCount; } // Resolve case like "11 to 3pm" - if (beginHour + 12 < endHour) + if (beginHour + Constants.HalfDayHourCount < endHour) { - beginHour += 12; + beginHour += Constants.HalfDayHourCount; } isValid = true; @@ -243,7 +243,7 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject if (match.Success && match.Index == 0 && match.Index + match.Length == trimedText.Length) { // Cases like "half past seven" are not handled here - if (match.Groups["prefix"].Success) + if (match.Groups[Constants.PrefixGroupName].Success) { return ret; } @@ -259,7 +259,7 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject int endSecond = invalidFlag; // Get time1 and time2 - var hourGroup = match.Groups["hour"]; + var hourGroup = match.Groups[Constants.HourGroupName]; var hourStr = hourGroup.Captures[0].Value; @@ -290,9 +290,9 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject var time2EndIndex = time2StartIndex + match.Groups["time2"].Length; // Get beginMinute (if exists) and endMinute (if exists) - for (int i = 0; i < match.Groups["min"].Captures.Count; i++) + for (int i = 0; i < match.Groups[Constants.MinuteGroupName].Captures.Count; i++) { - var minuteCapture = match.Groups["min"].Captures[i]; + var minuteCapture = match.Groups[Constants.MinuteGroupName].Captures[i]; if (minuteCapture.Index >= time1StartIndex && minuteCapture.Index + minuteCapture.Length <= time1EndIndex) { beginMinute = int.Parse(minuteCapture.Value); @@ -304,9 +304,9 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject } // Get beginSecond (if exists) and endSecond (if exists) - for (int i = 0; i < match.Groups["sec"].Captures.Count; i++) + for (int i = 0; i < match.Groups[Constants.SecondGroupName].Captures.Count; i++) { - var secondCapture = match.Groups["sec"].Captures[i]; + var secondCapture = match.Groups[Constants.SecondGroupName].Captures[i]; if (secondCapture.Index >= time1StartIndex && secondCapture.Index + secondCapture.Length <= time1EndIndex) { beginSecond = int.Parse(secondCapture.Value); @@ -322,9 +322,9 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject var leftDesc = match.Groups["leftDesc"].Value; var rightDesc = match.Groups["rightDesc"].Value; - for (int i = 0; i < match.Groups["desc"].Captures.Count; i++) + for (int i = 0; i < match.Groups[Constants.DescGroupName].Captures.Count; i++) { - var descCapture = match.Groups["desc"].Captures[i]; + var descCapture = match.Groups[Constants.DescGroupName].Captures[i]; if (descCapture.Index >= time1StartIndex && descCapture.Index + descCapture.Length <= time1EndIndex && string.IsNullOrEmpty(leftDesc)) { leftDesc = descCapture.Value; @@ -350,31 +350,31 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject { if (hasLeftAm) { - if (beginHour >= 12) + if (beginHour >= Constants.HalfDayHourCount) { - beginDateTime = beginDateTime.AddHours(-12); + beginDateTime = beginDateTime.AddHours(-Constants.HalfDayHourCount); } } else if (hasLeftPm) { - if (beginHour < 12) + if (beginHour < Constants.HalfDayHourCount) { - beginDateTime = beginDateTime.AddHours(12); + beginDateTime = beginDateTime.AddHours(Constants.HalfDayHourCount); } } if (hasRightAm) { - if (endHour >= 12) + if (endHour >= Constants.HalfDayHourCount) { - endDateTime = endDateTime.AddHours(-12); + endDateTime = endDateTime.AddHours(-Constants.HalfDayHourCount); } } else if (hasRightPm) { - if (endHour < 12) + if (endHour < Constants.HalfDayHourCount) { - endDateTime = endDateTime.AddHours(12); + endDateTime = endDateTime.AddHours(Constants.HalfDayHourCount); } } } @@ -383,38 +383,38 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject { if (hasLeftAm) { - if (beginHour >= 12) + if (beginHour >= Constants.HalfDayHourCount) { - beginDateTime = beginDateTime.AddHours(-12); + beginDateTime = beginDateTime.AddHours(-Constants.HalfDayHourCount); } - if (endHour < 12) + if (endHour < Constants.HalfDayHourCount) { if (endDateTime < beginDateTime) { - endDateTime = endDateTime.AddHours(12); + endDateTime = endDateTime.AddHours(Constants.HalfDayHourCount); } } } else if (hasLeftPm) { - if (beginHour < 12) + if (beginHour < Constants.HalfDayHourCount) { - beginDateTime = beginDateTime.AddHours(12); + beginDateTime = beginDateTime.AddHours(Constants.HalfDayHourCount); } - if (endHour < 12) + if (endHour < Constants.HalfDayHourCount) { if (endDateTime < beginDateTime) { var span = beginDateTime - endDateTime; - if (span.TotalHours >= 12) + if (span.TotalHours >= Constants.HalfDayHourCount) { endDateTime = endDateTime.AddHours(24); } else { - endDateTime = endDateTime.AddHours(12); + endDateTime = endDateTime.AddHours(Constants.HalfDayHourCount); } } } @@ -422,55 +422,55 @@ private DateTimeResolutionResult ParseSpecificTimeCases(string text, DateObject if (hasRightAm) { - if (endHour >= 12) + if (endHour >= Constants.HalfDayHourCount) { - endDateTime = endDateTime.AddHours(-12); + endDateTime = endDateTime.AddHours(-Constants.HalfDayHourCount); } - if (beginHour < 12) + if (beginHour < Constants.HalfDayHourCount) { if (endDateTime < beginDateTime) { - beginDateTime = beginDateTime.AddHours(-12); + beginDateTime = beginDateTime.AddHours(-Constants.HalfDayHourCount); } } } else if (hasRightPm) { - if (endHour < 12) + if (endHour < Constants.HalfDayHourCount) { - endDateTime = endDateTime.AddHours(12); + endDateTime = endDateTime.AddHours(Constants.HalfDayHourCount); } - if (beginHour < 12) + if (beginHour < Constants.HalfDayHourCount) { if (endDateTime < beginDateTime) { - beginDateTime = beginDateTime.AddHours(-12); + beginDateTime = beginDateTime.AddHours(-Constants.HalfDayHourCount); } else { var span = endDateTime - beginDateTime; - if (span.TotalHours > 12) + if (span.TotalHours > Constants.HalfDayHourCount) { - beginDateTime = beginDateTime.AddHours(12); + beginDateTime = beginDateTime.AddHours(Constants.HalfDayHourCount); } } } } } // No 'am' or 'pm' indicator - else if (!hasLeft && !hasRight && beginHour <= 12 && endHour <= 12) + else if (!hasLeft && !hasRight && beginHour <= Constants.HalfDayHourCount && endHour <= Constants.HalfDayHourCount) { if (beginHour > endHour) { - if (beginHour == 12) + if (beginHour == Constants.HalfDayHourCount) { - beginDateTime = beginDateTime.AddHours(-12); + beginDateTime = beginDateTime.AddHours(-Constants.HalfDayHourCount); } else { - endDateTime = endDateTime.AddHours(12); + endDateTime = endDateTime.AddHours(Constants.HalfDayHourCount); } } ret.Comment = Constants.Comment_AmPm; @@ -595,9 +595,9 @@ private DateTimeResolutionResult MergeTwoTimePoints(string text, DateObject refe var beginTime = (DateObject)((DateTimeResolutionResult)pr1.Value).FutureValue; var endTime = (DateObject)((DateTimeResolutionResult)pr2.Value).FutureValue; - if (!string.IsNullOrEmpty(ampmStr2) && ampmStr2.EndsWith(Constants.Comment_AmPm) && endTime <= beginTime && endTime.AddHours(12) > beginTime) + if (!string.IsNullOrEmpty(ampmStr2) && ampmStr2.EndsWith(Constants.Comment_AmPm) && endTime <= beginTime && endTime.AddHours(Constants.HalfDayHourCount) > beginTime) { - endTime = endTime.AddHours(12); + endTime = endTime.AddHours(Constants.HalfDayHourCount); ((DateTimeResolutionResult)pr2.Value).FutureValue = endTime; pr2.TimexStr = $"T{endTime.Hour}"; if (endTime.Minute > 0) @@ -606,9 +606,9 @@ private DateTimeResolutionResult MergeTwoTimePoints(string text, DateObject refe } } - if (!string.IsNullOrEmpty(ampmStr1) && ampmStr1.EndsWith(Constants.Comment_AmPm) && endTime > beginTime.AddHours(12)) + if (!string.IsNullOrEmpty(ampmStr1) && ampmStr1.EndsWith(Constants.Comment_AmPm) && endTime > beginTime.AddHours(Constants.HalfDayHourCount)) { - beginTime = beginTime.AddHours(12); + beginTime = beginTime.AddHours(Constants.HalfDayHourCount); ((DateTimeResolutionResult)pr1.Value).FutureValue = beginTime; pr1.TimexStr = $"T{beginTime.Hour}"; if (beginTime.Minute > 0) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeZoneParser.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeZoneParser.cs index 1250a885bd..f3b6939b6a 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeZoneParser.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Parsers/BaseTimeZoneParser.cs @@ -54,7 +54,7 @@ public int ComputeMinutes(string utcOffset) minutes = 0; } - if (hours > 12) + if (hours > Constants.HalfDayHourCount) { return Constants.InvalidOffsetValue; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimeParserConfiguration.cs index 6fcb8d5dad..2ee4ac7d68 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimeParserConfiguration.cs @@ -88,13 +88,13 @@ public int GetHour(string text, int hour) int result = hour; //TODO: Replace with a regex - if ((trimedText.EndsWith("manha") || trimedText.EndsWith("madrugada")) && hour >= 12) + if ((trimedText.EndsWith("manha") || trimedText.EndsWith("madrugada")) && hour >= Constants.HalfDayHourCount) { - result -= 12; + result -= Constants.HalfDayHourCount; } - else if (!(trimedText.EndsWith("manha") || trimedText.EndsWith("madrugada")) && hour < 12) + else if (!(trimedText.EndsWith("manha") || trimedText.EndsWith("madrugada")) && hour < Constants.HalfDayHourCount) { - result += 12; + result += Constants.HalfDayHourCount; } return result; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimePeriodParserConfiguration.cs index 6670cfd610..aca2be6a86 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseDateTimePeriodParserConfiguration.cs @@ -130,12 +130,12 @@ public bool GetMatchedTimeRange(string text, out string timeStr, out int beginHo { timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.Contains("passado o meio dia") || trimedText.Contains("depois do meio dia")) { timeStr = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("tarde")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimeParserConfiguration.cs index f1525db143..816e9e4551 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimeParserConfiguration.cs @@ -101,22 +101,22 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha var oclockStr = match.Groups["oclock"].Value; if (string.IsNullOrEmpty(oclockStr)) { - var amStr = match.Groups["am"].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; if (!string.IsNullOrEmpty(amStr)) { - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - deltaHour = -12; + deltaHour = -Constants.HalfDayHourCount; } hasAm = true; } - var pmStr = match.Groups["pm"].Value; + var pmStr = match.Groups[Constants.PmGroupName].Value; if (!string.IsNullOrEmpty(pmStr)) { - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - deltaHour = 12; + deltaHour = Constants.HalfDayHourCount; } hasPm = true; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimePeriodParserConfiguration.cs index f264479b73..5e91538ea2 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Portuguese/Parsers/PortugueseTimePeriodParserConfiguration.cs @@ -66,12 +66,12 @@ public bool GetMatchedTimexRange(string text, out string timex, out int beginHou { timex = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.Contains("passado o meio dia") || trimedText.Contains("depois do meio dia")) { timex = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("tarde")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimeParserConfiguration.cs index c8070e44dc..756cb923cc 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimeParserConfiguration.cs @@ -50,8 +50,8 @@ public class SpanishDateTimeParserConfiguration : BaseOptionsConfiguration, IDat public Regex DateNumberConnectorRegex { get; } - public Regex PrepositionRegex { get; } - + public Regex PrepositionRegex { get; } + public Regex ConnectorRegex { get; } public IImmutableDictionary Numbers { get; } @@ -91,13 +91,13 @@ public int GetHour(string text, int hour) int result = hour; //TODO: Replace with a regex - if ((trimedText.EndsWith("mañana") || trimedText.EndsWith("madrugada")) && hour >= 12) + if ((trimedText.EndsWith("mañana") || trimedText.EndsWith("madrugada")) && hour >= Constants.HalfDayHourCount) { - result -= 12; + result -= Constants.HalfDayHourCount; } - else if (!(trimedText.EndsWith("mañana") || trimedText.EndsWith("madrugada")) && hour < 12) + else if (!(trimedText.EndsWith("mañana") || trimedText.EndsWith("madrugada")) && hour < Constants.HalfDayHourCount) { - result += 12; + result += Constants.HalfDayHourCount; } return result; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimePeriodParserConfiguration.cs index 1dededff3b..0ddf86490e 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimePeriodParserConfiguration.cs @@ -130,12 +130,12 @@ public bool GetMatchedTimeRange(string text, out string timeStr, out int beginHo { timeStr = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.Contains("pasado mediodia") || trimedText.Contains("pasado el mediodia")) { timeStr = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("tarde")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimeParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimeParserConfiguration.cs index f362501826..a2bfd6de8d 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimeParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimeParserConfiguration.cs @@ -99,22 +99,22 @@ public void AdjustBySuffix(string suffix, ref int hour, ref int min, ref bool ha var oclockStr = match.Groups["oclock"].Value; if (string.IsNullOrEmpty(oclockStr)) { - var amStr = match.Groups["am"].Value; + var amStr = match.Groups[Constants.AmGroupName].Value; if (!string.IsNullOrEmpty(amStr)) { - if (hour >= 12) + if (hour >= Constants.HalfDayHourCount) { - deltaHour = -12; + deltaHour = -Constants.HalfDayHourCount; } hasAm = true; } - var pmStr = match.Groups["pm"].Value; + var pmStr = match.Groups[Constants.PmGroupName].Value; if (!string.IsNullOrEmpty(pmStr)) { - if (hour < 12) + if (hour < Constants.HalfDayHourCount) { - deltaHour = 12; + deltaHour = Constants.HalfDayHourCount; } hasPm = true; } diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimePeriodParserConfiguration.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimePeriodParserConfiguration.cs index d4f696a92f..fdfdfa2bf0 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimePeriodParserConfiguration.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishTimePeriodParserConfiguration.cs @@ -66,12 +66,12 @@ public bool GetMatchedTimexRange(string text, out string timex, out int beginHou { timex = "TMO"; beginHour = 8; - endHour = 12; + endHour = Constants.HalfDayHourCount; } else if (trimedText.Contains("pasado mediodia") || trimedText.Contains("pasado el mediodia")) { timex = "TAF"; - beginHour = 12; + beginHour = Constants.HalfDayHourCount; endHour = 16; } else if (trimedText.EndsWith("tarde")) diff --git a/.NET/Microsoft.Recognizers.Text.DateTime/Utilities/FormatUtil.cs b/.NET/Microsoft.Recognizers.Text.DateTime/Utilities/FormatUtil.cs index b7f82bd51c..1f1a11697d 100644 --- a/.NET/Microsoft.Recognizers.Text.DateTime/Utilities/FormatUtil.cs +++ b/.NET/Microsoft.Recognizers.Text.DateTime/Utilities/FormatUtil.cs @@ -5,36 +5,36 @@ namespace Microsoft.Recognizers.Text.DateTime { - public class FormatUtil - { - public static readonly Regex HourTimexRegex = new Regex(@"(?(); - - int lastPos = 0; + } + + public static string FormatDate(System.DateTime date) + { + return string.Join("-", date.Year.ToString("D4"), date.Month.ToString("D2"), date.Day.ToString("D2")); + } + + public static string FormatTime(System.DateTime time) + { + return string.Join(":", time.Hour.ToString("D2"), time.Minute.ToString("D2"), time.Second.ToString("D2")); + } + + public static string FormatDateTime(System.DateTime datetime) + { + return FormatDate(datetime) + " " + FormatTime(datetime); + } + + public static string AllStringToPm(string timeStr) + { + var matches = HourTimexRegex.Matches(timeStr); + var splited = new List(); + + int lastPos = 0; foreach (Match match in matches) - { - if (lastPos != match.Index) - { - splited.Add(timeStr.Substring(lastPos, match.Index - lastPos)); + { + if (lastPos != match.Index) + { + splited.Add(timeStr.Substring(lastPos, match.Index - lastPos)); } splited.Add(timeStr.Substring(match.Index, match.Length)); lastPos = match.Index + match.Length; @@ -123,53 +123,53 @@ public static string AllStringToPm(string timeStr) splited.Add(timeStr.Substring(lastPos)); } - for (int i = 0; i < splited.Count; i += 1) - { - if (HourTimexRegex.IsMatch(splited[i])) - { - splited[i] = ToPm(splited[i]); - } - } - - // Modify weekDay timex for the cases which cross day boundary - if (splited.Count >= 4) - { - var weekDayStartMatch = WeekDayTimexRegex.Match(splited[0]).Groups[1]; - var weekDayEndMatch = WeekDayTimexRegex.Match(splited[2]).Groups[1]; - var hourStartMatch = HourTimexRegex.Match(splited[1]).Groups[1]; - var hourEndMatch = HourTimexRegex.Match(splited[3]).Groups[1]; - - if (int.TryParse(weekDayStartMatch.Value, out var weekDayStart) && - int.TryParse(weekDayEndMatch.Value, out var weekDayEnd) && - int.TryParse(hourStartMatch.Value, out var hourStart) && - int.TryParse(hourEndMatch.Value, out var hourEnd)) - { - if (hourEnd < hourStart && weekDayStart == weekDayEnd) - { - weekDayEnd = weekDayEnd == Constants.WeekDayCount ? 1 : weekDayEnd + 1; - splited[2] = splited[2].Substring(0, weekDayEndMatch.Index) + weekDayEnd; - } - } - } - - return string.Concat(splited); - } - - public static string ToPm(string timeStr) - { - bool hasT = false; - if (timeStr.StartsWith("T")) - { - hasT = true; - timeStr = timeStr.Substring(1); - } - - var splited = timeStr.Split(':'); - var hour = int.Parse(splited[0]); - hour = hour >= 12 ? hour - 12: hour + 12; - splited[0] = hour.ToString("D2"); - - return hasT ? "T" + string.Join(":", splited) : string.Join(":", splited); + for (int i = 0; i < splited.Count; i += 1) + { + if (HourTimexRegex.IsMatch(splited[i])) + { + splited[i] = ToPm(splited[i]); + } + } + + // Modify weekDay timex for the cases which cross day boundary + if (splited.Count >= 4) + { + var weekDayStartMatch = WeekDayTimexRegex.Match(splited[0]).Groups[1]; + var weekDayEndMatch = WeekDayTimexRegex.Match(splited[2]).Groups[1]; + var hourStartMatch = HourTimexRegex.Match(splited[1]).Groups[1]; + var hourEndMatch = HourTimexRegex.Match(splited[3]).Groups[1]; + + if (int.TryParse(weekDayStartMatch.Value, out var weekDayStart) && + int.TryParse(weekDayEndMatch.Value, out var weekDayEnd) && + int.TryParse(hourStartMatch.Value, out var hourStart) && + int.TryParse(hourEndMatch.Value, out var hourEnd)) + { + if (hourEnd < hourStart && weekDayStart == weekDayEnd) + { + weekDayEnd = weekDayEnd == Constants.WeekDayCount ? 1 : weekDayEnd + 1; + splited[2] = splited[2].Substring(0, weekDayEndMatch.Index) + weekDayEnd; + } + } + } + + return string.Concat(splited); + } + + public static string ToPm(string timeStr) + { + bool hasT = false; + if (timeStr.StartsWith("T")) + { + hasT = true; + timeStr = timeStr.Substring(1); + } + + var splited = timeStr.Split(':'); + var hour = int.Parse(splited[0]); + hour = hour >= Constants.HalfDayHourCount ? hour - Constants.HalfDayHourCount: hour + Constants.HalfDayHourCount; + splited[0] = hour.ToString("D2"); + + return hasT ? "T" + string.Join(":", splited) : string.Join(":", splited); } public static string ToIsoWeekTimex(System.DateTime monday) diff --git a/JavaScript/packages/recognizers-date-time/src/dateTime/baseTime.ts b/JavaScript/packages/recognizers-date-time/src/dateTime/baseTime.ts index bd6d73f139..e349b58033 100644 --- a/JavaScript/packages/recognizers-date-time/src/dateTime/baseTime.ts +++ b/JavaScript/packages/recognizers-date-time/src/dateTime/baseTime.ts @@ -270,23 +270,27 @@ export class BaseTimeExtractor implements IDateTimeExtractor { // adjust by desc string let descStr = match.groups('desc').value.toLowerCase(); - if (!StringUtility.isNullOrWhitespace(descStr)) { - if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0 - || RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0) { - if (hour >= 12) { - hour -= 12; - } - if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) { - hasAm = true; - } - } - else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0) { - if (hour < 12) { - hour += 12; - } - hasPm = true; + if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0 + || RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0 + || !StringUtility.isNullOrEmpty(match.groups('iam').value)) { + + if (hour >= 12) { + hour -= 12; + } + + if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) { + hasAm = true; } } + else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0 + || !StringUtility.isNullOrEmpty(match.groups('ipm').value)) { + + if (hour < 12) { + hour += 12; + } + + hasPm = true; + } // adjust min by prefix let timePrefix = match.groups('prefix').value.toLowerCase(); diff --git a/JavaScript/packages/recognizers-date-time/src/resources/englishDateTime.ts b/JavaScript/packages/recognizers-date-time/src/resources/englishDateTime.ts index 9a475c970e..8f10c5284c 100644 --- a/JavaScript/packages/recognizers-date-time/src/resources/englishDateTime.ts +++ b/JavaScript/packages/recognizers-date-time/src/resources/englishDateTime.ts @@ -115,7 +115,7 @@ export namespace EnglishDateTime { export const MidafternoonRegex = `(?midafternoon|mid-afternoon|mid afternoon)`; export const MiddayRegex = `(?midday|mid-day|mid day|((12\\s)?noon))`; export const MidTimeRegex = `(?(${MidnightRegex}|${MidmorningRegex}|${MidafternoonRegex}|${MiddayRegex}))`; - export const AtRegex = `\\b(((?<=\\bat\\s+)(${WrittenTimeRegex}|${HourNumRegex}|${BaseDateTime.HourRegex}|${MidTimeRegex}))|${MidTimeRegex})\\b`; + export const AtRegex = `\\b(((?<=\\bat\\s+)(${WrittenTimeRegex}|${HourNumRegex}|${BaseDateTime.HourRegex}(\\s*((?a)|(?p)))?|${MidTimeRegex}))|${MidTimeRegex})\\b`; export const IshRegex = `\\b(${BaseDateTime.HourRegex}(-|——)?ish|noonish|noon)\\b`; export const TimeUnitRegex = `([^A-Za-z]{1,}|\\b)(?hours|hour|hrs|hr|h|minutes|minute|mins|min|seconds|second|secs|sec)\\b`; export const RestrictedTimeUnitRegex = `(?hour|minute)\\b`; @@ -227,8 +227,8 @@ export namespace EnglishDateTime { export const DecadeWithCenturyRegex = `(the\\s+)?(((?\\d|1\\d|2\\d)?(')?(?\\d0)(')?s)|((${CenturyRegex}(\\s+|-)(and\\s+)?)?${DecadeRegex})|(${CenturyRegex}(\\s+|-)(and\\s+)?(?tens|hundreds)))`; export const RelativeDecadeRegex = `\\b((the\\s+)?${RelativeRegex}\\s+((?[\\w,]+)\\s+)?decades?)\\b`; export const YearAfterRegex = `\\b(or\\s+(above|after))\\b`; - export const YearPeriodRegex = `(((from|during|in|between)\\s+)?${YearRegex}\\s*(${TillRegex}|${RangeConnectorRegex})\\s*${YearRegex})`; - export const ComplexDatePeriodRegex = `((from|during|in|between)\\s+)?(?.+)\\s*(${TillRegex}|${RangeConnectorRegex})\\s*(?.+)`; + export const YearPeriodRegex = `((((from|during|in)\\s+)?${YearRegex}\\s*(${TillRegex})\\s*${YearRegex})|(((between)\\s+)${YearRegex}\\s*(${RangeConnectorRegex})\\s*${YearRegex}))`; + export const ComplexDatePeriodRegex = `(((from|during|in)\\s+)?(?.+)\\s*(${TillRegex})\\s*(?.+)|((between)\\s+)(?.+)\\s*(${RangeConnectorRegex})\\s*(?.+))`; export const UnitMap: ReadonlyMap = new Map([["years", "Y"],["year", "Y"],["months", "MON"],["month", "MON"],["weeks", "W"],["week", "W"],["days", "D"],["day", "D"],["hours", "H"],["hour", "H"],["hrs", "H"],["hr", "H"],["h", "H"],["minutes", "M"],["minute", "M"],["mins", "M"],["min", "M"],["seconds", "S"],["second", "S"],["secs", "S"],["sec", "S"]]); export const UnitValueMap: ReadonlyMap = new Map([["years", 31536000],["year", 31536000],["months", 2592000],["month", 2592000],["weeks", 604800],["week", 604800],["days", 86400],["day", 86400],["hours", 3600],["hour", 3600],["hrs", 3600],["hr", 3600],["h", 3600],["minutes", 60],["minute", 60],["mins", 60],["min", 60],["seconds", 1],["second", 1],["secs", 1],["sec", 1]]); export const SeasonMap: ReadonlyMap = new Map([["spring", "SP"],["summer", "SU"],["fall", "FA"],["autumn", "FA"],["winter", "WI"]]); diff --git a/Patterns/English/English-DateTime.yaml b/Patterns/English/English-DateTime.yaml index 3332636ad5..c2333c7d70 100644 --- a/Patterns/English/English-DateTime.yaml +++ b/Patterns/English/English-DateTime.yaml @@ -273,7 +273,7 @@ MidTimeRegex: !nestedRegex def: (?({MidnightRegex}|{MidmorningRegex}|{MidafternoonRegex}|{MiddayRegex})) references: [ MidnightRegex, MidmorningRegex, MidafternoonRegex, MiddayRegex ] AtRegex: !nestedRegex - def: \b(((?<=\bat\s+)({WrittenTimeRegex}|{HourNumRegex}|{BaseDateTime.HourRegex}|{MidTimeRegex}))|{MidTimeRegex})\b + def: \b(((?<=\bat\s+)({WrittenTimeRegex}|{HourNumRegex}|{BaseDateTime.HourRegex}(\s*((?a)|(?p)))?|{MidTimeRegex}))|{MidTimeRegex})\b references: [ WrittenTimeRegex, HourNumRegex, BaseDateTime.HourRegex, MidTimeRegex ] IshRegex: !nestedRegex def: '\b({BaseDateTime.HourRegex}(-|——)?ish|noonish|noon)\b' diff --git a/Python/libraries/recognizers-date-time/recognizers_date_time/date_time/base_time.py b/Python/libraries/recognizers-date-time/recognizers_date_time/date_time/base_time.py index b976fd9c9e..ce0268c171 100644 --- a/Python/libraries/recognizers-date-time/recognizers_date_time/date_time/base_time.py +++ b/Python/libraries/recognizers-date-time/recognizers_date_time/date_time/base_time.py @@ -260,20 +260,22 @@ def match_to_time(self, match: Match, reference: datetime) -> DateTimeResolution if sec_str.strip(): second = int(sec_str) has_seconds = True + #adjust by desc string - desc_str = RegExpUtility.get_group(match, 'desc') - desc_str = desc_str.lower() - if desc_str.strip(): - if any([regex.search(self.config.utility_configuration.am_desc_regex, desc_str), - regex.search(self.config.utility_configuration.am_pm_desc_regex, desc_str)]): - if hour >= 12: - hour -= 12 - if regex.search(self.config.utility_configuration.am_pm_desc_regex, desc_str) is None: - has_am = True - elif regex.search(self.config.utility_configuration.pm_desc__regex, desc_str) is not None: - if hour < 12: - hour += 12 - has_pm = True + desc_str = RegExpUtility.get_group(match, 'desc').lower() + + if any([regex.search(self.config.utility_configuration.am_desc_regex, desc_str), + regex.search(self.config.utility_configuration.am_pm_desc_regex, desc_str)]) or RegExpUtility.get_group( + match, 'iam'): + if hour >= 12: + hour -= 12 + if regex.search(self.config.utility_configuration.am_pm_desc_regex, desc_str) is None: + has_am = True + elif regex.search(self.config.utility_configuration.pm_desc__regex, + desc_str) is not None or RegExpUtility.get_group(match, 'ipm'): + if hour < 12: + hour += 12 + has_pm = True #adjust min by prefix time_prefix = RegExpUtility.get_group(match, 'prefix') diff --git a/Python/libraries/recognizers-date-time/recognizers_date_time/resources/english_date_time.py b/Python/libraries/recognizers-date-time/recognizers_date_time/resources/english_date_time.py index 80f18500bb..0bf9052655 100644 --- a/Python/libraries/recognizers-date-time/recognizers_date_time/resources/english_date_time.py +++ b/Python/libraries/recognizers-date-time/recognizers_date_time/resources/english_date_time.py @@ -116,7 +116,7 @@ class EnglishDateTime: MidafternoonRegex = f'(?midafternoon|mid-afternoon|mid afternoon)' MiddayRegex = f'(?midday|mid-day|mid day|((12\\s)?noon))' MidTimeRegex = f'(?({MidnightRegex}|{MidmorningRegex}|{MidafternoonRegex}|{MiddayRegex}))' - AtRegex = f'\\b(((?<=\\bat\\s+)({WrittenTimeRegex}|{HourNumRegex}|{BaseDateTime.HourRegex}|{MidTimeRegex}))|{MidTimeRegex})\\b' + AtRegex = f'\\b(((?<=\\bat\\s+)({WrittenTimeRegex}|{HourNumRegex}|{BaseDateTime.HourRegex}(\\s*((?a)|(?p)))?|{MidTimeRegex}))|{MidTimeRegex})\\b' IshRegex = f'\\b({BaseDateTime.HourRegex}(-|——)?ish|noonish|noon)\\b' TimeUnitRegex = f'([^A-Za-z]{{1,}}|\\b)(?hours|hour|hrs|hr|h|minutes|minute|mins|min|seconds|second|secs|sec)\\b' RestrictedTimeUnitRegex = f'(?hour|minute)\\b' @@ -228,8 +228,8 @@ class EnglishDateTime: DecadeWithCenturyRegex = f'(the\\s+)?(((?\\d|1\\d|2\\d)?(\')?(?\\d0)(\')?s)|(({CenturyRegex}(\\s+|-)(and\\s+)?)?{DecadeRegex})|({CenturyRegex}(\\s+|-)(and\\s+)?(?tens|hundreds)))' RelativeDecadeRegex = f'\\b((the\\s+)?{RelativeRegex}\\s+((?[\\w,]+)\\s+)?decades?)\\b' YearAfterRegex = f'\\b(or\\s+(above|after))\\b' - YearPeriodRegex = f'(((from|during|in|between)\\s+)?{YearRegex}\\s*({TillRegex}|{RangeConnectorRegex})\\s*{YearRegex})' - ComplexDatePeriodRegex = f'((from|during|in|between)\\s+)?(?.+)\\s*({TillRegex}|{RangeConnectorRegex})\\s*(?.+)' + YearPeriodRegex = f'((((from|during|in)\\s+)?{YearRegex}\\s*({TillRegex})\\s*{YearRegex})|(((between)\\s+){YearRegex}\\s*({RangeConnectorRegex})\\s*{YearRegex}))' + ComplexDatePeriodRegex = f'(((from|during|in)\\s+)?(?.+)\\s*({TillRegex})\\s*(?.+)|((between)\\s+)(?.+)\\s*({RangeConnectorRegex})\\s*(?.+))' UnitMap = dict([('years', 'Y'), ('year', 'Y'), ('months', 'MON'), diff --git a/Specs/DateTime/English/DateTimeExtractor.json b/Specs/DateTime/English/DateTimeExtractor.json index 5a4b3a1310..4d504f9c9b 100644 --- a/Specs/DateTime/English/DateTimeExtractor.json +++ b/Specs/DateTime/English/DateTimeExtractor.json @@ -805,5 +805,27 @@ "Length": 12 } ] + }, + { + "Input": "Please book Skype call today at 9a.", + "Results": [ + { + "Text": "today at 9a", + "Type": "datetime", + "Start": 23, + "Length": 11 + } + ] + }, + { + "Input": "Please book Skype call today at 9p.", + "Results": [ + { + "Text": "today at 9p", + "Type": "datetime", + "Start": 23, + "Length": 11 + } + ] } -] \ No newline at end of file +] diff --git a/Specs/DateTime/English/DateTimeModel.json b/Specs/DateTime/English/DateTimeModel.json index 50562258b6..a1d1ec3c72 100644 --- a/Specs/DateTime/English/DateTimeModel.json +++ b/Specs/DateTime/English/DateTimeModel.json @@ -4562,5 +4562,53 @@ } } ] + }, + { + "Input": "Please book Skype call today at 9a.", + "Context": { + "ReferenceDateTime": "2018-06-28T00:00:00" + }, + "NotSupported": "java", + "Results": [ + { + "Text": "today at 9a", + "Start": 23, + "End": 33, + "TypeName": "datetimeV2.datetime", + "Resolution": { + "values": [ + { + "timex": "2018-06-28T09", + "type": "datetime", + "value": "2018-06-28 09:00:00" + } + ] + } + } + ] + }, + { + "Input": "Please book Skype call today at 9p.", + "Context": { + "ReferenceDateTime": "2018-06-28T00:00:00" + }, + "NotSupported": "java", + "Results": [ + { + "Text": "today at 9p", + "Start": 23, + "End": 33, + "TypeName": "datetimeV2.datetime", + "Resolution": { + "values": [ + { + "timex": "2018-06-28T21", + "type": "datetime", + "value": "2018-06-28 21:00:00" + } + ] + } + } + ] } ] \ No newline at end of file diff --git a/Specs/DateTime/English/DateTimeModelComplexCalendar.json b/Specs/DateTime/English/DateTimeModelComplexCalendar.json index 17eacd47ef..91dcd32c63 100644 --- a/Specs/DateTime/English/DateTimeModelComplexCalendar.json +++ b/Specs/DateTime/English/DateTimeModelComplexCalendar.json @@ -3986,5 +3986,53 @@ } } ] + }, + { + "Input": "Please book Skype call today at 9a.", + "Context": { + "ReferenceDateTime": "2018-06-28T00:00:00" + }, + "NotSupported": "java", + "Results": [ + { + "Text": "today at 9a", + "Start": 23, + "End": 33, + "TypeName": "datetimeV2.datetime", + "Resolution": { + "values": [ + { + "timex": "2018-06-28T09", + "type": "datetime", + "value": "2018-06-28 09:00:00" + } + ] + } + } + ] + }, + { + "Input": "Please book Skype call today at 9p.", + "Context": { + "ReferenceDateTime": "2018-06-28T00:00:00" + }, + "NotSupported": "java", + "Results": [ + { + "Text": "today at 9p", + "Start": 23, + "End": 33, + "TypeName": "datetimeV2.datetime", + "Resolution": { + "values": [ + { + "timex": "2018-06-28T21", + "type": "datetime", + "value": "2018-06-28 21:00:00" + } + ] + } + } + ] } ] \ No newline at end of file diff --git a/Specs/DateTime/English/TimeExtractor.json b/Specs/DateTime/English/TimeExtractor.json index 616ed67a12..a9c05a177b 100644 --- a/Specs/DateTime/English/TimeExtractor.json +++ b/Specs/DateTime/English/TimeExtractor.json @@ -846,5 +846,42 @@ { "Input": "Cortana, schedule a meeting for next week.\nBentonville, AR 72716 P: 479.277", "Results": [] + }, + { + "Input": "9p is suitable for me.", + "Results": [ + { + "Text": "9p", + "Type": "time", + "Start": 0, + "Length": 2 + } + ] + }, + { + "Input": "9a is suitable for me.", + "Results": [] + }, + { + "Input": "I'll be back at 9p.", + "Results": [ + { + "Text": "9p", + "Type": "time", + "Start": 16, + "Length": 2 + } + ] + }, + { + "Input": "I'll be back at 9a.", + "Results": [ + { + "Text": "9a", + "Type": "time", + "Start": 16, + "Length": 2 + } + ] } ] \ No newline at end of file