Compartir a través de


Walkthrough: Use Conversational Grammar Builder to Handle Multi-Keyword Responses

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Use Conversational Grammar Builder to author speech grammars that recognize responses containing more than one semantic item at a time. Create a grammar that recognizes a response containing both destination and date, when a user is asked only for a destination.

Prerequisites

  • Speech Server.
  • Sufficient permissions to create and debug ASP.NET Web application projects on the computer where Speech Server is installed.

Designing Multi-Keyword Grammars

Authors should use caution when considering adding a multi-keyword grammar to a speech application.

Covering all the permutations that are opened by a multi-keyword grammar is difficult. The following example involves only two keywords. With only two keywords in a response, the author has to consider at least these possibilities:

  • Only the first keyword is recognized
  • Only the second keyword is recognized
  • Both keywords are recognized
  • Neither keyword is recognized

The author might also want to consider explicit code to handle cases where both keywords are recognized, while one of the keywords violates a business rule (for example, withdrawing more than the account balance or picking a destination that is not available). If the grammar is designed to handle more than just two keywords, the possibilities increase far beyond this.

Furthermore, allowing a multi-keyword response implies a prompt style that is more relaxed than normal (for example, "What kind of latte would you like?" rather than "Would you like mocha, latte, or cappuccino?"). More open-ended prompts encourage a variety of unexpected responses.

Authors are advised not to add multi-keyword grammars unless they are prepared to deal with the complexities they introduce to the grammar and workflow design.

Creating Keyword Grammars

Eventually, this grammar contains two semantic items, with possible values for each item, and words to recognize for each value.

Create the Grammar and Add Semantic Items

First create the project, and then add to the grammar a single semantic item and three possible values for the semantic item.

To add keywords

  1. Create a new voice response application project, and then name the project MultiKeyword.

    For more information about creating voice response applications, see How to: Create a Voice Response Application.

  2. In Solution Explorer, double-click the .gbuilder file under the Grammars node.

  3. In the Keywords tree view pane, right-click Keywords, and then click Add Keyword Container.

  4. Type Destination, and then press ENTER.

    This represents one semantic item in this grammar.

  5. Right-click Destination, click Add Keyword, type France, and then press ENTER.

    This represents one possible value for the semantic item.

  6. Add two more keywords, UK and Italy.

    The grammar now contains a single semantic item, with three possible values for the semantic item.

Now, add words to recognize for the three values.

To add keyword phrases

  1. In the Keywords tree view pane, select the keyword France.

  2. In the Keyword Phrase pane, click the en-US tab, double-click the first row, type Paris, and then press ENTER.

  3. In the next row, type Nice.

    These two entries represent words to recognize for the selected value in the Keywords pane.

  4. In the Keywords tree view pane, select the keyword UK, and then add London and Birmingham as words to recognize.

  5. In the Keywords tree view pane, select the keyword Italy, and then add Rome and Milan as words to recognize.

    The grammar now contains two words to recognize for each of the three values for the one semantic item.

So far, if a user says "Nice," the returned semantic value is France, for the semantic item Destination. Now, add the second semantic item, its keywords, and values. Using the steps described in the two previous procedures, add the items listed in the following table.

Item Value

Keyword Container

DepartureDate

Keywords

Today, Tomorrow

Values for the keyword Today

now, today

Values for the keyword Tomorrow

later, tomorrow

Add Response Data

So far, this is a typical grammar. The key step is adding the training sentence answers. Training sentences indicate how the grammar can parse answers that include more than one keyword.

To add training sentence responses

  1. In the Answers pane, right-click Answers, and then click Add Keyword Answer.

  2. Type Travel, and then press ENTER.

  3. Right-click Travel, click Add Keyword Reference, and then click Destination.

  4. Repeat the previous step, and then select DepartureDate as a keyword reference.

  5. Select Travel, and then type the following training sentences in the Answer Examples pane:

    • I want to go to Rome today

    • London tomorrow please

    • I think Paris tomorrow

    • Can I go to Milan later

    • How about Birmingham now

    • I want to go to Nice today

      Note

      The response phrases do not include periods. Do not add punctuation to entries in a Conversational Grammar Builder grammar.

  6. Save the grammar.

Validate and Build the Grammar

The grammar must be parsed and compiled before it is used. By default, the grammar MultiKeyword.gbuilder, when compiled, is output in the Grammar folder as MultiKeyword_EN_US.cfg. Before the grammar is compiled, build the lexicon file.

To build the lexicon file

  1. In Solution Explorer, double-click the .cal file in the Lexicons node.

  2. Click Build.

To parse, validate, and build the grammar

  1. In the Answers pane, right-click travel, and then click Parse.

    In the Answer Examples pane, the colored text indicates semantic items. The uncolored preamble and postamble text is training text that the grammar engine uses to learn to recognize the occurrence of semantic items in spoken text.

  2. On the Grammar menu, click Validate Grammar.

    Note the text in the output pane.

  3. On the Grammar menu, click Compile All.

