in libraries/bot-dialogs/src/main/java/com/microsoft/recognizers/text/datetime/parsers/BaseTimePeriodParser.java [142:267]
private DateTimeResolutionResult parsePureNumCases(String text, LocalDateTime referenceTime) {
DateTimeResolutionResult ret = new DateTimeResolutionResult();
int year = referenceTime.getYear();
int month = referenceTime.getMonthValue();
int day = referenceTime.getDayOfMonth();
String trimmedText = text.trim().toLowerCase();
ConditionalMatch match = RegexExtension.matchBegin(this.config.getPureNumberFromToRegex(), trimmedText, true);
if (!match.getSuccess()) {
match = RegexExtension.matchBegin(this.config.getPureNumberBetweenAndRegex(), trimmedText, true);
}
if (match.getSuccess()) {
// this "from .. to .." pattern is valid if followed by a Date OR Constants.PmGroupName
boolean isValid = false;
// get hours
MatchGroup hourGroup = match.getMatch().get().getGroup(Constants.HourGroupName);
String hourStr = hourGroup.captures[0].value;
int afterHourIndex = hourGroup.captures[0].index + hourGroup.captures[0].length;
// hard to integrate this part into the regex
if (afterHourIndex == trimmedText.length() || !trimmedText.substring(afterHourIndex).trim().startsWith(":")) {
int beginHour;
if (!this.config.getNumbers().containsKey(hourStr)) {
beginHour = Integer.parseInt(hourStr);
} else {
beginHour = this.config.getNumbers().get(hourStr);
}
hourStr = hourGroup.captures[1].value;
afterHourIndex = hourGroup.captures[1].index + hourGroup.captures[1].length;
if (afterHourIndex == trimmedText.length() || !trimmedText.substring(afterHourIndex).trim().startsWith(":")) {
int endHour;
if (!this.config.getNumbers().containsKey(hourStr)) {
endHour = Integer.parseInt(hourStr);
} else {
endHour = this.config.getNumbers().get(hourStr);
}
// parse Constants.PmGroupName
String leftDesc = match.getMatch().get().getGroup("leftDesc").value;
String rightDesc = match.getMatch().get().getGroup("rightDesc").value;
String pmStr = match.getMatch().get().getGroup(Constants.PmGroupName).value;
String amStr = match.getMatch().get().getGroup(Constants.AmGroupName).value;
String descStr = match.getMatch().get().getGroup(Constants.DescGroupName).value;
// The "ampm" only occurs in time, we don't have to consider it here
if (StringUtility.isNullOrEmpty(leftDesc)) {
boolean rightAmValid = !StringUtility.isNullOrEmpty(rightDesc) &&
Arrays.stream(RegExpUtility.getMatches(config.getUtilityConfiguration().getAmDescRegex(), rightDesc.toLowerCase())).findFirst().isPresent();
boolean rightPmValid = !StringUtility.isNullOrEmpty(rightDesc) &&
Arrays.stream(RegExpUtility.getMatches(config.getUtilityConfiguration().getPmDescRegex(), rightDesc.toLowerCase())).findFirst().isPresent();
if (!StringUtility.isNullOrEmpty(amStr) || rightAmValid) {
if (endHour >= Constants.HalfDayHourCount) {
endHour -= Constants.HalfDayHourCount;
}
if (beginHour >= Constants.HalfDayHourCount && beginHour - Constants.HalfDayHourCount < endHour) {
beginHour -= Constants.HalfDayHourCount;
}
// Resolve case like "11 to 3am"
if (beginHour < Constants.HalfDayHourCount && beginHour > endHour) {
beginHour += Constants.HalfDayHourCount;
}
isValid = true;
} else if (!StringUtility.isNullOrEmpty(pmStr) || rightPmValid) {
if (endHour < Constants.HalfDayHourCount) {
endHour += Constants.HalfDayHourCount;
}
// Resolve case like "11 to 3pm"
if (beginHour + Constants.HalfDayHourCount < endHour) {
beginHour += Constants.HalfDayHourCount;
}
isValid = true;
}
}
if (isValid) {
String beginStr = String.format("T%02d", beginHour);
String endStr = String.format("T%02d", endHour);
if (endHour >= beginHour) {
ret.setTimex(String.format("(%s,%s,PT%sH)", beginStr, endStr, (endHour - beginHour)));
} else {
ret.setTimex(String.format("(%s,%s,PT%sH)", beginStr, endStr, (endHour - beginHour + 24)));
}
// Try to get the timezone resolution
List<ExtractResult> timeErs = config.getTimeExtractor().extract(trimmedText);
for (ExtractResult er : timeErs) {
DateTimeParseResult pr = config.getTimeParser().parse(er, referenceTime);
if (((DateTimeResolutionResult)pr.getValue()).getTimeZoneResolution() != null) {
ret.setTimeZoneResolution(((DateTimeResolutionResult)pr.getValue()).getTimeZoneResolution());
break;
}
}
ret.setFutureValue(
new Pair<LocalDateTime, LocalDateTime>(DateUtil.safeCreateFromMinValue(year, month, day, beginHour, 0, 0),
DateUtil.safeCreateFromMinValue(year, month, day, endHour, 0, 0)));
ret.setPastValue(
new Pair<LocalDateTime, LocalDateTime>(DateUtil.safeCreateFromMinValue(year, month, day, beginHour, 0, 0),
DateUtil.safeCreateFromMinValue(year, month, day, endHour, 0, 0)));
ret.setSuccess(true);
}
}
}
}
return ret;
}