You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Amazon Lex外出调度机器人:解析2-4pm格式时间范围的方案

Solution for Parsing Hyphenated Time Ranges in Amazon Lex

When building your out-of-office scheduling bot, handling hyphenated time ranges like 2-4pm or 10am-noon without adjusting your voice-friendly Lex utterances is totally doable with a bit of regex and logic in your Lambda validation function. Here's a step-by-step approach to split these ranges into valid StartTime and EndTime slots:

Step 1: Detect Hyphenated Time Patterns

First, use a regex to spot hyphenated time ranges in the user's input. This pattern will catch numbers with optional am/pm suffixes, plus terms like "noon" or "midnight":

import re

# Case-insensitive regex to match hyphenated time ranges
time_range_regex = re.compile(r'(\d+(?:am|pm)?)-(\d+(?:am|pm)|noon|midnight)', re.IGNORECASE)

Step 2: Split and Reconstruct Full Time Strings

Once you've extracted the hyphenated segment, you need to fill in missing am/pm suffixes so Lex can parse the times correctly. For example, 2-4pm should become 2pm (start) and 4pm (end):

def parse_hyphenated_time(time_segment):
    match = time_range_regex.match(time_segment)
    if not match:
        return None, None
    
    start_part, end_part = match.groups()
    start_time = start_part.strip()
    end_time = end_part.strip()
    
    # Add missing am/pm from end to start
    if not re.search(r'am|pm', start_time, re.IGNORECASE) and re.search(r'am|pm', end_time, re.IGNORECASE):
        suffix = re.search(r'am|pm', end_time, re.IGNORECASE).group()
        start_time += suffix
    
    # Add missing am/pm from start to end
    elif not re.search(r'am|pm', end_time, re.IGNORECASE) and re.search(r'am|pm', start_time, re.IGNORECASE):
        suffix = re.search(r'am|pm', start_time, re.IGNORECASE).group()
        end_time += suffix
    
    return start_time, end_time

Step 3: Integrate with Your Lambda Handler

In your Lambda function, check if the user's input has a hyphenated time range (and if the slots aren't already filled). Then split the range and update the StartTime and EndTime slots:

def lambda_handler(event, context):
    slots = event['currentIntent']['slots']
    user_input = event['inputTranscript'].lower()
    
    # Only process if slots are empty and a hyphenated time exists
    if not slots['StartTime'] or not slots['EndTime']:
        time_match = time_range_regex.search(user_input)
        if time_match:
            start_time, end_time = parse_hyphenated_time(time_match.group())
            slots['StartTime'] = start_time
            slots['EndTime'] = end_time
    
    # Pass updated slots back to Lex
    return {
        'sessionAttributes': event['sessionAttributes'],
        'dialogAction': {
            'type': 'Delegate',
            'slots': slots
        }
    }

Step 4: Handle Contextual Dates

Don't forget to account for date context like "tomorrow"! Lex's built-in slot resolution can pair your parsed times with the correct date, but if needed, you can append the date term to both start and end times before setting the slots (e.g., 2pm tomorrow instead of just 2pm).

Edge Cases to Test

  • Mixed suffixes: 10am-2pm (no need to add anything)
  • Noon/midnight: noon-3pm or 10am-midnight (these terms don't need am/pm)
  • Single-digit hours: 1-3pm → 1pm to 3pm
  • Case variations: 9AM-12PM or Noon-4pm

This approach keeps your Lex utterances voice-compatible while ensuring you handle all those user-friendly hyphenated time inputs.

内容的提问来源于stack exchange,提问作者Ryan Giglio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:58:28