# How to train a supervised sequence classifier like CRF, if we have to extract start date and end date from a user query in python

I have to build a chatbot, in python in which a user can apply for a leave. I want to extract start date and end date from a users query. I did some research on couple of algorithms and found CRF Entity Extractor as the best one. I now want to see a similar implemented solution in python, which I can use it as a reference. I would like to see an end to end solution right from training dataset to predicting start and end dates from query. Kindly help.

For example:

1.query: "I want to take leave from 2nd April to 5th April." Predicted dates: "02-04-2020" and "05-04-2020"

1.query: "I want to take leave on next Monday" Predicted dates: "09-03-2020"

I'm not sure that building a model using CRF is the best approach here. It's going to require quite a bit of training data and effort to get it working like you want. Dates are pretty structured in most cases so there are more straightforward approaches to extracting them. Stanford's SUTime library, for instance, does exactly what you're describing in your question. Although is is Java based there is a Python wrapper for it. For example, for the following input:

query = 'I need a desk for tomorrow from 2pm to 3pm'

Using the SUTime Python wrapper, this is the parsed result you'd get:

[
{
"end": 26,
"start": 18,
"text": "tomorrow",
"type": "DATE",
"value": "2016-10-14"
},
{
"end": 42,
"start": 27,
"text": "from 2pm to 3pm",
"type": "DURATION",
"value": {
"begin": "T14:00",
"end": "T15:00"
}
}
]