Choices.Add Method (GrammarBuilder[])

Adds one of more GrammarBuilder object to the list of options already encapsulated in the current instance of Choices.

Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)

Syntax

'Declaration




Parameters

  • alternateChoices
    One or more GrammarBuilder objects, each of which represents a possible option for recognition.

Remarks

Because of support for implicit conversion from Choices, SemanticResultKey, and SemanticResultValue to the GrammarBuilder, these three classes may be added to a Choices object as well.

Applications can freely intermix both of Add(String[]) and Add(GrammarBuilder[]).

Example

In the example below Choices objects are created, than then added to so as to create a grammar supporting a choice of two command "Set background to [choice color string]" and "Change background to [choice color string]".

The choice of color strings is created using the Add(String[]) method, and used to create to GrammarBuilder objects.

The GrammarBuilder objects are then combined to into a Choices using [Add](https://msdn.microsoft.com/en-us/library/m:microsoft.speech.recognition.choices.add(microsoft.speech.recognition.grammarbuilder%5b%5d(v=office.13)).

A final GrammarBuilder instance is created, created using the ToGrammarBuilder method on Choices, and the appropriate grammar returned.

private Grammar CreateWithChoiceAdd() {
    Choices colorChoice = new Choices();      // Associate the string name of the color with each item.

    foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) {
        // Associate the string name of the color with each item.
        colorChoice.Add(colorName);
    }

    // Syntax "Set Background to [color]"
    GrammarBuilder setBackgroundToBuilder = new GrammarBuilder("Set Background to");
    setBackgroundToBuilder.Append(colorChoice);

    // Syntax "change Background to [color]"
    GrammarBuilder changeBackgroundToBuilder = new GrammarBuilder("change Background to");
    changeBackgroundToBuilder.Append(colorChoice);

    //Add grammar builder to choices.
    Choices choiceOfBuilders = new Choices();
    choiceOfBuilders.Add(setBackgroundToBuilder,changeBackgroundToBuilder);
    //NB We could have done with a constructor.

    GrammarBuilder gb = choiceOfBuilders.ToGrammarBuilder();
    Grammar grammar = new Grammar(gb);
    grammar.Name = "Set/Change Background to";

    return grammar;
}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

Choices Class
Choices Members
Microsoft.Speech.Recognition Namespace
GrammarBuilder
Grammar