Skip to content

Commit

Permalink
[.NET] Refactor BaseDateExtractor to remove wrongfully merged perf hi…
Browse files Browse the repository at this point in the history
…t code (#1819)
  • Loading branch information
songwenhao1 authored and tellarin committed Aug 23, 2019
1 parent f3c0291 commit a640186
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonth = $@"^\s*(van|in)\s*{MonthRegex}";
public static readonly string MonthEnd = $@"{MonthRegex}\s*(de)?\s*$";
public static readonly string WeekDayEnd = $@"(deze\s+)?{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public const string RangeUnitRegex = @"\b(?<unit>jaren|jaar|maanden|maand|weken|week)\b";
public const string HourNumRegex = @"\b(?<hournum>nul|een|één|twee|drie|vier|vijf|zes|zeven|acht|negen|tien|elf|twaalf|dertien|veertien|vijftien|zestien|zeventien|achttien|negentien|twintig|eenentwintig|éénentwintig|tweeentwintig|tweeëntwintig|drieëntwintig|vierentwintig)\b";
public const string MinuteNumRegex = @"(?<minnum>nul|een|één|twee|drie|vier|vijf|zes|zeven|acht|negen|tien|elf|twaalf|dertien|veertien|vijftien|zestien|zeventien|achttien|negentien|twintig|eenentwintig|éénentwintig|tweeentwintig|tweeëntwintig|drieëntwintig|vierentwintig|vijfentwintig|zesentwintig|zevenentwintig|achtentwintig|negenentwintig|dertig|eenendertig|tweeëndertig|drieëndertig|vierendertig|vijfendertig|zesendertig|zevenendertig|achtendertig|negenendertig|veertig|eenenveertig|tweeënveertig|drieënveertig|vierenveertig|vijfenveertig|zesenveertig|zevenenveertig|achtenveertig|negenenveertig|eenenvijftig|vijftig|tweeënvijftig|drieënvijftig|vierenvijftig|vijfenvijftig|zesenvijftig|zevenenvijftig|achtenvijftig|negenenvijftig)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonth = $@"^\s*of\s*{MonthRegex}";
public static readonly string MonthEnd = $@"{MonthRegex}\s*(the)?\s*$";
public static readonly string WeekDayEnd = $@"(this\s+)?{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public const string RangeUnitRegex = @"\b(?<unit>years?|months?|weeks?)\b";
public const string HourNumRegex = @"\b(?<hournum>zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)\b";
public const string MinuteNumRegex = @"(?<minnum>ten|eleven|twelve|thirteen|fifteen|eighteen|(four|six|seven|nine)(teen)?|twenty|thirty|forty|fifty|one|two|three|five|eight)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonth = $@"^\s*de\s*{MonthRegex}";
public static readonly string MonthEnd = $@"{MonthRegex}\s*(le)?\s*$";
public static readonly string WeekDayEnd = $@"{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public const string RangeUnitRegex = @"\b(?<unit>(l')?ann[eé]e(s)?|mois|semaines?)\b";
public const string HourNumRegex = @"\b(?<hournum>zero|[aá]\s+une?|deux|trois|quatre|cinq|six|sept|huit|neuf|onze|douze|treize|quatorze|quinze|dix-six|dix-sept|dix-huit|dix-neuf|vingt|vingt-et-un|vingt-deux|vingt-trois|dix)\b";
public const string MinuteNumRegex = @"(?<minnum>un|deux|trois|quatre|cinq|six|sept|huit|neuf|onze|douze|treize|quatorze|quinze|seize|dix-sept|dix-huit|dix-neuf|vingt|trente|quarante|cinquante|dix)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonth = $@"^(\s*des\s*|\s*)?{MonthRegex}";
public static readonly string MonthEnd = $@"{MonthRegex}\s*(de[rmn])?\s*$";
public static readonly string WeekDayEnd = $@"{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public const string RangeUnitRegex = @"(?<unit>jahren?|jahr|monaten?|monat|wochen?|tagen?|tag)";
public const string OclockRegex = @"(?<oclock>uhr)";
public const string HourNumRegex = @"\b(?<hournum>einundzwanzig|zweiundzwanzig|dreiundzwanzig|vierundzwanzig|zwölf|zwoelf|dreizehn|vierzehn|fünfzehn|sechzehn|siebzehn|achtzehn|neunzehn|zwanzig|'null'|eins?|zwei|zwo|drei|vier|fünf|fuenf|sechs|sieben|acht|neun|zehn|elf)\b";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonth = $@"(^\s*(di\s+)?){MonthRegex}";
public static readonly string MonthEnd = $@"{MonthRegex}\s*,?\s*(il)?\s*$";
public static readonly string WeekDayEnd = $@"(quest[oa]\s+)?{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public const string RangeUnitRegex = @"\b(?<unit>l'anno|ann[oi]?|mes[ei]|settiman[ae])\b";
public const string HourNumRegex = @"\b(?<hournum>zero|(?<=l')una|due|tre|quattro|cinque|sei|sette|otto|nove|dieci|undici|dodici|tredici|quattordici|quindici|sedici|diciassette|diciotto|diciannove|venti|ventuno|ventidue|ventitre|ventiquattro)\b";
public const string FivesRegex = @"(?<tens>(quindici|venti(\s*cinque)?|trenta(\s*cinque)?|quaranta(\s*cinque)?|cinquanta(\s*cinque)?|dieci|cinque))\b";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonthRegex = $@"^\s*de\s*{MonthSuffixRegex}";
public static readonly string MonthEndRegex = $@"({MonthRegex}\s*(o)?\s*$)";
public static readonly string WeekDayEnd = $@"{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public static readonly string DateYearRegex = $@"(?<year>{YearRegex}|{TwoDigitYearRegex})";
public static readonly string DateExtractor1 = $@"\b({WeekDayRegex}(\s+|\s*,\s*))?{DayRegex}?((\s*(de)|[/\\\.\-])\s*)?{MonthRegex}\b";
public static readonly string DateExtractor2 = $@"\b({WeekDayRegex}(\s+|\s*,\s*))?{DayRegex}\s*([\.\-]|de)?\s*{MonthRegex}?(\s*(,|de)\s*){YearRegex}\b";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonthRegex = $@"^\s*de\s*{MonthSuffixRegex}";
public static readonly string MonthEndRegex = $@"({MonthRegex}\s*(el)?\s*$)";
public static readonly string WeekDayEnd = $@"{WeekDayRegex}\s*,?\s*$";
public const string WeekDayStart = @"^[\.]";
public static readonly string DateYearRegex = $@"(?<year>{YearRegex}|{TwoDigitYearRegex})";
public static readonly string DateExtractor1 = $@"\b({WeekDayRegex}(\s+|\s*,\s*))?{DayRegex}?((\s*(de)|[/\\\.\-])\s*)?{MonthRegex}\b";
public static readonly string DateExtractor2 = $@"\b({WeekDayRegex}(\s+|\s*,\s*))?{DayRegex}\s*([\.\-]|de)\s*{MonthRegex}(\s*,\s*|\s*(del?)\s*){DateYearRegex}\b";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public static class DateTimeDefinitions
public static readonly string OfMonth = $@"^\s*{MonthRegex}";
public static readonly string MonthEnd = $@"{MonthPossessiveSuffixRegex}\s*$";
public static readonly string WeekDayEnd = $@"(bu\s+)?{WeekDayRegex}\s*,?\s*$";
public static readonly string WeekDayStart = $@"\s*,?\s*{WeekDayRegex}";
public const string RangeUnitRegex = @"\b(?<unit>hafta|ay|yıl)\b";
public const string HourNumRegex = @"\b(?<hournum>bir|iki|üç|dört|beş|altı|yedi|sekiz|dokuz|on|on bir|on iki)\b";
public const string AtHourNumRegex = @"\b(?<hournum>birde|ikide|üçte|dörtte|beşte|altıda|yedide|sekizde|dokuzda|onda|on birde|on ikide|1'de|2'de|3'te|4'te|5'te|6'da|7'de|8'de|9'da|10'da|11'de|12'de|13'te|14'te|15'te|16'da|17'de|18'de|19'da|20'de|21'de|22'de|23'te|24'te)\b";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public class DutchDateExtractorConfiguration : BaseDateTimeOptionsConfiguration,
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -238,6 +241,8 @@ public DutchDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.ForTheRegex => ForTheRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public class EnglishDateExtractorConfiguration : BaseDateTimeOptionsConfiguratio
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -239,6 +242,8 @@ public EnglishDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.ForTheRegex => ForTheRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,7 @@ private void ExtendWithWeekdayAndYear(ref int startIndex, ref int endIndex, int
// Check for weekday in the suffix
if (!matchWeekDay.Success)
{
suffix = text.Substring(endIndex);
var weekDayStart = @"^\s*,?\s*" + this.Config.WeekDayRegex;
Regex weekDayStartRegex = new Regex(weekDayStart, RegexOptions.Compiled | RegexOptions.IgnoreCase);
matchWeekDay = weekDayStartRegex.Match(suffix);
matchWeekDay = this.Config.WeekDayStart.Match(suffix);
}

if (matchWeekDay.Success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface IDateExtractorConfiguration : IDateTimeOptionsConfiguration

Regex WeekDayEnd { get; }

Regex WeekDayStart { get; }

Regex DateUnitRegex { get; }

Regex ForTheRegex { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public class FrenchDateExtractorConfiguration : BaseDateTimeOptionsConfiguration
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -207,6 +210,8 @@ public FrenchDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.WeekDayAndDayOfMonthRegex => WeekDayAndDayOfMothRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class GermanDateExtractorConfiguration : BaseDateTimeOptionsConfiguration
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -204,6 +207,8 @@ public GermanDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.ForTheRegex => ForTheRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public class ItalianDateExtractorConfiguration : BaseDateTimeOptionsConfiguratio
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -205,6 +208,8 @@ public ItalianDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.WeekDayAndDayOfMonthRegex => WeekDayAndDayOfMothRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public class PortugueseDateExtractorConfiguration : BaseDateTimeOptionsConfigura
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -198,6 +201,8 @@ public PortugueseDateExtractorConfiguration(IDateTimeOptionsConfiguration config

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.ForTheRegex => ForTheRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public class SpanishDateExtractorConfiguration : BaseDateTimeOptionsConfiguratio
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -195,6 +198,8 @@ public SpanishDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.ForTheRegex => ForTheRegex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public class TurkishDateExtractorConfiguration : BaseDateTimeOptionsConfiguratio
public static readonly Regex WeekDayEnd =
new Regex(DateTimeDefinitions.WeekDayEnd, RegexFlags);

public static readonly Regex WeekDayStart =
new Regex(DateTimeDefinitions.WeekDayStart, RegexFlags);

public static readonly Regex YearSuffix =
new Regex(DateTimeDefinitions.YearSuffix, RegexFlags);

Expand Down Expand Up @@ -245,6 +248,8 @@ public TurkishDateExtractorConfiguration(IDateTimeOptionsConfiguration config)

Regex IDateExtractorConfiguration.WeekDayEnd => WeekDayEnd;

Regex IDateExtractorConfiguration.WeekDayStart => WeekDayStart;

Regex IDateExtractorConfiguration.DateUnitRegex => DateUnitRegex;

Regex IDateExtractorConfiguration.ForTheRegex => ForTheRegex;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Patterns/Dutch/Dutch-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ MonthEnd: !nestedRegex
WeekDayEnd: !nestedRegex
def: '(deze\s+)?{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
RangeUnitRegex: !simpleRegex
def: \b(?<unit>jaren|jaar|maanden|maand|weken|week)\b
HourNumRegex: !simpleRegex
Expand Down
2 changes: 2 additions & 0 deletions Patterns/English/English-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ MonthEnd: !nestedRegex
WeekDayEnd: !nestedRegex
def: '(this\s+)?{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
RangeUnitRegex: !simpleRegex
def: \b(?<unit>years?|months?|weeks?)\b
HourNumRegex: !simpleRegex
Expand Down
2 changes: 2 additions & 0 deletions Patterns/French/French-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ MonthEnd: !nestedRegex
WeekDayEnd: !nestedRegex
def: '{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
RangeUnitRegex: !simpleRegex
def: \b(?<unit>(l')?ann[eé]e(s)?|mois|semaines?)\b
HourNumRegex: !simpleRegex
Expand Down
2 changes: 2 additions & 0 deletions Patterns/German/German-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ MonthEnd: !nestedRegex
WeekDayEnd: !nestedRegex
def: '{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
RangeUnitRegex: !simpleRegex
def: (?<unit>jahren?|jahr|monaten?|monat|wochen?|tagen?|tag)
OclockRegex: !simpleRegex
Expand Down
2 changes: 2 additions & 0 deletions Patterns/Italian/Italian-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ MonthEnd: !nestedRegex
WeekDayEnd: !nestedRegex
def: '(quest[oa]\s+)?{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
RangeUnitRegex: !simpleRegex
def: \b(?<unit>l'anno|ann[oi]?|mes[ei]|settiman[ae])\b
HourNumRegex: !simpleRegex
Expand Down
2 changes: 2 additions & 0 deletions Patterns/Portuguese/Portuguese-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ MonthEndRegex: !nestedRegex
WeekDayEnd: !nestedRegex
def: '{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
DateYearRegex: !nestedRegex
def: (?<year>{YearRegex}|{TwoDigitYearRegex})
references: [ YearRegex, TwoDigitYearRegex ]
Expand Down
2 changes: 2 additions & 0 deletions Patterns/Spanish/Spanish-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ MonthEndRegex: !nestedRegex
WeekDayEnd: !nestedRegex
def: '{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !simpleRegex
def: ^[\.]
DateYearRegex: !nestedRegex
def: (?<year>{YearRegex}|{TwoDigitYearRegex})
references: [ YearRegex, TwoDigitYearRegex ]
Expand Down
3 changes: 3 additions & 0 deletions Patterns/Turkish/Turkish-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ MonthEnd: !nestedRegex
WeekDayEnd: !nestedRegex
def: '(bu\s+)?{WeekDayRegex}\s*,?\s*$'
references: [ WeekDayRegex ]
WeekDayStart: !nestedRegex
def: '\s*,?\s*{WeekDayRegex}'
references: [ WeekDayRegex ]
RangeUnitRegex: !simpleRegex
def: \b(?<unit>hafta|ay|yıl)\b
HourNumRegex: !simpleRegex
Expand Down

0 comments on commit a640186

Please sign in to comment.