Skip to content

Commit

Permalink
Fix regression bug in ordinal.relative (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
songwenhao1 authored and tellarin committed Jun 19, 2019
1 parent 1b81155 commit d789236
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public static class NumbersDefinitions
public static readonly string AllIntRegexWithDozenSuffixLocks = $@"(?<=\b)(((half\s+)?a\s+dozen)|({AllIntRegex}\s+dozen(s)?))(?=\b)";
public const string RoundNumberOrdinalRegex = @"(hundredth|thousandth|millionth|billionth|trillionth)";
public const string NumberOrdinalRegex = @"(first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth|thirteenth|fourteenth|fifteenth|sixteenth|seventeenth|eighteenth|nineteenth|twentieth|thirtieth|fortieth|fiftieth|sixtieth|seventieth|eightieth|ninetieth)";
public const string RelativeOrdinalRegex = @"(?<relativeOrdinal>(next|previous)\s+one|(the\s+second|next)\s+to\s+last|the\s+one\s+before\s+the\s+last(\s+one)?|the\s+last\s+but\s+one|(ante)?penultimate|last|next|previous)";
public const string RelativeOrdinalRegex = @"(?<relativeOrdinal>(next|previous|current)\s+one|(the\s+second|next)\s+to\s+last|the\s+one\s+before\s+the\s+last(\s+one)?|the\s+last\s+but\s+one|(ante)?penultimate|last|next|previous|current)";
public static readonly string BasicOrdinalRegex = $@"({NumberOrdinalRegex}|{RelativeOrdinalRegex})";
public static readonly string SuffixBasicOrdinalRegex = $@"((((({TensNumberIntegerRegex}(\s+(and\s+)?|\s*-\s*){ZeroToNineIntegerRegex})|{TensNumberIntegerRegex}|{ZeroToNineIntegerRegex}|{AnIntRegex})(\s+{RoundNumberIntegerRegex})+)\s+(and\s+)?)*({TensNumberIntegerRegex}(\s+|\s*-\s*))?{BasicOrdinalRegex})";
public static readonly string SuffixRoundNumberOrdinalRegex = $@"(({AllIntRegex}\s+){RoundNumberOrdinalRegex})";
public static readonly string AllOrdinalRegex = $@"({SuffixBasicOrdinalRegex}|{SuffixRoundNumberOrdinalRegex})";
public const string OrdinalSuffixRegex = @"(?<=\b)((\d*(1st|2nd|3rd|4th|5th|6th|7th|8th|9th|0th))|(11th|12th))(?=\b)";
public const string OrdinalNumericRegex = @"(?<=\b)(\d{1,3}(\s*,\s*\d{3})*\s*th)(?=\b)";
public static readonly string OrdinalRoundNumberRegex = $@"(?<!(a|an)\s+){RoundNumberOrdinalRegex}";
public static readonly string OrdinalRoundNumberRegex = $@"(?<!an?\s+){RoundNumberOrdinalRegex}";
public static readonly string OrdinalEnglishRegex = $@"(?<=\b){AllOrdinalRegex}(?=\b)";
public const string FractionNotationWithSpacesRegex = @"(((?<=\W|^)-\s*)|(?<=\b))\d+\s+\d+[/]\d+(?=(\b[^/]|$))";
public const string FractionNotationRegex = @"(((?<=\W|^)-\s*)|(?<![/-])(?<=\b))\d+[/]\d+(?=(\b[^/]|$))";
public static readonly string FractionNounRegex = $@"(?<=\b)({AllIntRegex}\s+(and\s+)?)?({AllIntRegex})(\s+|\s*-\s*)((({AllOrdinalRegex})|({RoundNumberOrdinalRegex}))s|halves|quarters)(?=\b)";
public static readonly string FractionNounWithArticleRegex = $@"(?<=\b)((({AllIntRegex}\s+(and\s+)?)?(a|an|one)(\s+|\s*-\s*)(?!\bfirst\b|\bsecond\b)(({AllOrdinalRegex})|({RoundNumberOrdinalRegex})|half|quarter))|(half))(?=\b)";
public static readonly string FractionNounWithArticleRegex = $@"(?<=\b)((({AllIntRegex}\s+(and\s+)?)?(an?|one)(\s+|\s*-\s*)(?!\bfirst\b|\bsecond\b)(({AllOrdinalRegex})|({RoundNumberOrdinalRegex})|half|quarter))|(half))(?=\b)";
public static readonly string FractionPrepositionRegex = $@"(?<=\b)(?<numerator>({AllIntRegex})|((?<![\.,])\d+))\s+(over|in|out\s+of)\s+(?<denominator>({AllIntRegex})|(\d+)(?![\.,]))(?=\b)";
public static readonly string FractionPrepositionWithinPercentModeRegex = $@"(?<=\b)(?<numerator>({AllIntRegex})|((?<![\.,])\d+))\s+over\s+(?<denominator>({AllIntRegex})|(\d+)(?![\.,]))(?=\b)";
public static readonly string AllPointRegex = $@"((\s+{ZeroToNineIntegerRegex})+|(\s+{SeparaIntRegex}))";
Expand Down Expand Up @@ -254,9 +254,12 @@ public static class NumbersDefinitions
{
{ @"last", @"0" },
{ @"next one", @"1" },
{ @"current", @"0" },
{ @"current one", @"0" },
{ @"previous one", @"-1" },
{ @"the second to last", @"-1" },
{ @"the one before the last one", @"-1" },
{ @"the one before the last", @"-1" },
{ @"next to last", @"-1" },
{ @"penultimate", @"-1" },
{ @"the last but one", @"-1" },
Expand All @@ -269,8 +272,11 @@ public static class NumbersDefinitions
{ @"last", @"end" },
{ @"next one", @"current" },
{ @"previous one", @"current" },
{ @"current", @"current" },
{ @"current one", @"current" },
{ @"the second to last", @"end" },
{ @"the one before the last one", @"end" },
{ @"the one before the last", @"end" },
{ @"next to last", @"end" },
{ @"penultimate", @"end" },
{ @"the last but one", @"end" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public virtual List<ExtractResult> Extract(string source)
if (ExtractType.Contains(Constants.MODEL_ORDINAL))
{
er.Metadata = new Metadata();
if (originalMatch.Groups[Constants.RelativeOrdinalGroupName].Success)
if ((Options & NumberOptions.SuppressExtendedTypes) == 0 && originalMatch.Groups[Constants.RelativeOrdinalGroupName].Success)
{
er.Metadata.IsOrdinalRelative = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public virtual ParseResult Parse(ExtractResult extResult)
// Add "offset" and "relativeTo" for ordinal
if (!string.IsNullOrEmpty(ret.Type) && ret.Type.Contains(Constants.MODEL_ORDINAL))
{
if (Config.RelativeReferenceOffsetMap.ContainsKey(extResult.Text) &&
if ((this.Config.Options & NumberOptions.SuppressExtendedTypes) == 0 && Config.RelativeReferenceOffsetMap.ContainsKey(extResult.Text) &&
Config.RelativeReferenceRelativeToMap.ContainsKey(extResult.Text))
{
ret.Metadata.Offset = Config.RelativeReferenceOffsetMap[extResult.Text];
Expand Down Expand Up @@ -302,10 +302,13 @@ protected ParseResult TextNumberParse(ExtractResult extResult)
handle = Config.HalfADozenRegex.Replace(handle, Config.HalfADozenText);

// Handling cases like "last", "next one", "previous one"
if (Config.RelativeReferenceOffsetMap.ContainsKey(extResult.Text) &&
Config.RelativeReferenceRelativeToMap.ContainsKey(extResult.Text))
if ((this.Config.Options & NumberOptions.SuppressExtendedTypes) == 0)
{
return result;
if (Config.RelativeReferenceOffsetMap.ContainsKey(extResult.Text) &&
Config.RelativeReferenceRelativeToMap.ContainsKey(extResult.Text))
{
return result;
}
}

var numGroup = handle.Split(Config.WrittenDecimalSeparatorTexts.ToArray(), StringSplitOptions.RemoveEmptyEntries);
Expand Down
12 changes: 9 additions & 3 deletions Patterns/English/English-Numbers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ RoundNumberOrdinalRegex: !simpleRegex
NumberOrdinalRegex: !simpleRegex
def: (first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth|thirteenth|fourteenth|fifteenth|sixteenth|seventeenth|eighteenth|nineteenth|twentieth|thirtieth|fortieth|fiftieth|sixtieth|seventieth|eightieth|ninetieth)
RelativeOrdinalRegex: !simpleRegex
def: (?<relativeOrdinal>(next|previous)\s+one|(the\s+second|next)\s+to\s+last|the\s+one\s+before\s+the\s+last(\s+one)?|the\s+last\s+but\s+one|(ante)?penultimate|last|next|previous)
def: (?<relativeOrdinal>(next|previous|current)\s+one|(the\s+second|next)\s+to\s+last|the\s+one\s+before\s+the\s+last(\s+one)?|the\s+last\s+but\s+one|(ante)?penultimate|last|next|previous|current)
BasicOrdinalRegex: !nestedRegex
def: ({NumberOrdinalRegex}|{RelativeOrdinalRegex})
references: [ NumberOrdinalRegex, RelativeOrdinalRegex ]
Expand All @@ -73,7 +73,7 @@ OrdinalSuffixRegex: !simpleRegex
OrdinalNumericRegex: !simpleRegex
def: (?<=\b)(\d{1,3}(\s*,\s*\d{3})*\s*th)(?=\b)
OrdinalRoundNumberRegex: !nestedRegex
def: (?<!(a|an)\s+){RoundNumberOrdinalRegex}
def: (?<!an?\s+){RoundNumberOrdinalRegex}
references: [ RoundNumberOrdinalRegex ]
OrdinalEnglishRegex: !nestedRegex
def: (?<=\b){AllOrdinalRegex}(?=\b)
Expand All @@ -87,7 +87,7 @@ FractionNounRegex: !nestedRegex
def: (?<=\b)({AllIntRegex}\s+(and\s+)?)?({AllIntRegex})(\s+|\s*-\s*)((({AllOrdinalRegex})|({RoundNumberOrdinalRegex}))s|halves|quarters)(?=\b)
references: [ AllIntRegex, AllOrdinalRegex, RoundNumberOrdinalRegex ]
FractionNounWithArticleRegex: !nestedRegex
def: (?<=\b)((({AllIntRegex}\s+(and\s+)?)?(a|an|one)(\s+|\s*-\s*)(?!\bfirst\b|\bsecond\b)(({AllOrdinalRegex})|({RoundNumberOrdinalRegex})|half|quarter))|(half))(?=\b)
def: (?<=\b)((({AllIntRegex}\s+(and\s+)?)?(an?|one)(\s+|\s*-\s*)(?!\bfirst\b|\bsecond\b)(({AllOrdinalRegex})|({RoundNumberOrdinalRegex})|half|quarter))|(half))(?=\b)
references: [ AllIntRegex, AllOrdinalRegex, RoundNumberOrdinalRegex ]
FractionPrepositionRegex: !nestedRegex
def: (?<=\b)(?<numerator>({AllIntRegex})|((?<![\.,])\d+))\s+(over|in|out\s+of)\s+(?<denominator>({AllIntRegex})|(\d+)(?![\.,]))(?=\b)
Expand Down Expand Up @@ -365,9 +365,12 @@ RelativeReferenceOffsetMap: !dictionary
entries:
last: 0
next one: 1
current: 0
current one: 0
previous one: -1
the second to last: -1
the one before the last one: -1
the one before the last: -1
next to last: -1
penultimate: -1
the last but one: -1
Expand All @@ -380,8 +383,11 @@ RelativeReferenceRelativeToMap: !dictionary
last: end
next one: current
previous one: current
current: current
current one: current
the second to last: end
the one before the last one: end
the one before the last: end
next to last: end
penultimate: end
the last but one: end
Expand Down
68 changes: 68 additions & 0 deletions Specs/Number/English/OrdinalModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -678,5 +678,73 @@
}
}
]
},
{
"Input": "The one before the last one is the right one",
"NotSupported": "javascript, python, java",
"Results": [
{
"Text": "the one before the last one",
"Start": 0,
"End": 26,
"TypeName": "ordinal.relative",
"Resolution": {
"offset": "-1",
"relativeTo": "end",
"value": "end-1"
}
}
]
},
{
"Input": "I meant the one before the last",
"NotSupported": "javascript, python, java",
"Results": [
{
"Text": "the one before the last",
"Start": 8,
"End": 30,
"TypeName": "ordinal.relative",
"Resolution": {
"offset": "-1",
"relativeTo": "end",
"value": "end-1"
}
}
]
},
{
"Input": "I meant the current one",
"NotSupported": "javascript, python, java",
"Results": [
{
"Text": "current one",
"Start": 12,
"End": 22,
"TypeName": "ordinal.relative",
"Resolution": {
"offset": "0",
"relativeTo": "current",
"value": "current+0"
}
}
]
},
{
"Input": "Look at the current page",
"NotSupported": "javascript, python, java",
"Results": [
{
"Text": "current",
"Start": 12,
"End": 18,
"TypeName": "ordinal.relative",
"Resolution": {
"offset": "0",
"relativeTo": "current",
"value": "current+0"
}
}
]
}
]
20 changes: 20 additions & 0 deletions Specs/Number/English/OrdinalModelSuppressExtendedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,25 @@
"NotSupported": "javascript, python, java",
"Results": [
]
},
{
"Input": "The one before the last one is the right one",
"NotSupported": "javascript, python, java",
"Results": []
},
{
"Input": "I meant the one before the last",
"NotSupported": "javascript, python, java",
"Results": []
},
{
"Input": "I meant the current one",
"NotSupported": "javascript, python, java",
"Results": []
},
{
"Input": "Look at the current page",
"NotSupported": "javascript, python, java",
"Results": []
}
]

0 comments on commit d789236

Please sign in to comment.