|
6 | 6 | using System.Globalization;
|
7 | 7 | using System.Linq;
|
8 | 8 | using System.Text.RegularExpressions;
|
| 9 | +using Microsoft.Recognizers.Text.DateTime.English; |
9 | 10 | using Microsoft.Recognizers.Text.DateTime.Utilities;
|
10 | 11 | using Microsoft.Recognizers.Text.Utilities;
|
11 | 12 | using DateObject = System.DateTime;
|
@@ -476,6 +477,12 @@ private DateTimeResolutionResult ParseBaseDatePeriod(string text, DateObject ref
|
476 | 477 | innerResult = ParseDatePointWithAgoAndLater(text, referenceDate);
|
477 | 478 | }
|
478 | 479 |
|
| 480 | + // Cases like "for x weeks/days from today/12 sep etc." |
| 481 | + if (!innerResult.Success) |
| 482 | + { |
| 483 | + innerResult = ParseDatePointWithForPrefix(text, referenceDate); |
| 484 | + } |
| 485 | + |
479 | 486 | // Parse duration should be at the end since it will extract "the last week" from "the last week of July"
|
480 | 487 | if (!innerResult.Success)
|
481 | 488 | {
|
@@ -620,6 +627,65 @@ private DateTimeResolutionResult ParseDatePointWithAgoAndLater(string text, Date
|
620 | 627 | return ret;
|
621 | 628 | }
|
622 | 629 |
|
| 630 | + // Only handle cases like "for x weeks/days from today/tomorrow/some day" |
| 631 | + private DateTimeResolutionResult ParseDatePointWithForPrefix(string text, DateObject referenceDate) |
| 632 | + { |
| 633 | + var ret = new DateTimeResolutionResult(); |
| 634 | + var er = this.config.DateExtractor.Extract(text, referenceDate).FirstOrDefault(); |
| 635 | + |
| 636 | + if (er != null) |
| 637 | + { |
| 638 | + var beforeString = text.Substring(0, (int)er.Start); |
| 639 | + var isAgo = this.config.AgoRegex.Match(er.Text).Success; |
| 640 | + var config = this.config as EnglishDatePeriodParserConfiguration; |
| 641 | + |
| 642 | + if (!string.IsNullOrEmpty(beforeString) && config != null) |
| 643 | + { |
| 644 | + var matchFor = config.ForPrefixRegex.Match(beforeString); |
| 645 | + |
| 646 | + if (matchFor.Success && matchFor.Groups[Constants.ForGroupName].Success) |
| 647 | + { |
| 648 | + var pr = this.config.DateParser.Parse(er, referenceDate); |
| 649 | + var durationExtractionResult = this.config.DurationExtractor.Extract(er.Text, referenceDate).FirstOrDefault(); |
| 650 | + |
| 651 | + if (durationExtractionResult != null) |
| 652 | + { |
| 653 | + var duration = this.config.DurationParser.Parse(durationExtractionResult); |
| 654 | + var durationInSeconds = (double)((DateTimeResolutionResult)duration.Value).PastValue; |
| 655 | + |
| 656 | + DateObject startDate; |
| 657 | + DateObject endDate; |
| 658 | + |
| 659 | + if (isAgo) |
| 660 | + { |
| 661 | + startDate = (DateObject)((DateTimeResolutionResult)pr.Value).PastValue; |
| 662 | + endDate = startDate.AddSeconds(durationInSeconds); |
| 663 | + } |
| 664 | + else |
| 665 | + { |
| 666 | + endDate = (DateObject)((DateTimeResolutionResult)pr.Value).FutureValue; |
| 667 | + startDate = endDate.AddSeconds(-durationInSeconds); |
| 668 | + } |
| 669 | + |
| 670 | + if (startDate != DateObject.MinValue) |
| 671 | + { |
| 672 | + var startLuisStr = DateTimeFormatUtil.LuisDate(startDate); |
| 673 | + var endLuisStr = DateTimeFormatUtil.LuisDate(endDate); |
| 674 | + var durationTimex = ((DateTimeResolutionResult)duration.Value).Timex; |
| 675 | + |
| 676 | + ret.Timex = $"({startLuisStr},{endLuisStr},{durationTimex})"; |
| 677 | + ret.FutureValue = new Tuple<DateObject, DateObject>(startDate, endDate); |
| 678 | + ret.PastValue = new Tuple<DateObject, DateObject>(startDate, endDate); |
| 679 | + ret.Success = true; |
| 680 | + } |
| 681 | + } |
| 682 | + } |
| 683 | + } |
| 684 | + } |
| 685 | + |
| 686 | + return ret; |
| 687 | + } |
| 688 | + |
623 | 689 | private DateTimeResolutionResult ParseSingleTimePoint(string text, DateObject referenceDate, DateContext dateContext = null)
|
624 | 690 | {
|
625 | 691 | var ret = new DateTimeResolutionResult();
|
|
0 commit comments