Share via


Generating Prompts

  Microsoft Speech Technologies Homepage

The functions that generate the question and confirmation prompts used by the ColorChooser control are stored in a separate JScript file labeled ColorChooser-prompt.js. To reduce the risk of name conflicts, ColorChooser generates these functions as methods of a JScript object of type CCPrompt as shown in the following code sample.

function CCPrompt(colorObject, question, promptSelectFunctionName, availableColors)
{
    this.ColorObject = colorObject;

    this.Question = question;
    this.QuestionHelp = "Please choose one of the following colors";
    for(i=0; i<availableColors.length;i++)
    {
        this.QuestionHelp += ", " + availableColors[i] ;
    }
    this.QuestionHelp += ".";
    this.PreConfirm = "Did you say";
    this.PostConfirm = "?";         
    this.ConfirmHelp = "Please say yes or no, or choose another color.";
    this.Silence = "I didn't hear you.";
    this.NoReco = "I didn't understand you.";
        
    this.PromptSelectFunctionName = promptSelectFunctionName;
}

ColorChooser creates PromptSelectFunction functions for the following question and confirm prompts. These functions serve as switches based on the last command or exception.

CCPrompt.prototype.QuestionPromptSelectFunction = function(reason, count, answers)
{
    return this.GenericPromptSelectFunction(reason, count, answers, "question", this.Question, this.QuestionHelp);
}

CCPrompt.prototype.ConfirmPromptSelectFunction = function(reason, count, answers)
{
    var toConfirm = "";
    /* We only have one answer. We just grab it whatever the key in the object is. */
    foreach(i in answers) 
    {
        toConfirm = answers[i].value;
    }

    var confirmationPrompt = this.PreConfirm + " " + toConfirm + " " + this.PostConfirm;

    return this.GenericPromptSelectFunction(reason, count, answers, "confirm", confirmationPrompt, this.ConfirmHelp);   
}

The ColorChooser control must be able to switch to an appropriate prompt when the speech recognition engine does not recognize the user's speech, or when the user remains silent. An author-defined prompt function named GenericPromptSelectFunction handles unrecognized speech and silence.

The parameters for GenericPromptSelectFunction are: reason, count, answers, qa, prompt, and helpPrompt. The first three parameters are the same as those used by the QA PromptSelectFunction functions. The qa parameter specifies which step in the application control execution cycle requires a prompt. The value of this parameter can be either question or confirm. The last two parameters provide additional information to the prompt select function. This generic function is called by both the QuestionPromptSelectFunction and the ConfirmPromptSelectFunction.

CCPrompt.prototype.GenericPromptSelectFunction = function(reason, count, answers, qa, prompt, helpPrompt)
{
    var result = null;
    if(this.PromptSelectFunctionName != "")
    {
        try
        {
            result = eval(this.PromptSelectFunctionName + "(reason, count, answers, qa, this.ColorObject)");
        }
        catch(e)
        {
            result = null;
        }
        if(result != null)
        {
            return result;
        }
    }

    if (reason == "Help") 
    { 
        return helpPrompt + " " + prompt;
    }
    if (reason == "NoReco") 
    { 
        return this.NoReco + " " + prompt;
    }
    if (reason == "Silence") 
    { 
        return this.Silence + " " + prompt;
    }
    return prompt;
}

Linking the Prompt Script to the Control

The finished ColorChooser-prompt.js file must be linked to the ColorChooser control. The OnPreRender method of the ColorChooser control links the file in the following code sample.

protected override void OnPreRender(EventArgs e)
{
    Page.RegisterClientScriptBlock("CC_prompt", "<script language=\"JScript\" src=\"./ColorChooser-prompt.js\"></script>");             
}

See Also

Creating ColorChooser: A Custom Application Speech Control | Creating the Class and SemanticItem Property | Creating the Grammars | Implementing the Dialogue Flow | Creating a Client-Side Object and SALT