使用 dateparser.search 模块解析文本时排除相对日期

Exclude relative dates when parsing text with the dateparser.search module

尝试从 dateparser.search 获取 search_dates 忽略相关日期,例如“明天”、“下周”、“更多”等

目前,这是我得到的行为:

from dateparser.search import search_dates

In: search_dates("more", settings={"STRICT_PARSING": True})

Out: [("more", datetime.datetime(2021, 4, 27, 11, 21, 45, 998830))]

In: search_dates("March 15, 2020", settings={"STRICT_PARSING": True})

Out: [("March 15, 2020", datetime.datetime(2020, 3, 15, 0, 0))]

我期待:

from dateparser.search import search_dates

In: search_dates("more", settings={"STRICT_PARSING": True})

Out: [("more", None)]

In: search_dates("March 15, 2020", settings={"STRICT_PARSING": True})

Out: [("March 15, 2020", datetime.datetime(2020, 3, 15, 0, 0))]

为此,您需要从 search_dates 使用的解析器列表中排除 relative-time 解析器。

from dateparser_data.settings import default_parsers
from dateparser.search import search_dates

# you start by creating a list of all parsers minus the relative-time parser
parsers = [parser for parser in default_parsers if parser != 'relative-time']

# then you pass the list you just created to the settings
search_dates('today', settings={'PARSERS': parsers})

使用您的示例:

In: search_dates("more", settings={"STRICT_PARSING": True, , 'PARSERS': parsers})

Out: None

In: search_dates("March 15, 2020", settings={"STRICT_PARSING": True, 'PARSERS': parsers})

Out: [('March 15, 2020', datetime.datetime(2020, 3, 15, 0, 0))]

docs