如何解决Alexa处理含Double表述的手机号输入问题?
I’ve run into similar headaches with Alexa’s slot handling for colloquial number phrases like "double X" when working with non-US locales—totally frustrating when you can’t rely on the built-in phone number slot! Let’s break down some reliable fixes to get this working consistently:
1. Ditch Default Slots for a Custom Digit Phrase Slot
The default AMAZON.FOUR_DIGIT_NUMBER and AMAZON.NUMBER slots aren’t trained to handle phrases like "double nine". Instead, create a custom slot type (e.g., SpokenDigitPhrase) and populate it with every possible phrase your users might use:
- Single digits:
zero,one, ...,nine - Double digits:
double zero,double one, ...,double nine - (Optional) Common sequences like
triple sevenif you expect those too
Then, update your intent to use four instances of this custom slot (or however many groups you prefer) instead of the default number slots. This tells Alexa exactly what kinds of phrases to listen for, cutting down on those random ? values.
2. Add Targeted Sample Utterances
Alexa’s NLP learns from examples, so flood your intent’s utterances with phrases that include "double" variations. For example:
My number is {DigitGroup1} {DigitGroup2} {DigitGroup3} {DigitGroup4}It's double five {DigitGroup2} {DigitGroup3} double sixThe number is {DigitGroup1} double seven {DigitGroup3} {DigitGroup4}{DigitGroup1} {DigitGroup2} double three {DigitGroup4}
The more diverse these examples are, the better Alexa will get at recognizing "double" phrases in context.
3. Build a Robust Parsing Function in Lambda
Once you’ve got the slot values, write Python logic to convert spoken phrases to digits. Here’s a straightforward implementation:
def parse_spoken_digits(phrase): # Map every possible spoken phrase to its digit equivalent digit_mapping = { "zero": "0", "one": "1", "two": "2", "three": "3", "four": "4", "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9", "double zero": "00", "double one": "11", "double two": "22", "double three": "33", "double four": "44", "double five": "55", "double six": "66", "double seven": "77", "double eight": "88", "double nine": "99" } # Handle cases where Alexa might split or combine words import re # Match either "double [digit]" or single digit phrases phrase_parts = re.findall(r"(double\s+\w+|\w+)", phrase.lower().strip()) parsed_digits = "" for part in phrase_parts: parsed_digits += digit_mapping.get(part.strip(), "") return parsed_digits # Usage in your Lambda handler slot1 = handler_input.request_envelope.request.intent.slots["DigitGroup1"].value or "" slot2 = handler_input.request_envelope.request.intent.slots["DigitGroup2"].value or "" slot3 = handler_input.request_envelope.request.intent.slots["DigitGroup3"].value or "" slot4 = handler_input.request_envelope.request.intent.slots["DigitGroup4"].value or "" full_phone_number = parse_spoken_digits(slot1) + parse_spoken_digits(slot2) + parse_spoken_digits(slot3) + parse_spoken_digits(slot4)
4. Add Fault Tolerance for Missing/Invalid Slots
Even with custom slots, Alexa might occasionally return ? or incomplete values. Add logic to catch this and prompt the user for clarification:
if len(full_phone_number) != 10: # Reprompt for the full number or the missing part return handler_input.response_builder.speak( "Sorry, I didn't catch all of your phone number. Could you please repeat it for me?" ).ask( "You can say something like 'double nine seven three four three eight two double six'." ).response
5. Test Edge Cases Rigorously
Test with phrases like:
- Mixes of single and double digits:
double two five double eight three nine zero double one - Fast speech (Alexa might combine words)
- Accents or regional variations of number pronunciation
This will help you tweak your custom slot and parsing logic to cover real-world user input.
内容的提问来源于stack exchange,提问作者theNoobExpert

