Skip to content

Commit

Permalink
[.NET] Refinements to #1870 to reduce relative mealtime side-effects (#…
Browse files Browse the repository at this point in the history
…1874)

* Small refinement in mealtime changes to reduce side effects

* Re-generating resources in other platforms.
  • Loading branch information
tellarin authored and wgx998877 committed Sep 16, 2019
1 parent 8c06639 commit 4323533
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,11 @@ public static class DateTimeDefinitions
public static readonly string[] DurationDateRestrictions = { @"today", @"now" };
public static readonly Dictionary<string, string> AmbiguityFiltersDict = new Dictionary<string, string>
{
{ @"\bmorning|afternoon|evening|night|day\b", @"\b(good\s+(morning|afternoon|evening|night|day))|(nighty\s+night)\b" },
{ @"^(morning|afternoon|evening|night|day)\b", @"\b(good\s+(morning|afternoon|evening|night|day))|(nighty\s+night)\b" },
{ @"\bnow\b", @"\b(^now,)|\b((is|are)\s+now\s+for|for\s+now)\b" },
{ @"\bmay\b", @"\b((((!|\.|\?|,|;|)\s+|^)may i)|(i|you|he|she|we|they)\s+may|(may\s+((((also|not|(also not)|well)\s+)?(be|ask|contain|constitute|e-?mail|take|have|result|involve|get|work|reply|differ))|(or may not))))\b" },
{ @"\b(a|one) second\b", @"\b(?<!an?\s+)(a|one) second (round|time)\b" }
{ @"\b(a|one) second\b", @"\b(?<!an?\s+)(a|one) second (round|time)\b" },
{ @"\b(breakfast|brunch|lunch(time)?|dinner(time)?|supper)$", @"(?<!\b(before|after|around|circa)\b\s)(breakfast|brunch|lunch(time)?|dinner(time)?|supper)" }
};
public static readonly IList<string> MorningTermList = new List<string>
{
Expand Down
1 change: 1 addition & 0 deletions .NET/Microsoft.Recognizers.Text.DateTime/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public static class Constants
public const string BusinessDayGroupName = "business";
public const string LeftAmPmGroupName = "leftDesc";
public const string RightAmPmGroupName = "rightDesc";
public const string MealTimeGroupName = "mealTime";

public const string DECADE_UNIT = "10Y";
public const string FORTNIGHT_UNIT = "2W";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,15 @@ private List<Token> MergeTwoTimePoints(string text, DateObject reference, List<E
// Regarding the phrase as-- {Date} {TimePeriod}, like "2015-9-23 1pm to 4"
// Or {TimePeriod} on {Date}, like "1:30 to 4 on 2015-9-23"
var timePeriodErs = config.TimePeriodExtractor.Extract(text, reference);
dateErs.AddRange(timePeriodErs);

// Mealtime periods (like "dinnertime") are not currently fully supported in merging.
foreach (var timePeriod in timePeriodErs)
{
if (timePeriod.Metadata == null || !timePeriod.Metadata.IsMealtime)
{
dateErs.Add(timePeriod);
}
}

var points = dateErs.OrderBy(x => x.Start).ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ public List<ExtractResult> Extract(string text, DateObject reference)

ret = FilterUnspecificDatePeriod(ret);

ret = AddMod(ret, text);

// Remove common ambiguous cases
ret = FilterAmbiguity(ret, text);

ret = FilterUnspecificTimePeriod(ret);
ret = AddMod(ret, text);

// Filtering
if ((this.config.Options & DateTimeOptions.CalendarMode) != 0)
Expand Down Expand Up @@ -245,16 +243,6 @@ private List<ExtractResult> FilterUnspecificDatePeriod(List<ExtractResult> ers)
return ers;
}

private List<ExtractResult> FilterUnspecificTimePeriod(List<ExtractResult> ers)
{
if (this.config.UnspecificTimePeriodRegex != null)
{
ers.RemoveAll(o => this.config.UnspecificTimePeriodRegex.IsExactMatch(o.Text, true));
}

return ers;
}

private List<ExtractResult> FilterAmbiguity(List<ExtractResult> extractResults, string text)
{
if (this.config.AmbiguityFiltersDict != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,19 @@ private List<Token> MatchTimeOfDay(string text)
var matches = this.config.TimeOfDayRegex.Matches(text);
foreach (Match match in matches)
{
ret.Add(new Token(match.Index, match.Index + match.Length));
Metadata metadata = null;

if (match.Groups[Constants.MealTimeGroupName].Success)
{
metadata = new Metadata
{
IsMealtime = true,
};
}

var token = new Token(match.Index, match.Index + match.Length, metadata);

ret.Add(token);
}

return ret;
Expand Down
2 changes: 2 additions & 0 deletions .NET/Microsoft.Recognizers.Text/Extractors/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public class Metadata
public string Offset { get; set; } = string.Empty;

public string RelativeTo { get; set; } = string.Empty;

public bool IsMealtime { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1316,10 +1316,11 @@ public class EnglishDateTime {
public static final List<String> DurationDateRestrictions = Arrays.asList("today", "now");

public static final ImmutableMap<String, String> AmbiguityFiltersDict = ImmutableMap.<String, String>builder()
.put("\\bmorning|afternoon|evening|night|day\\b", "\\b(good\\s+(morning|afternoon|evening|night|day))|(nighty\\s+night)\\b")
.put("^(morning|afternoon|evening|night|day)\\b", "\\b(good\\s+(morning|afternoon|evening|night|day))|(nighty\\s+night)\\b")
.put("\\bnow\\b", "\\b(^now,)|\\b((is|are)\\s+now\\s+for|for\\s+now)\\b")
.put("\\bmay\\b", "\\b((((!|\\.|\\?|,|;|)\\s+|^)may i)|(i|you|he|she|we|they)\\s+may|(may\\s+((((also|not|(also not)|well)\\s+)?(be|ask|contain|constitute|e-?mail|take|have|result|involve|get|work|reply|differ))|(or may not))))\\b")
.put("\\b(a|one) second\\b", "\\b(?<!an?\\s+)(a|one) second (round|time)\\b")
.put("\\b(breakfast|brunch|lunch(time)?|dinner(time)?|supper)$", "(?<!\\b(before|after|around|circa)\\b\\s)(breakfast|brunch|lunch(time)?|dinner(time)?|supper)")
.build();

public static final List<String> MorningTermList = Arrays.asList("morning");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export namespace EnglishDateTime {
export const DefaultLanguageFallback = 'MDY';
export const SuperfluousWordList = [ "preferably","how about","maybe","say","like" ];
export const DurationDateRestrictions = [ "today","now" ];
export const AmbiguityFiltersDict: ReadonlyMap<string, string> = new Map<string, string>([["\\bmorning|afternoon|evening|night|day\\b", "\\b(good\\s+(morning|afternoon|evening|night|day))|(nighty\\s+night)\\b"],["\\bnow\\b", "\\b(^now,)|\\b((is|are)\\s+now\\s+for|for\\s+now)\\b"],["\\bmay\\b", "\\b((((!|\\.|\\?|,|;|)\\s+|^)may i)|(i|you|he|she|we|they)\\s+may|(may\\s+((((also|not|(also not)|well)\\s+)?(be|ask|contain|constitute|e-?mail|take|have|result|involve|get|work|reply|differ))|(or may not))))\\b"],["\\b(a|one) second\\b", "\\b(?<!an?\\s+)(a|one) second (round|time)\\b"]]);
export const AmbiguityFiltersDict: ReadonlyMap<string, string> = new Map<string, string>([["^(morning|afternoon|evening|night|day)\\b", "\\b(good\\s+(morning|afternoon|evening|night|day))|(nighty\\s+night)\\b"],["\\bnow\\b", "\\b(^now,)|\\b((is|are)\\s+now\\s+for|for\\s+now)\\b"],["\\bmay\\b", "\\b((((!|\\.|\\?|,|;|)\\s+|^)may i)|(i|you|he|she|we|they)\\s+may|(may\\s+((((also|not|(also not)|well)\\s+)?(be|ask|contain|constitute|e-?mail|take|have|result|involve|get|work|reply|differ))|(or may not))))\\b"],["\\b(a|one) second\\b", "\\b(?<!an?\\s+)(a|one) second (round|time)\\b"],["\\b(breakfast|brunch|lunch(time)?|dinner(time)?|supper)$", "(?<!\\b(before|after|around|circa)\\b\\s)(breakfast|brunch|lunch(time)?|dinner(time)?|supper)"]]);
export const MorningTermList = [ "morning" ];
export const AfternoonTermList = [ "afternoon" ];
export const EveningTermList = [ "evening" ];
Expand Down
3 changes: 2 additions & 1 deletion Patterns/English/English-DateTime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,11 @@ DurationDateRestrictions: [ today, now ]
AmbiguityFiltersDict: !dictionary
types: [ string, string ]
entries:
'\bmorning|afternoon|evening|night|day\b': '\b(good\s+(morning|afternoon|evening|night|day))|(nighty\s+night)\b'
'^(morning|afternoon|evening|night|day)\b': '\b(good\s+(morning|afternoon|evening|night|day))|(nighty\s+night)\b'
'\bnow\b': '\b(^now,)|\b((is|are)\s+now\s+for|for\s+now)\b'
'\bmay\b': '\b((((!|\.|\?|,|;|)\s+|^)may i)|(i|you|he|she|we|they)\s+may|(may\s+((((also|not|(also not)|well)\s+)?(be|ask|contain|constitute|e-?mail|take|have|result|involve|get|work|reply|differ))|(or may not))))\b'
'\b(a|one) second\b': '\b(?<!an?\s+)(a|one) second (round|time)\b'
'\b(breakfast|brunch|lunch(time)?|dinner(time)?|supper)$': '(?<!\b(before|after|around|circa)\b\s)(breakfast|brunch|lunch(time)?|dinner(time)?|supper)'
# For TimeOfDay resolution
MorningTermList: !list
types: [ string ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,11 @@ class EnglishDateTime:
DefaultLanguageFallback = 'MDY'
SuperfluousWordList = [r'preferably', r'how about', r'maybe', r'say', r'like']
DurationDateRestrictions = [r'today', r'now']
AmbiguityFiltersDict = dict([("\\bmorning|afternoon|evening|night|day\\b", "\\b(good\\s+(morning|afternoon|evening|night|day))|(nighty\\s+night)\\b"),
AmbiguityFiltersDict = dict([("^(morning|afternoon|evening|night|day)\\b", "\\b(good\\s+(morning|afternoon|evening|night|day))|(nighty\\s+night)\\b"),
("\\bnow\\b", "\\b(^now,)|\\b((is|are)\\s+now\\s+for|for\\s+now)\\b"),
("\\bmay\\b", "\\b((((!|\\.|\\?|,|;|)\\s+|^)may i)|(i|you|he|she|we|they)\\s+may|(may\\s+((((also|not|(also not)|well)\\s+)?(be|ask|contain|constitute|e-?mail|take|have|result|involve|get|work|reply|differ))|(or may not))))\\b"),
("\\b(a|one) second\\b", "\\b(?<!an?\\s+)(a|one) second (round|time)\\b")])
("\\b(a|one) second\\b", "\\b(?<!an?\\s+)(a|one) second (round|time)\\b"),
("\\b(breakfast|brunch|lunch(time)?|dinner(time)?|supper)$", "(?<!\\b(before|after|around|circa)\\b\\s)(breakfast|brunch|lunch(time)?|dinner(time)?|supper)")])
MorningTermList = [r'morning']
AfternoonTermList = [r'afternoon']
EveningTermList = [r'evening']
Expand Down
24 changes: 24 additions & 0 deletions Specs/DateTime/English/DateTimeModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -14531,5 +14531,29 @@
"ReferenceDateTime": "2019-09-12T00:00:00"
},
"Results": []
},
{
"Input": "We will have that staff dinner seven p . m .",
"NotSupported": "javascript, python, java",
"Context": {
"ReferenceDateTime": "2019-09-12T00:00:00"
},
"Results": [
{
"Text": "seven p . m .",
"Start": 31,
"End": 43,
"TypeName": "datetimeV2.time",
"Resolution": {
"values": [
{
"timex": "T19",
"type": "time",
"value": "19:00:00"
}
]
}
}
]
}
]
29 changes: 29 additions & 0 deletions Specs/DateTime/English/DateTimeModelComplexCalendar.json
Original file line number Diff line number Diff line change
Expand Up @@ -12511,5 +12511,34 @@
"ReferenceDateTime": "2019-09-12T00:00:00"
},
"Results": []
},
{
"Input": "We will have that staff dinner march twenty second at seven p . m .",
"NotSupported": "javascript, python, java",
"Context": {
"ReferenceDateTime": "2019-09-12T00:00:00"
},
"Results": [
{
"Text": "march twenty second at seven p . m .",
"Start": 31,
"End": 66,
"TypeName": "datetimeV2.datetime",
"Resolution": {
"values": [
{
"timex": "XXXX-03-22T19",
"type": "datetime",
"value": "2019-03-22 19:00:00"
},
{
"timex": "XXXX-03-22T19",
"type": "datetime",
"value": "2020-03-22 19:00:00"
}
]
}
}
]
}
]

0 comments on commit 4323533

Please sign in to comment.