使用拼字樣式擷取位置值 - Amazon Lex

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用拼字樣式擷取位置值

Amazon Lex V2 提供內建插槽,可擷取使用者特定資訊,例如名字、姓氏、電子郵件地址或英數字元識別碼。例如,您可以使用AMAZON.LastName插槽擷取姓氏,例如「傑克遜」或「加西亞」。不過,Amazon Lex V2 可能會與難以發音或在地區設定中不常見的姓氏混淆,例如「秀蘭」。要捕獲此類名稱,您可以要求用戶通過字母或按單詞樣式拼寫提供拼寫輸入。

Amazon Lex V2 提供三種插槽引導樣式供您使用。當您設定插槽引出樣式時,它會改變 Amazon Lex V2 解譯使用者輸入的方式。

按字母拼寫 — 使用這種風格,您可以指示機器人聆聽拼寫而不是整個短語。例如,要捕獲「秀蘭」之類的姓氏,您可以告訴用戶一次拼出他們的姓氏一個字母。機器人將捕獲拼寫並將字母解析為單詞。例如,如果使用者說「x i u l a n」,則機器人會將姓氏擷取為「xiulan」。

按字拼寫 — 在語音對話中,尤其是使用電話,有幾個字母,例如「t」,「b」,「p」,這聽起來很相似。擷取英數字元值或拼字名稱時,會產生不正確的值,您可以提示使用者提供識別字詞以及字母。例如,如果對預訂 ID 的請求的語音回應是「abp123」,則您的機器人可能會改為識別「ab b 123」這個短語。如果這是一個不正確的值,你可以要求用戶提供輸入作為「a 在 alpha b 中,如在男孩 p 中,如彼得一二三」。機器人會將輸入解析為「abp123」。

使用逐字拼字時,您可以使用下列格式:

  • 「如在」(一如蘋果)

  • 「為」(蘋果的一個)

  • 「喜歡」(像蘋果一樣)

默認-這是使用單詞發音進行插槽捕獲的自然風格。例如,它可以自然地捕獲諸如「約翰·斯泰爾斯」之類的名稱。如果未指定插槽引出樣式,則機器人會使用默認樣式。對於AMAZON.AlphaNumericAMAZON.UKPostal代碼插槽類型,默認樣式支持通過字母輸入拼寫。

如果使用字母和單詞混合說出名稱「秀蘭」,例如「x 如 x 射線 i u l 中的獅子 a n」,則必須將插槽引出樣式設置為樣式。 spell-by-word spell-by-letter 風格不會識別它。

您應該創建一個語音界面,以自然的對話風格捕獲插槽值,以獲得更好的體驗。對於未使用自然樣式正確擷取的輸入,您可以重新提示使用者,並將插槽引出樣式設定為或。 spell-by-letter spell-by-word

您可以使用 spell-by-word 英文 (美國)、英文 (英國) 和英文 (澳洲) 語言的下列插槽類型和 spell-by-letter 樣式:

啟用拼字

當您從使 spell-by-letter 用者引出插槽 spell-by-word 時,可以在執行階段啟用和。您可以使用PutSession、、RecognizeTextStartConversation操作來設定拼字樣式。RecognizeUtterance您也可以啟 spell-by-word 用 spell-by-letter 和使用 Lambda 函數。

您可以在上述其中一個 API 作業的dialogAction要求中,或在設定 Lambda 回應時,使用sessionState欄位的欄位來設定拼字樣式 (如準備回應格式需詳細資訊,請參閱)。只有當對話方塊動作類型為,ElicitSlot且要引出的槽為其中一種支援的槽類型時,您才能設定樣式。

下列 JSON 程式碼顯示設定為使用此 spell-by-word 樣式的dialogAction欄位:

"dialogAction": { "slotElicitationStyle": "SpellByWord", "slotToElicit": "BookingId", "type": "ElicitSlot" }

slotElicitationStyle 欄位可以設定為 SpellByLetterSpellByWordDefault。如果您未指定值,則值會設定為Default

注意

您無法透過主控台啟用 spell-by-letter 或 spell-by-word 引出樣式。

範例程式碼

如果第一次嘗試解析不起作用的插槽值,通常會執行更改拼寫樣式。下列程式碼範例是 Python Lambda 函數,它會在第二次嘗試解析插槽時使用該 spell-by-word 樣式。