Ask the User a Question

Add a QuestionAnswerActivity activity and a StatementActivity activity. Use the QuestionAnswer activity to ask a question of the user. Use the Statement activity to provide an appropriate response to the user.

To ask a question of the user

  1. In the Toolbox, click the Speech Dialog Components tab, and then drag and drop a QuestionAnswer activity onto the design surface between answerCallActivity1 and disconnectCallActivity1.

  2. Set the Name property to askTravelQuestion.

  3. On the design surface in askTravelQuestion, click Edit Prompts.

  4. On QuestionAnswer Property Builder, type Where would you like to travel to in the Main box, and then click OK.

  5. On the design surface in askTravelQuestion, click Attach Grammar.

  6. In QuestionAnswer Property Builder, click Travel on the Grammar tab, and then click OK.

  7. Drag and drop a Statement onto the design surface between askTravelQuestion and disconnectCallActivity1.

  8. Set the Name property to confirm.

Confirm Answers and Partial Answers

The author has a challenge at this point. There are at least three possible scenarios:

  • The end-user provides destination information.
  • The end-user provides travel date information.
  • The end-user provides both destination and date information.

The application must be able to respond appropriately. Use code to do this. A multi-key grammar produces an array of SemanticValue objects. Use the ContainsKey method to select members of the array.

To respond to the user

  1. Right-click confirm, and then click Generate Handlers.

  2. In the code window, enter the following statements in the event handler generated by Speech Server, and then build the project.

    //Did the user provide both answers?
    if (this.askTravelQuestion.RecognitionResult.Semantics.ContainsKey("Destination") == true & this.askTravelQuestion.RecognitionResult.Semantics.ContainsKey("DepartureDate") == true)
    {
      string departureDateResponse = this.askTravelQuestion.RecognitionResult.Semantics["DepartureDate"].Value.ToString();
      string destinationResponse = this.askTravelQuestion.RecognitionResult.Semantics["Destination"].Value.ToString();
      this.confirm.MainPrompt.AppendText("Thank you");
      this.confirm.MainPrompt.AppendBreak(TimeSpan.FromSeconds(1));
      this.confirm.MainPrompt.AppendText(destinationResponse);
      this.confirm.MainPrompt.AppendBreak(TimeSpan.FromSeconds(1));
      this.confirm.MainPrompt.AppendText(departureDateResponse);
    }
    
    //Did the user provide a "destination" answer?
    else if (this.askTravelQuestion.RecognitionResult.Semantics.ContainsKey("Destination")== true)
    {
      //If yes, confirm by repeating back the user's answer.
      this.confirm.MainPrompt.SetText(this.askTravelQuestion.RecognitionResult.Semantics["Destination"].Value.ToString());
    
      //Now, prompt the user to answer the "departure date" question.
      if (this.askTravelQuestion.RecognitionResult.Semantics.ContainsKey("DepartureDate") == false)
      {
        //this.askTravelQuestion.MainPrompt.ClearContent();
        this.confirm.MainPrompt.AppendBreak(TimeSpan.FromSeconds(1));
                        this.confirm.MainPrompt.AppendText("When do you want to depart?");
      }
    }
    
    //Did the user provide a "departure date" answer?
    else if (this.askTravelQuestion.RecognitionResult.Semantics.ContainsKey("DepartureDate")== true)
    {
      //If yes, confirm by repeating back the user's answer.
      this.confirm.MainPrompt.SetText(this.askTravelQuestion.RecognitionResult.Semantics["DepartureDate"].Value.ToString());
    
      //Now, prompt the user to answer the "destination" question.
      if (this.askTravelQuestion.RecognitionResult.Semantics.ContainsKey("Destination") == false)
      {
        //this.askTravelQuestion.MainPrompt.ClearContent();
        this.confirm.MainPrompt.AppendBreak(TimeSpan.FromSeconds(1));
        this.confirm.MainPrompt.AppendText("Where do you want to go?");
      }
    }
    

Run the Application

Finally, run the application in the debugger and test various responses to see how the application responds.

To run the application in the debugger

  1. Press F5.

  2. In the Debugging Not Enabled dialog box, click OK.

  3. In the Voice Response Debugging Window dialog box, click the SIP Phone tab.

  4. When the application is finished initializing, click Call.

  5. On the User Input tab, type Milan in the Text Input box, and then click Submit.

    The application should confirm the destination and ask for departure information.

  6. In the Voice Response Debugging Window dialog box, click Call again, and this time respond by typing London tomorrow.

    The application should respond by saying thank you and confirming both answers, with a pause inserted between each part of the response.

See Also

Other Resources

Use Conversational Grammar Builder to Create Grammars
Use Speech Grammar Editor to Create Grammars