0
votes

I'm using spaCy as NLP library to detect named entities. I want to extract dates and time reference fom the text automatically. For example, capture the date in this sentence: I will go to the show on 1/1/2020 and detect that 1/1/2020 is a DATE named entity.

But I also want to understand relative time phrases, for example I will go to the show tomorrow. tomorrow is detected as DATE named entity, but I don't know which time it refers - if today is 1/1/2020 than tomorrow is 1/2/2020. I want to get the 1/2/2020 directly from the named entity, even if it relative.

I tried to do that manually by created a dictionary, but the date named entities are very wide and I miss them with a static dictionary.

Is there any way to receive the actual time from a relative date named entity?

1
Hello, what have you tried? Please provide a minimal reproducible example for us to aid you. Please review how to ask for a quicker response. Also, take some time to review "Named Entity Recognition" in spaCy, as it sounds like a potential solution for you. - APhillips
@APhillips, I didn't try anything except of string compare of specific words like "tomorrow" or "today". I have already saw that docs of spaCy but there is nothing about that - nrofis

1 Answers

1
votes

You can try the dateparser library. Link to Docs

pip install dateparser

Example:

from dateparser import parse
from dateparser.search import search_dates

print(parse('Tomorrow'))
print(parse('01/01/20'))
print(search_dates("I will go to the show tomorrow"))
print(search_dates("The client arrived to the office for the first time in March 3rd, 2004 and got serviced, after a couple of months, on May 6th 2004, the customer returned indicating a defect on the part"))

output:

2020-01-30 21:03:17.551187
2020-01-01 00:00:00
[('tomorrow', datetime.datetime(2020, 1, 30, 21, 6, 19, 545368))]
[('in March 3rd, 2004 and', datetime.datetime(2004, 3, 3, 0, 0)), 
 ('on May 6th 2004', datetime.datetime(2004, 5, 6, 0, 0))]