若要使用範例程式碼,您必須具備:

  • 具有一種語言的機器人,英語(GB)(en_GB)。

  • 一個意圖,「CheckAccount」與一個樣本語言,「我想檢查我的帳戶」。確定已在意圖定義的 [程式碼掛接] 區段中選取 [使用 Lambda 函數進行初始化和驗證]。

  • 意圖應該有一個AMAZON.UKPostalCode內置類型的插槽 PostalCode「「。

  • 定義了 Lambda 函數的別名。如需詳細資訊,請參閱 建立 Lambda 函數並將其附加至機器人別名

import json import time import os import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) # --- Helpers that build all of the responses --- def get_slots(intent_request): return intent_request['sessionState']['intent']['slots'] def get_session_attributes(intent_request): sessionState = intent_request['sessionState'] if 'sessionAttributes' in sessionState: return sessionState['sessionAttributes'] return {} def get_slot(intent_request, slotName): slots = get_slots(intent_request) if slots is not None and slotName in slots and slots[slotName] is not None: logger.debug('resolvedValue={}'.format(slots[slotName]['value']['resolvedValues'])) return slots[slotName]['value']['resolvedValues'] else: return None def elicit_slot(session_attributes, intent_request, slots, slot_to_elicit, slot_elicitation_style, message): return {'sessionState': {'dialogAction': {'type': 'ElicitSlot', 'slotToElicit': slot_to_elicit, 'slotElicitationStyle': slot_elicitation_style }, 'intent': {'name': intent_request['sessionState']['intent']['name'], 'slots': slots, 'state': 'InProgress' }, 'sessionAttributes': session_attributes, 'originatingRequestId': 'REQUESTID' }, 'sessionId': intent_request['sessionId'], 'messages': [ message ], 'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None } def build_validation_result(isvalid, violated_slot, slot_elicitation_style, message_content): return {'isValid': isvalid, 'violatedSlot': violated_slot, 'slotElicitationStyle': slot_elicitation_style, 'message': {'contentType': 'PlainText', 'content': message_content} } def GetItemInDatabase(postal_code): """ Perform database check for transcribed postal code. This is a no-op check that shows that postal_code can't be found in the database. """ return None def validate_postal_code(intent_request): postal_code = get_slot(intent_request, 'PostalCode') if GetItemInDatabase(postal_code) is None: return build_validation_result( False, 'PostalCode', 'SpellByWord', "Sorry, I can't find your information. " + "To try again, spell out your postal " + "code using words, like a as in apple." ) return {'isValid': True} def check_account(intent_request): """ Performs dialog management and fulfillment for checking an account with a postal code. Besides fulfillment, the implementation for this intent demonstrates the following: 1) Use of elicitSlot in slot validation and re-prompting. 2) Use of sessionAttributes to pass information that can be used to guide a conversation. """ slots = get_slots(intent_request) postal_code = get_slot(intent_request, 'PostalCode') session_attributes = get_session_attributes(intent_request) if intent_request['invocationSource'] == 'DialogCodeHook': # Validate the PostalCode slot. If any aren't valid, # re-elicit for the value. validation_result = validate_postal_code(intent_request) if not validation_result['isValid']: slots[validation_result['violatedSlot']] = None return elicit_slot( session_attributes, intent_request, slots, validation_result['violatedSlot'], validation_result['slotElicitationStyle'], validation_result['message'] ) return close( intent_request, session_attributes, 'Fulfilled', {'contentType': 'PlainText', 'content': 'Thanks' } ) def close(intent_request, session_attributes, fulfillment_state, message): intent_request['sessionState']['intent']['state'] = fulfillment_state return { 'sessionState': { 'sessionAttributes': session_attributes, 'dialogAction': { 'type': 'Close' }, 'intent': intent_request['sessionState']['intent'], 'originatingRequestId': 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }, 'messages': [ message ], 'sessionId': intent_request['sessionId'], 'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None } # --- Intents --- def dispatch(intent_request): """ Called when the user specifies an intent for this bot. """ intent_name = intent_request['sessionState']['intent']['name'] response = None # Dispatch to your bot's intent handlers if intent_name == 'CheckAccount': response = check_account(intent_request) return response # --- Main handler --- def lambda_handler(event, context): """ Route the incoming request based on the intent. The JSON body of the request is provided in the event slot. """ # By default, treat the user request as coming from # Eastern Standard Time. os.environ['TZ'] = 'America/New_York' time.tzset() logger.debug('event={}'.format(json.dumps(event))) response = dispatch(event) logger.debug("response={}".format(json.dumps(response))) return response