命名实体识别相关日期

Named Entity recognition relative date

我正在使用 spaCy 作为 NLP 库来检测命名实体。我想自动从文本中提取日期和时间参考。比如抓取这句话中的日期:I will go to the show on 1/1/2020,检测出1/1/2020是一个DATE命名实体。

但我也想理解相对时间短语,例如I will go to the show tomorrowtomorrow 被检测为 DATE 命名实体,但我不知道它指的是哪个时间 - 如果今天是 1/1/2020,明天是 1/2/2020。我想直接从命名实体获取 1/2/2020,即使它是相对的。

我试图通过创建一个字典来手动执行此操作,但是命名实体的日期非常宽,我用静态字典想念它们。

有什么方法可以从相对日期命名实体接收实际时间吗?

您可以尝试 dateparser 库。 Link to Docs

pip install dateparser

示例:

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"))

输出:

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))]