AMAZON.DATE 插槽类型的自定义处理以仅查找过去的日期

Custom handling for AMAZON.DATE slot type to find Past Dates only

问题

我在文档中知道 AMAZON.DATE 插槽类型默认查找未来日期。据我所知,默认情况下我无法将其设置为过去的日期。

如果是这种情况,我可以在我的代码中做什么来处理这个问题?

例子

我的案例的更简单版本:从特定日期获取新闻文章。对于今天的日期 (2021-11-14),一位用户说了以下内容:

User Input Intended Date input_date diff = (input_date- today).days Handling
Get me news articles from last Wednesday 2021-11-10 2021-11-10 -4 Accept as-is
Get me news articles from November 10th 2021 2021-11-10 2021-11-10 -4 Accept as-is
Get me Wednesday's news 2021-11-10 2021-11-17 3 Subtract 7 days
Get me the news from November 10th 2021-11-10 2022-11-10 361 Returned date is in future: needs 1 year subtracted
Get me the news for next week 2022-11-15 2022-11-15 1 Returned date is in future: user specifically asked for future news (for some reason), should give user friendly error response

我试过的接近解决方案

这是我在 Python 中模拟的一个函数,用于故障排除和一些详细注释:

def getArticleDate(input_date):
    today = datetime.date.today()
    diff = (input_date - today).days

    if diff <= 0:
        # === CASE A ===
        # Condition: input_date is in the past
        # Input: User must have been specific enough or used keyword like "last" or "previous"
        # Example input: "from last Wednesday", or "June 19th 2021", or "in the previous month"
        # Handling: None

        return input_date

    if diff <= 7:
        # === CASE B ===
        # Condition: input_date is up to a week in the future
        # Input: User MIGHT have specified day of week
        # Example input: "Wednesday" on any day but Wednesday
        # Handling: Subtract 7 days

        return input_date - datetime.timedelta(days=7)

    if input_date.month == today.month and input_date.year == today.year:
        # === CASE C ===
        # Condition: input_date is within the same month and year as today, but more than 1 week in the future
        # Input: User MIGHT have specified the day of the month but not the month
        # Example input: "on the 21st"
        # Handling: Get the last occurrance of that day of the month
        # Note: Usually, but not necessarily the previous month, e.g. user says "on the 31st" on October 20th and there is no September 31st so must be July 31st

        end_of_month = today
        while end_of_month.day < input_date.day:
            end_of_month = end_of_month.replace(day=1) - datetime.timedelta(days=1)
        return end_of_month.replace(day=input_date.day)

    if input_date.replace(year=input_date.year - 1) < today:
        # === CASE D ===
        # Condition: input_date is more than a week in the future but no more than 1 year
        # Input: User MIGHT have specified day and a month, but not a year
        # Example: "May 10th", or "on 27th September"
        # Handling: Subtract 1 year
        return input_date.replace(year=input_date.year - 1)

    # === CASE E ===
    # Condition: input_date is more than 1 year in the future
    # Input: User must have specified a date more than 1 year in the future
    # Example: "August 21st, 2022"
    # Handling: Raise error
    raise ValueError(
        f"{input_date.isoformat()} is out of range."
    )

这在两种情况下不起作用:

  1. 用户指定了未来一年以内的日期,例如“明天”或“下个月”或“2022 年 1 月 30 日”。相反,它由 CASE BCASE CCASE D.
  2. 处理
  3. 用户指定了一个月中的某一天,而不是月份,并且 input_date 是未来不到一周的时间,例如《从十七日起》11月14日。这应该由 CASE C 处理,但由 CASE B 处理。切换顺序正好扭转了问题,使指定工作日的输入由 CASE C 而不是 CASE B.
  4. 处理

感谢您的帮助,我一直被困在这个问题上!

不幸的是,Amazon 强制使用自己的 NLU 解析引擎,并没有给出用户实际所说的原始文本。这使得这些类型的任务几乎不可能实现,并且会产生很多 “用户可能已经指定”。这为您带来了两种完全相反的方法,您可以在此处使用:

  • 只依赖亚马逊为您提供的东西并将其称为平台功能(不是错误 :))。 Amazon Alexa 用户已经习惯了,因为所有的开发者都有同样的问题,几乎所有类似的技能都会有同样的问题。
  • 使用 AMAZON.SearchQuery 插槽类型并手动处理所有内容。很难,但可行。