Skip to content

Commit

Permalink
Support cases like "today at 9a" (#682)
Browse files Browse the repository at this point in the history
- replace some named group's names to constants
  • Loading branch information
sanxing-chen authored and tellarin committed Jun 29, 2018
1 parent 42c2fff commit 953fd70
Show file tree
Hide file tree
Showing 51 changed files with 838 additions and 664 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static class DateTimeDefinitions
public const string MidafternoonRegex = @"(?<midafternoon>midafternoon|mid-afternoon|mid afternoon)";
public const string MiddayRegex = @"(?<midday>midday|mid-day|mid day|((12\s)?noon))";
public static readonly string MidTimeRegex = $@"(?<mid>({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*((?<iam>a)|(?<ipm>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)(?<unit>hours|hour|hrs|hr|h|minutes|minute|mins|min|seconds|second|secs|sec)\b";
public const string RestrictedTimeUnitRegex = @"(?<unit>hour|minute)\b";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public static class TimeFunctions

public static TimeResult HandleLess(DateTimeExtra<TimeType> 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)
Expand All @@ -117,10 +117,10 @@ public static TimeResult HandleLess(DateTimeExtra<TimeType> extra)

public static TimeResult HandleChinese(DateTimeExtra<TimeType> 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
Expand All @@ -135,9 +135,9 @@ public static TimeResult HandleDigit(DateTimeExtra<TimeType> 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;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion .NET/Microsoft.Recognizers.Text.DateTime/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Loading

0 comments on commit 953fd70

Please sign in to comment.