|
7 | 7 | using System.Linq;
|
8 | 8 | using System.Reflection;
|
9 | 9 | using System.Text.RegularExpressions;
|
| 10 | +using Microsoft.Recognizers.Text.DateTime.English; |
10 | 11 | using Microsoft.Recognizers.Text.Utilities;
|
11 | 12 | using DateObject = System.DateTime;
|
12 | 13 |
|
@@ -170,6 +171,12 @@ protected DateTimeResolutionResult InternalParse(string entityText, DateObject r
|
170 | 171 | innerResult = ParseDateWithTimePeriodSuffix(entityText, referenceTime);
|
171 | 172 | }
|
172 | 173 |
|
| 174 | + if (!innerResult.Success) |
| 175 | + { |
| 176 | + // Parsing cases like [duration] starting [datetime] |
| 177 | + innerResult = ParseStartingWithDuration(entityText, referenceTime); |
| 178 | + } |
| 179 | + |
173 | 180 | if (!innerResult.Success)
|
174 | 181 | {
|
175 | 182 | innerResult = ParseDuration(entityText, referenceTime);
|
@@ -1378,6 +1385,51 @@ private DateTimeResolutionResult ParseDuration(string text, DateObject reference
|
1378 | 1385 | return ret;
|
1379 | 1386 | }
|
1380 | 1387 |
|
| 1388 | + private DateTimeResolutionResult ParseStartingWithDuration(string text, DateObject referenceTime) |
| 1389 | + { |
| 1390 | + var ret = new DateTimeResolutionResult(); |
| 1391 | + var datetimeERs = Config.DateTimeExtractor.Extract(text, referenceTime); |
| 1392 | + var enConfig = Config as EnglishDateTimePeriodParserConfiguration; |
| 1393 | + |
| 1394 | + if (enConfig != null && enConfig.StartingRegex.Match(text).Success && datetimeERs.Count == 1) |
| 1395 | + { |
| 1396 | + var beforeString = text.Substring(0, (int)datetimeERs[0].Start); |
| 1397 | + |
| 1398 | + if (!string.IsNullOrEmpty(beforeString) && enConfig.StartingRegex.MatchEnd(beforeString, true).Success) |
| 1399 | + { |
| 1400 | + var pr = Config.DateTimeParser.Parse(datetimeERs[0], referenceTime); |
| 1401 | + var durationERs = Config.DurationExtractor.Extract(beforeString, referenceTime); |
| 1402 | + |
| 1403 | + if (durationERs.Count == 1) |
| 1404 | + { |
| 1405 | + var duration = Config.DurationParser.Parse(durationERs[0]); |
| 1406 | + var durationInSeconds = (double)((DateTimeResolutionResult)duration.Value).PastValue; |
| 1407 | + |
| 1408 | + DateObject startDate; |
| 1409 | + DateObject endDate; |
| 1410 | + |
| 1411 | + startDate = (DateObject)((DateTimeResolutionResult)pr.Value).PastValue; |
| 1412 | + endDate = startDate.AddSeconds(durationInSeconds); |
| 1413 | + |
| 1414 | + if (startDate != DateObject.MinValue) |
| 1415 | + { |
| 1416 | + var startLuisStr = $"{DateTimeFormatUtil.LuisDate(startDate)}{DateTimeFormatUtil.ShortTime(startDate.Hour, startDate.Minute, startDate.Second)}"; |
| 1417 | + var endLuisStr = $"{DateTimeFormatUtil.LuisDate(endDate)}{DateTimeFormatUtil.ShortTime(endDate.Hour, endDate.Minute, endDate.Second)}"; |
| 1418 | + var durationTimex = ((DateTimeResolutionResult)duration.Value).Timex; |
| 1419 | + |
| 1420 | + ret.Timex = $"({startLuisStr},{endLuisStr},{durationTimex})"; |
| 1421 | + ret.FutureValue = new Tuple<DateObject, DateObject>(startDate, endDate); |
| 1422 | + ret.PastValue = new Tuple<DateObject, DateObject>(startDate, endDate); |
| 1423 | + ret.SubDateTimeEntities = new List<object> { pr, duration }; |
| 1424 | + ret.Success = true; |
| 1425 | + } |
| 1426 | + } |
| 1427 | + } |
| 1428 | + } |
| 1429 | + |
| 1430 | + return ret; |
| 1431 | + } |
| 1432 | + |
1381 | 1433 | // Parse "last minute", "next hour"
|
1382 | 1434 | private DateTimeResolutionResult ParseRelativeUnit(string text, DateObject referenceTime)
|
1383 | 1435 | {
|
|
0 commit comments