Understanding Date Ranges in Your Chatbot
When your chatbot performs tasks of a personal assistant like scheduling meetings or generating reports, you need to make sure it can understand dates and date ranges.
Step 1. Resolve
LUIS has a set of pre-built entities to recognize date and time (builtin.datetime
). It will understand when your users say tomorrow
, October 1st
or next week
, for example, and will convert that to a date or a duration. Couple examples:
1 | // tomorrow |
Unfortunately, the only
quarter
-based duration LUIS understands right now islast quarter
. It doesn’t recognizethis quarter
,next quarter
, or plurals likelast three quarters
.
As you can see, the resolutions are indicative, use different formats, and need to be parsed to get converted to dates and date ranges.
Step 2. Parse
When LUIS detects a datetime
entity (e.g. tomorrow
) it will send back the resolution
along with the extracted entity
itself (the word tomorrow
in this case).
First, I try to understand what time span the user asked about:
1 | const span = |
Then I parse the dates and durations with moment
:
1 | const moment = require('moment'); |
Step 3. Understand
Now we have the date representing the beginning of the period the user asked about. If today was Friday 11/18, for example, and you asked for last three weeks
, the date
would be Sun, Oct 23
(weeks start on Sunday in US unless you use isoweek
with moment
).
One date is not enough though for utterances like:
1 | please generate a service cost report for the last two weeks |
Your report generation service/API is likely to require a date range.
LUIS can also understand numbers spelled as digits like 2
or 5
or spelled as words like two
or five
. A phrase like last two weeks
will produce two entities:
1 | "entities": [ |
Last thing I need to do to understand the range, is to extract the number and do the date math:
1 | const moment = require('moment'); |
And that’s it. Now last three weeks
is understood as 10/23 - 11/12
. And last quarter
will be 10/1 0:00 - 12/31 23:59
.
Understanding Date Ranges in Your Chatbot
https://www.pveller.com/understanding-date-ranges-in-your-chatbot/