Recognizers-Text
Recognizers-Text copied to clipboard
[EN DateTimeV2] "by last month", "by current month" is crashing the library
Describe the bug
by last month, by current month is crashing the library, even LUIS recognition is not accurate. Unable to parse the above utterances. But for last month & for current month work fine in LUIS and this library. Probably due to by
string in these utterances.
To Reproduce
Steps to reproduce the behavior:
1.Try asking Sales by last month or Sales by current month
2.LUIS is not recognising accurate. Refer screenshot.
3.Due to
by
string, this library is crashing.
[ { start: 25,
end: 37,
resolution: { values: [Array] },
text: 'by last month',
typeName: 'datetimeV2.daterange' } ]
Expected behavior
This library should return array of values like in the case of 'Sales for last month' or 'Sales for current month'
{ isFutureDate: false,
value: [ 2019-12-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z ] }
Sample input/output 1.Sales by last month 2.Sales by current month
Platform (please complete the following information):
- Platform: [Node: v11.6.0]
- Environment: [npm package: moment:2.24.0]
- Version of package [e.g. v1.1.4]
export function validateAndExtractDateRange(input: string) { try { _logger.info(
validateAndExtractDateRange`);
_logger.debug(input);
var results = recognizeDate(input);
_logger.info(`recognizeDate results:`);
_logger.debug(results);
// Check there are valid results
if (results && results[0].typeName.startsWith('datetimeV2')) {
// The DateTime model can return several resolution types (https://github.com/Microsoft/Recognizers-Text/blob/master/JavaScript/recognizers-date-time/src/dateTime/constants.ts#L2-L9)
// We only care for those with a date, date and time, or date time period:
// date, daterange, datetime, datetimerange
var first = results[0];
var subType = first.typeName.split('.')[1];
var resolutionValues = first.resolution && first.resolution.values;
if (!resolutionValues) {
// no resolution values
return {
isFutureDate: false
}
}
if (subType.includes('date') && !subType.includes('range')) {
// a date (or date & time) or multiple
var moments = resolutionValues.map((m: any) => new Date(m.value));
var moment = moments.find(isFuture) || moments[0]; // Look for the first future moment; default to first resolution
if (isFuture(moment)) {
// a future moment, valid!
return {
isFutureDate: true,
value: moment
};
}
// a past moment
return {
isFutureDate: false,
value: moment,
}
} else if (subType.includes('date') && subType.includes('range')) {
// range
var from = new Date(resolutionValues[0].start);
var to = new Date(resolutionValues[0].end);
if (!isNaN(from.getTime()) && !isNaN(to.getTime())) {
if (isFuture(from) && isFuture(to)) {
// future
return {
isFutureDate: true,
value: [from, to]
};
}
// past
return {
isFutureDate: false,
value: [from, to]
};
}
}
}
return {
isFutureDate: false
};
} catch (err) { _logger.error(err); return { isFutureDate: false }; } }`
[2020-01-28T18:12:54.845] [INFO] default - validateAndExtractDateRange [2020-01-28T18:12:55.004] [INFO] default - recognizeDate results: [2020-01-28T18:12:55.004] [DEBUG] default - [ { start: 25, end: 39, resolution: { values: [Array] }, text: 'by last quarter', typeName: 'datetimeV2.daterange' } ] The text section breaks library