-
Notifications
You must be signed in to change notification settings - Fork 431
[ES DateTimeV2] Fixed "anoche" not properly handled in resolution (#2473) #2503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
363 changes: 187 additions & 176 deletions
363
...Microsoft.Recognizers.Text.DateTime/Spanish/Parsers/SpanishDateTimeParserConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,176 +1,187 @@ | ||
| using System; | ||
| using System.Collections.Immutable; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| using Microsoft.Recognizers.Definitions.Spanish; | ||
| using Microsoft.Recognizers.Text.DateTime.Utilities; | ||
|
|
||
| namespace Microsoft.Recognizers.Text.DateTime.Spanish | ||
| { | ||
| public class SpanishDateTimeParserConfiguration : BaseDateTimeOptionsConfiguration, IDateTimeParserConfiguration | ||
| { | ||
|
|
||
| private const RegexOptions RegexFlags = RegexOptions.Singleline | RegexOptions.ExplicitCapture; | ||
|
|
||
| public SpanishDateTimeParserConfiguration(ICommonDateTimeParserConfiguration config) | ||
| : base(config) | ||
| { | ||
| TokenBeforeDate = DateTimeDefinitions.TokenBeforeDate; | ||
| TokenBeforeTime = DateTimeDefinitions.TokenBeforeTime; | ||
| DateExtractor = config.DateExtractor; | ||
| TimeExtractor = config.TimeExtractor; | ||
| DateParser = config.DateParser; | ||
| TimeParser = config.TimeParser; | ||
|
|
||
| NowRegex = SpanishDateTimeExtractorConfiguration.NowRegex; | ||
|
|
||
| AMTimeRegex = new Regex(DateTimeDefinitions.AmTimeRegex, RegexFlags); | ||
| PMTimeRegex = new Regex(DateTimeDefinitions.PmTimeRegex, RegexFlags); | ||
|
|
||
| SimpleTimeOfTodayAfterRegex = SpanishDateTimeExtractorConfiguration.SimpleTimeOfTodayAfterRegex; | ||
| SimpleTimeOfTodayBeforeRegex = SpanishDateTimeExtractorConfiguration.SimpleTimeOfTodayBeforeRegex; | ||
| SpecificTimeOfDayRegex = SpanishDateTimeExtractorConfiguration.SpecificTimeOfDayRegex; | ||
| SpecificEndOfRegex = SpanishDateTimeExtractorConfiguration.SpecificEndOfRegex; | ||
| UnspecificEndOfRegex = SpanishDateTimeExtractorConfiguration.UnspecificEndOfRegex; | ||
| UnitRegex = SpanishDateTimeExtractorConfiguration.UnitRegex; | ||
| DateNumberConnectorRegex = SpanishDateTimeExtractorConfiguration.DateNumberConnectorRegex; | ||
| YearRegex = SpanishDateTimeExtractorConfiguration.YearRegex; | ||
|
|
||
| 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 string TokenBeforeDate { get; } | ||
|
|
||
| public string TokenBeforeTime { get; } | ||
|
|
||
| public IDateExtractor 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<string, string> 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 SpecificEndOfRegex { get; } | ||
|
|
||
| public Regex UnspecificEndOfRegex { get; } | ||
|
|
||
| public Regex UnitRegex { get; } | ||
|
|
||
| public Regex DateNumberConnectorRegex { get; } | ||
|
|
||
| public Regex PrepositionRegex { get; } | ||
|
|
||
| public Regex ConnectorRegex { get; } | ||
|
|
||
| public Regex YearRegex { get; } | ||
|
|
||
| public IImmutableDictionary<string, int> Numbers { get; } | ||
|
|
||
| public IDateTimeUtilityConfiguration UtilityConfiguration { get; } | ||
|
|
||
| public int GetHour(string text, int hour) | ||
| { | ||
| var trimmedText = text.Trim(); | ||
| int result = hour; | ||
|
|
||
| // @TODO: move hardcoded values to resources file | ||
| if ((trimmedText.EndsWith("mañana", StringComparison.Ordinal) || trimmedText.EndsWith("madrugada", StringComparison.Ordinal)) && | ||
| hour >= Constants.HalfDayHourCount) | ||
| { | ||
| result -= Constants.HalfDayHourCount; | ||
| } | ||
| else if (!(trimmedText.EndsWith("mañana", StringComparison.Ordinal) || trimmedText.EndsWith("madrugada", StringComparison.Ordinal)) && | ||
| hour < Constants.HalfDayHourCount) | ||
| { | ||
| result += Constants.HalfDayHourCount; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public bool GetMatchedNowTimex(string text, out string timex) | ||
| { | ||
|
|
||
| var trimmedText = text.Trim(); | ||
|
|
||
| // @TODO move hardcoded values to resources file | ||
| if (trimmedText.EndsWith("ahora", StringComparison.Ordinal) || trimmedText.EndsWith("mismo", StringComparison.Ordinal) || trimmedText.EndsWith("momento", StringComparison.Ordinal)) | ||
| { | ||
| timex = "PRESENT_REF"; | ||
| } | ||
| else if (trimmedText.EndsWith("posible", StringComparison.Ordinal) || trimmedText.EndsWith("pueda", StringComparison.Ordinal) || | ||
| trimmedText.EndsWith("puedas", StringComparison.Ordinal) || trimmedText.EndsWith("podamos", StringComparison.Ordinal) || trimmedText.EndsWith("puedan", StringComparison.Ordinal)) | ||
| { | ||
| timex = "FUTURE_REF"; | ||
| } | ||
| else if (trimmedText.EndsWith("mente", StringComparison.Ordinal)) | ||
| { | ||
| timex = "PAST_REF"; | ||
| } | ||
| else | ||
| { | ||
| timex = null; | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public int GetSwiftDay(string text) | ||
| { | ||
| var trimmedText = text.Trim(); | ||
| var swift = 0; | ||
|
|
||
| // @TODO move hardcoded values to resources file | ||
| if (SpanishDatePeriodParserConfiguration.PreviousPrefixRegex.IsMatch(trimmedText) || | ||
| trimmedText.StartsWith("anoche", StringComparison.Ordinal)) | ||
| { | ||
| swift = -1; | ||
| } | ||
| else if (SpanishDatePeriodParserConfiguration.NextPrefixRegex.IsMatch(trimmedText)) | ||
| { | ||
| swift = 1; | ||
| } | ||
|
|
||
| return swift; | ||
| } | ||
|
|
||
| public bool ContainsAmbiguousToken(string text, string matchedText) | ||
| { | ||
| // @TODO move hardcoded values to resources file | ||
| return text.Contains("esta mañana") && matchedText.Contains("mañana"); | ||
| } | ||
| } | ||
| } | ||
| using System; | ||
| using System.Collections.Immutable; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| using Microsoft.Recognizers.Definitions.Spanish; | ||
| using Microsoft.Recognizers.Text.DateTime.Utilities; | ||
| using Microsoft.Recognizers.Text.Utilities; | ||
|
|
||
| namespace Microsoft.Recognizers.Text.DateTime.Spanish | ||
| { | ||
| public class SpanishDateTimeParserConfiguration : BaseDateTimeOptionsConfiguration, IDateTimeParserConfiguration | ||
| { | ||
|
|
||
| private const RegexOptions RegexFlags = RegexOptions.Singleline | RegexOptions.ExplicitCapture; | ||
|
|
||
| public SpanishDateTimeParserConfiguration(ICommonDateTimeParserConfiguration config) | ||
| : base(config) | ||
| { | ||
| TokenBeforeDate = DateTimeDefinitions.TokenBeforeDate; | ||
| TokenBeforeTime = DateTimeDefinitions.TokenBeforeTime; | ||
| DateExtractor = config.DateExtractor; | ||
| TimeExtractor = config.TimeExtractor; | ||
| DateParser = config.DateParser; | ||
| TimeParser = config.TimeParser; | ||
|
|
||
| NowRegex = SpanishDateTimeExtractorConfiguration.NowRegex; | ||
|
|
||
| AMTimeRegex = new Regex(DateTimeDefinitions.AmTimeRegex, RegexFlags); | ||
| PMTimeRegex = new Regex(DateTimeDefinitions.PmTimeRegex, RegexFlags); | ||
| NightTimeRegex = new Regex(DateTimeDefinitions.NightTimeRegex, RegexFlags); | ||
| LastNightTimeRegex = new Regex(DateTimeDefinitions.LastNightTimeRegex, RegexFlags); | ||
| NowTimeRegex = new Regex(DateTimeDefinitions.NowTimeRegex, RegexFlags); | ||
| RecentlyTimeRegex = new Regex(DateTimeDefinitions.RecentlyTimeRegex, RegexFlags); | ||
| AsapTimeRegex = new Regex(DateTimeDefinitions.AsapTimeRegex, RegexFlags); | ||
|
|
||
| SimpleTimeOfTodayAfterRegex = SpanishDateTimeExtractorConfiguration.SimpleTimeOfTodayAfterRegex; | ||
| SimpleTimeOfTodayBeforeRegex = SpanishDateTimeExtractorConfiguration.SimpleTimeOfTodayBeforeRegex; | ||
| SpecificTimeOfDayRegex = SpanishDateTimeExtractorConfiguration.SpecificTimeOfDayRegex; | ||
| SpecificEndOfRegex = SpanishDateTimeExtractorConfiguration.SpecificEndOfRegex; | ||
| UnspecificEndOfRegex = SpanishDateTimeExtractorConfiguration.UnspecificEndOfRegex; | ||
| UnitRegex = SpanishDateTimeExtractorConfiguration.UnitRegex; | ||
| DateNumberConnectorRegex = SpanishDateTimeExtractorConfiguration.DateNumberConnectorRegex; | ||
| YearRegex = SpanishDateTimeExtractorConfiguration.YearRegex; | ||
|
|
||
| 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 string TokenBeforeDate { get; } | ||
|
|
||
| public string TokenBeforeTime { get; } | ||
|
|
||
| public IDateExtractor 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<string, string> UnitMap { get; } | ||
|
|
||
| public Regex NowRegex { get; } | ||
|
|
||
| public Regex AMTimeRegex { get; } | ||
|
|
||
| public Regex PMTimeRegex { get; } | ||
|
|
||
| public Regex NightTimeRegex { get; } | ||
|
|
||
| public Regex LastNightTimeRegex { get; } | ||
|
|
||
| public Regex NowTimeRegex { get; } | ||
|
|
||
| public Regex RecentlyTimeRegex { get; } | ||
|
|
||
| public Regex AsapTimeRegex { get; } | ||
|
|
||
| public Regex SimpleTimeOfTodayAfterRegex { get; } | ||
|
|
||
| public Regex SimpleTimeOfTodayBeforeRegex { get; } | ||
|
|
||
| public Regex SpecificTimeOfDayRegex { get; } | ||
|
|
||
| public Regex SpecificEndOfRegex { get; } | ||
|
|
||
| public Regex UnspecificEndOfRegex { get; } | ||
|
|
||
| public Regex UnitRegex { get; } | ||
|
|
||
| public Regex DateNumberConnectorRegex { get; } | ||
|
|
||
| public Regex PrepositionRegex { get; } | ||
|
|
||
| public Regex ConnectorRegex { get; } | ||
|
|
||
| public Regex YearRegex { get; } | ||
|
|
||
| public IImmutableDictionary<string, int> Numbers { get; } | ||
|
|
||
| public IDateTimeUtilityConfiguration UtilityConfiguration { get; } | ||
|
|
||
| public int GetHour(string text, int hour) | ||
| { | ||
| int result = hour; | ||
|
|
||
| var trimmedText = text.Trim(); | ||
|
|
||
| if (AMTimeRegex.MatchEnd(trimmedText, trim: true).Success && hour >= Constants.HalfDayHourCount) | ||
| { | ||
| result -= Constants.HalfDayHourCount; | ||
| } | ||
| else if (!AMTimeRegex.MatchEnd(trimmedText, trim: true).Success && hour < Constants.HalfDayHourCount && | ||
| !(NightTimeRegex.MatchEnd(trimmedText, trim: true).Success && hour < Constants.QuarterDayHourCount)) | ||
| { | ||
| result += Constants.HalfDayHourCount; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public bool GetMatchedNowTimex(string text, out string timex) | ||
| { | ||
| var trimmedText = text.Trim(); | ||
|
|
||
| if (NowTimeRegex.MatchEnd(trimmedText, trim: true).Success) | ||
| { | ||
| timex = "PRESENT_REF"; | ||
| } | ||
| else if (RecentlyTimeRegex.MatchEnd(trimmedText, trim: true).Success) | ||
| { | ||
| timex = "PAST_REF"; | ||
| } | ||
| else if (AsapTimeRegex.MatchEnd(trimmedText, trim: true).Success) | ||
| { | ||
| timex = "FUTURE_REF"; | ||
| } | ||
| else | ||
| { | ||
| timex = null; | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public int GetSwiftDay(string text) | ||
| { | ||
| var trimmedText = text.Trim(); | ||
| var swift = 0; | ||
|
|
||
| if (SpanishDatePeriodParserConfiguration.PreviousPrefixRegex.IsMatch(trimmedText) || | ||
| LastNightTimeRegex.IsMatch(trimmedText)) | ||
| { | ||
| swift = -1; | ||
| } | ||
| else if (SpanishDatePeriodParserConfiguration.NextPrefixRegex.IsMatch(trimmedText)) | ||
| { | ||
| swift = 1; | ||
| } | ||
|
|
||
| return swift; | ||
| } | ||
|
|
||
| public bool ContainsAmbiguousToken(string text, string matchedText) | ||
| { | ||
| // @TODO move hardcoded values to resources file | ||
| return text.Contains("esta mañana") && matchedText.Contains("mañana"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please be careful to not modify line endings unless intentional.