Extract date/duration from text in Python

Viewed 152

The text and output to be extracted are similar to the following :

"Check it every two weeks" - two weeks

"Check it on day 1 and day 14" - day 1 and day 14

"day 19 and day fourteen are important" - day 19, day fourteen

"Check it on days 11 and 14" - day 11, day 14

"check it on day one and twelve" - day one, day twelve

I have tried using SUTime library to extract the necessary information, but it just works in the case of first example and fails to properly extract information from the rest. Considering the many ways in which the same text can be written, it is not very feasible to use ReGex. Are there any other solutions for this problem?

1 Answers

This is a good question, because if you really wanted to make sure you were picking up each mention of a day or duration, you would probably end up using machine learning, because then your program could be trained to recognise phrases like "every fortnight" or "bi-annually", which aren't numerical values. However, I think it might be wiser, for something like this, to go for an approach which doesn't require so much coding and effort. For a start, your program should recognise, judging by what you showed there, either an "every n days/weeks" and "check on days n and m" (n and m represent arbitrary numbers there). For a start, I would recommend using the words2num Python Library to convert any text, like "fourteen" in your example, to an integer. You would need to use a regex, probably, to parse the text - but only for words. Numbers can be found simply by using the split() builtin, to split the string into substrings of words by a delimiter, for which a space would be fine here. Then you could use the isdigit() function to see if it is an integer, and then apply your rule.

I realise these aren't step by step instructions, but instead it is one option. If you really want to make it efficient then Natural Language or machine learning would be the best path to take, but you could manage with basic parsing and Regex if you wanted.

Related