SemanticResultKey.ToGrammarBuilder Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an instance of GrammarBuilder constructed from the current SemanticResultKey instance.
public:
System::Speech::Recognition::GrammarBuilder ^ ToGrammarBuilder();
public System.Speech.Recognition.GrammarBuilder ToGrammarBuilder ();
member this.ToGrammarBuilder : unit -> System.Speech.Recognition.GrammarBuilder
Public Function ToGrammarBuilder () As GrammarBuilder
Returns
An instance of GrammarBuilder constructed from the current SemanticResultKey
instance.
Examples
The following example creates a Grammar object that supports commands to change the background color.
A Choices object (colorChoice
) containing the list of options for background colors is filled using the Add(GrammarBuilder[]) method with GrammarBuilder instances. The GrammarBuilder instances are obtained through the ToGrammarBuilder() method on the SemanticResultValue objects created from color strings.
A GrammarBuilder is then obtained by calling ToGrammarBuilder() on a SemanticResultKey instance, which will be used to key the semantic choices in colorChoice
.
private Grammar CreateGrammarBuilderRGBSemantics()
{
// Create a set of choices, each a lookup from a color name to RGB.
// Choices constructors do not take SemanticResultValue parameters, so cast
// the SemanticResultValue to GrammarBuilder.
Choices colorChoice = new Choices();
foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))
{
SemanticResultValue colorValue=new SemanticResultValue(colorName, Color.FromName(colorName).ToArgb());
// Use implicit conversion of SemanticResultValue to GrammarBuilder.
colorChoice.Add(colorValue.ToGrammarBuilder());
}
SemanticResultKey choiceKey = new SemanticResultKey("rgb", colorChoice);
GrammarBuilder choiceBuilder = choiceKey.ToGrammarBuilder();
// Create two intermediate grammars with introductory phrase and the color choice.
GrammarBuilder makeBackgroundBuilder = "Make background";
makeBackgroundBuilder.Append(choiceBuilder);
GrammarBuilder configureBackgroundBuilder = new GrammarBuilder("Configure background as");
configureBackgroundBuilder.Append((new SemanticResultKey("rgb", colorChoice)).ToGrammarBuilder());
// Create the Grammar object, which recognizes either intermediate grammar.
Grammar grammar = new Grammar(new Choices(new GrammarBuilder[] {makeBackgroundBuilder, configureBackgroundBuilder}));
grammar.Name = "Make Background /Configure background as";
return grammar;
}
Remarks
The use of ToGrammarBuilder is equivalent to using the GrammarBuilder constructor which takes SemanticResultKey as an argument (GrammarBuilder(SemanticResultKey)).
Applies to
Σχόλια
https://aka.ms/ContentUserFeedback.
Σύντομα διαθέσιμα: Καθ' όλη τη διάρκεια του 2024 θα καταργήσουμε σταδιακά τα ζητήματα GitHub ως μηχανισμό ανάδρασης για το περιεχόμενο και θα το αντικαταστήσουμε με ένα νέο σύστημα ανάδρασης. Για περισσότερες πληροφορίες, ανατρέξτε στο θέμα:Υποβολή και προβολή σχολίων για