I am using dateutil.parser
to extract date from text, as follows:
def extract_date(text):
try:
date = dparser.parse(text, fuzzy=True, dayfirst=True)
return f'{date.day}/{date.month}/{date.year}'
except Exception:
return 'NOTFOUND'
print(extract_date('some sample text 01-12-24))
>>> 1/12/2024
Which works perfectly fine when the date is fully defined. But when I give text like this which is incomplete,
print(extract_date('some sample text 1-12'))
>>> 1/12/2022
As you can see here 'year' is not defined but it automatically adds the current year which is '2022' which I don't want!
I tried making fuzzy=False
but then it is not able to extract the date at all. So, is there any solution to avoid this behavior or any kind of technique?