Hello Pasindu and Arun,
Following up on the initial confirmation that the built-in email
entity type cannot handle spelled-out addresses, here is a robust and effective solution to solve this problem using a combination of a String
entity and a custom JavaScript function.
The core idea is to first capture the user’s entire spelled-out phrase as a single string, and then use a script to reassemble and validate it.
The How-To Guide: A Step-by-Step Implementation
This solution involves three key steps within your Dialog Task:
Step 1: Configure the Entity Node to Capture the Input Instead of using the email
entity type, configure your entity node to use the type String . This will capture the user’s full utterance (e.g., “my email is j a n e dot d o e at example dot com”) without attempting to format it.
Step 2: Create a Script Node for Processing Immediately after the entity node, add a Script Node . This node will call a JavaScript function to perform the conversion and validation. You will store the returned values in context variables (e.g., context.isEmailValid
and context.formattedEmail
).
Step 3: Use a Logic Node to Check the Result After the script node, add a Logic Node (or a simple If/Else condition) to check the value of context.isEmailValid
.
- If
true
, the conversation proceeds. You can confirm the extracted email with the user: “Got it. Is your email {{context.formattedEmail}}
?”
- If
false
, the router can loop back to the entity node to ask the user again, perhaps with a more specific prompt like, “I had trouble understanding that. Could you please try spelling your email again?”
The JavaScript Function
Here is an example of how you may want to approach this problem.
/**
- =================================================================
- EMAIL PARSING UTILITY
- =================================================================
- This function is designed to handle a common user experience challenge:
- converting a spelled-out email address into a standard, usable format.
- It follows Kore.ai best practices by separating configuration (the mapper)
- from the core logic, making it easy to extend and maintain.
*/
// — Configuration Mapper —
// This object holds all the spoken words and their corresponding characters.
// To support new words (e.g., in other languages or slang), simply add them here.
const spokenWordMapper = {
’ at ': ‘@’,
’ dot ': ‘.’,
’ period ': ‘.’,
’ underscore ': ‘_’,
’ dash ': ‘-’,
’ hyphen ': ‘-’
// Add other potential spoken words here, e.g., ’ punto ': ‘.’ for Spanish.
};
/**
-
Parses a spelled-out string to construct and validate an email address.
-
@param {string} spelledOutString The raw string captured from the user.
-
@returns {object} An object with two properties:
-
- isValid (boolean): True if a valid email format was created.
-
- email (string|null): The formatted email address if valid.
*/
function parseAndValidateEmail(spelledOutString) {
// 1. Basic validation: Ensure input is a non-empty string.
if (typeof spelledOutString !== ‘string’ || !spelledOutString.trim()) {
koreDebugger.log(“Email Parser: Input was not a valid string.”);
return { isValid: false, email: null };
}
let processedString = ${spelledOutString.toLowerCase()}
;
// 2. Use the mapper to replace all known spoken words.
// This loop makes the function scalable and easy to update.
for (const word in spokenWordMapper) {
// We use a RegExp with the ‘g’ flag to replace all occurrences globally.
processedString = processedString.replace(new RegExp(word, ‘g’), spokenWordMapper[word]);
}
// 3. Remove all remaining spaces to join single characters (e.g., “j a n e” → “jane”).
let potentialEmail = processedString.trim().replace(/\s/g, ‘’);
koreDebugger.log("Email Parser: Processed string before validation: " + potentialEmail);
// 4. Validate the resulting string against a standard email format regex.
const emailRegex = new RegExp(
/^(([^<>()\.,;:\s@“]+(.[^<>()\.,;:\s@”]+)*)|(“.+”))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/
);
const isValid = emailRegex.test(potentialEmail);
// 5. Return the final result object.
if (isValid) {
koreDebugger.log(“Email Parser: Validation successful.”);
return { isValid: true, email: potentialEmail };
} else {
koreDebugger.log(“Email Parser: Validation failed.”);
return { isValid: false, email: null };
}
}
/*
// — Example Kore.ai Implementation —
// This script would run in a Script Node that follows an Entity Node.
// Let’s assume the Entity Node is named ‘SpelledOutEmail’. The user’s
// raw input would be stored in ‘context.entities.SpelledOutEmail’.
// 1. Read the raw string from the context object.
var rawInput = context.entities.SpelledOutEmail || “”;
// 2. Call the function with the user’s input.
var validationResult = parseAndValidateEmail(rawInput);
// 3. Store the results back into the context object.
// These new context variables can now be used by other nodes in the Dialog Task,
// such as a Router Node for conditional logic or a Message Node to confirm the email.
context.isEmailValid = validationResult.isValid;
context.formattedEmail = validationResult.email;
// 4. (Optional) Log to the debug console for testing.
koreDebugger.log("Email Validation Result: " + JSON.stringify(validationResult));
*/