Resolving time in NLP

"I want to go swimming next Tuesday" I want to machine to learn the date I want to go swimming. Is there any approaches or libraries that can

1. Extract "next Tuesday"

2. And calculate the exact date?

There is a Python library called dateparser that will accept a wide variety of formats, including relative dates like "next Tuesday", and return exact datetime representation.

A good solution as a service can be using Microsoft LUIS. In LUIS, you can use a prebuilt datetimeV2 entity which recognizes dates, times, date ranges and time durations. To know more about this prebuilt entity see the documentation.

I will list possible ways to approach you problem:

A- Naïve approach

1. Save current date
2. Extract day from string using regex and extract its rank in the week (example 0 for Monday 1 for Tuesday and/or so)
3. Find preceding word to output 3
4. If output 3 is "Next" --> exact date = date for the output from 2 in the next calendar week
5. If output 3 is "on"/"this" --> exact date = date for the output from 2 in the this calendar week.

If you are using python, you will need re and calendar packages.

Pros:

1. Fast in developing
2. Sufficient for most of the cases

Cons:

1. Slow in implementation, depending on how long is the length of text
2. You have to account for all possible scenarios in output 4 above

B- Classification

Create a ground truth of strings with positive and negative examples and feed it into a classifier to learn from those examples.

You would need to parse the text first as a pre-processing step (but this would be the case for most approaches, isn't it?)

Pros:

1. Fast in implementation
2. Less rule-based
3. Accuracy is expected to be high depending on the classifier

Cons:

1. Creating ground truth is a time consuming (could be minimised by combining this approach with the Naïve one or with some nlp)
2. LWill need time to train (but this is shouldn't be too long)

C- Do it the Apple way

Have a look at some information on the Apple Data Detectors

There are two parts to the question.

To solve the first problem, you can use something called Named Entity Recognition . The problem of finding date is actually a sub problem of the more general NER problem . Nevertheless , you can try the awesome Spacy NER Finder .

import spacy

doc = nlp(u'I want to go swimming next Tuesday')

for ent in doc.ents:
print(ent.text , ent.label_)


next Tuesday DATE

The code finds all the named entities in the text. You can filter out your relevant DATE entity if you get more than one kind of entity

The second problem is a bit simpler, once you have the DATE entity , its only a matter of your Python abilities. You can go by the following:

• Get the DATE entity
• Save the current date and day .
• Determine how many days you need to add to the current date by comparing today's day and the extracted day from the text.
• Add the no. of days to Today's date to get the desired date