'Extract specific DATE entity from an sentence by the use of spaCy, and calculate the relative time
import spacy
import en_core_web_sm
nlp = en_core_web_sm.load()
doc = nlp('I get cough yesterday, and tomorrow I will go to hostipital')
for t in doc.ents:
if t.label_ == 'DATE':
print(t.text)
output: yesterday, tomorrow
but I want only 'yesterday' be extracted. how can I optimize my rule to get my expected result.
one more thing, if I already get 'yesterday' and 'Jul 26 2021', how can I get the result of 'Jul 25 2021'.
Sincerely, thanks for help.
Solution 1:[1]
You can do this using python datetime and timedelta. Convert the date to a datetime object:
import datetime
from datetime import timedelta
created_date = datetime.datetime.strptime('Jul 25 2021', '%b %d %Y')
Then check if it is "yesterday":
if t.label_ == 'DATE':
if t.text.lower() == 'yesterday':
return_date = created_date + timedelta(days=-1)
return return_date
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sanskriti Timseena |