Share via


Did you say blue? No, red.

POSTED BY: AHMED STEWART, MSS Software Dev Engineer

Today I’d like to talk about one of the new activities that we’ve created in MSS 2007 Beta to handle common tasks, the Get and Confirm activity. Like the name suggests, this activity simple gets an utterance from the user and based on the confidence of the recognition, confirms it with the user or not.

The GetAndConfirmActivity, G&C for short, allows the developer to specify a grammar and based on the confidence of the recognition confirm the recognition. If the confirmation is successful, the activity exits, otherwise the get turn is started again to get a new recognition.

The control has default confirmation grammars that can recognize, yes and no as well as accept an updated reply. E.g.:

     App: Did you say blue?

     Caller: No, Red.

     App: Did you say red?

The example below will walk through a very simple usage of the Activity and shows how to add and use a custom Dtmf grammar during the confirmation phase of the recognition. I prefer to give examples and describe what they do so hopefully everything is nice and easy to understand.

I like to use the Executing handler to do things that are not going to change for the duration of the control’s execution so in the handler below I assign the grammar and prompt for the “get” turn to the control. The next section of code generates and assignes the Dtmf grammar for the confirmation phase as well as specifying a handler for the Grammar’s recognized event where the determination is made as to accept or deny the recognition if Dtmf is entered. By default, the confirmation threshold is set to one so the recognition is always confirmed, to alter it simply change the value. The confirmation threshold is the starting point where confirmation will not occur.

     private void getAndConfirmActivity1_Executing(object sender, ActivityExecutionStatusChangedEventArgs e)
     {
string[] grammarChoices = new string[] { "one", "two", "three", "four", "five" };

getAndConfirmActivity1.Grammars.Add(GenerateGrammar(SrgsGrammarMode.Voice,grammarChoices));
          getAndConfirmActivity1.MainPrompt.ClearContent();
          getAndConfirmActivity1.MainPrompt.AppendText("Say a number between one and five now.");

          string[] grammarOptions = new string[] { "1", "2" };
          Grammar confirmationDtmfGrammar = GenerateGrammar(SrgsGrammarMode.Dtmf, grammarOptions);
          confirmationDtmfGrammar.Recognized += new EventHandler<RecognizedEventArgs>(confirmationDtmfGrammar_Recognized);

getAndConfirmActivity1.ConfirmationDtmfGrammars.Add(confirmationDtmfGrammar);

     }

The Confirmation Turn starting handler is pretty basic, it just specifies the prompt that will be played during the confirmation turn.

     private void getAndConfirmActivity1_ConfirmationTurnStarting(object sender, TurnStartingEventArgs e)
     {

getAndConfirmActivity1.ConfirmationMainPrompt.ClearContent();

getAndConfirmActivity1.ConfirmationMainPrompt.AppendText("The last recognition result was, " + getAndConfirmActivity1.RecognitionResult.Text +
          ", if this is correct say yes or press 1. If not say no or press 2.");

     }

The last piece of the puzzle is the handler for the confirmationDtmfGrammar_Recognized. It simply checks the value of the recognized Dtmf digits and either accepts the recognition or denies it.

     void confirmationDtmfGrammar_Recognized(object sender, RecognizedEventArgs e)
     {
          if (1 == int.Parse(e.Result.Text))
          {
               getAndConfirmActivity1.Accept(sender, e);
          }
          else
          {
               getAndConfirmActivity1.Deny(sender, e);
          }
     }

In this example the default grammars provided with the control are still active. To disable them, just set the value of getAndConfirmActivity1.UseDefaultGrammars to false.

Since I know some of you are curious about what it contains here’s the GenerateGrammar function.

     /// <summary>
     /// This function generates a grammar with the members of the provided string array as choices.
     /// </summary>
     /// <param name="grammarMode">The mode of the grammar, either Dtmf or voice.</param>
     /// <param name="grammarChoices">The string array containing the choices in the grammar.</param>
     /// <returns>The constructed grammar object.</returns>
     public static Grammar GenerateGrammar(SrgsGrammarMode grammarMode, string[] grammarChoices)
     {
          if (0 >= grammarChoices.Length)
          {
               throw new ArgumentNullException(grammarChoices.ToString());
          }

          SrgsDocument grammarDoc = new SrgsDocument();
          grammarDoc.Mode = grammarMode;

          //The name of the rule that will be created
          SrgsRule rule = new SrgsRule("GrammarOptions");
          rule.Scope = SrgsRuleScope.Public;
          SrgsItem item = new SrgsItem();
          SrgsOneOf choice = new SrgsOneOf(grammarChoices);
          item.Add(choice);
          rule.Add(item);
          grammarDoc.Rules.Add(rule);
          grammarDoc.Root = rule;
          return new Grammar(grammarDoc);
     }

Comments

  • Anonymous
    September 18, 2006
    Very usefull post Ahmed.

    I don't recall seeing you blog before. You should blog more.
  • Anonymous
    October 04, 2006
    Ahmed - I liked the article.  I've just created a custom activity that confirms the RecognitionResult Alternates list.  I needed this for a tricky alpha numeric that can be miss recognised: MSS: What's the product code Caller: 123EPG MSS: Dis you say "123BPG"? Caller: No. MSS: Was it "123EPG"? Caller: Yes! ... Regards, Andrew Morris