我正在使用spaCy作为NLP库来检测命名实体。我想自动提取文本的日期和时间参考。例如,捕获此句子中的日期:I will go to the show on 1/1/2020
并检测1/1/2020
是 DATE 命名实体。
但我也想了解相对时间短语,例如I will go to the show tomorrow
.tomorrow
被检测为 DATE 命名实体,但我不知道它指的是哪个时间 - 如果今天是1/1/2020
,那么明天是1/2/2020
.我想直接从命名实体获取1/2/2020
,即使它是相对的。
我尝试通过创建字典来手动执行此操作,但是日期命名的实体非常宽,并且我用静态字典错过了它们。
有没有办法从相对日期命名实体接收实际时间?
您可以尝试dateparser
库。链接到文档
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))]