-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathGrammarBuilder.xml
More file actions
3043 lines (2519 loc) · 177 KB
/
Copy pathGrammarBuilder.xml
File metadata and controls
3043 lines (2519 loc) · 177 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<Type Name="GrammarBuilder" FullName="System.Speech.Recognition.GrammarBuilder">
<TypeSignature Language="C#" Value="public class GrammarBuilder" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit GrammarBuilder extends System.Object" />
<TypeSignature Language="DocId" Value="T:System.Speech.Recognition.GrammarBuilder" />
<TypeSignature Language="VB.NET" Value="Public Class GrammarBuilder" />
<TypeSignature Language="F#" Value="type GrammarBuilder = class" />
<TypeSignature Language="C++ CLI" Value="public ref class GrammarBuilder" />
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.1</AssemblyVersion>
<AssemblyVersion>9.0.0.2</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Diagnostics.DebuggerDisplay("{DebugSummary}")]</AttributeName>
<AttributeName Language="F#">[<System.Diagnostics.DebuggerDisplay("{DebugSummary}")>]</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>Provides a mechanism for programmatically building the constraints for a speech recognition grammar.</summary>
<remarks>
<format type="text/markdown"><. If you are familiar with SRGS but want to generate the grammars programmatically, you can use the <xref:System.Speech.Recognition.SrgsGrammar> namespace, whose members correspond closely to the elements and attributes defined by SRGS. If you are unfamiliar with SRGS, or you want a lightweight, programmatic approach to authoring grammars with which you can efficiently accomplish many common scenarios; you can use the <xref:System.Speech.Recognition.GrammarBuilder> and <xref:System.Speech.Recognition.Choices> classes.
Use <xref:System.Speech.Recognition.GrammarBuilder> objects to build a hierarchical tree composed of <xref:System.Speech.Recognition.Choices> objects that contain alternate phrases, interspersed with preamble and post-amble phrases at each node, and seeded with semantic values that convey meaning back to the application.
To use a <xref:System.Speech.Recognition.GrammarBuilder> to create a <xref:System.Speech.Recognition.Grammar> object, use the following steps.
1. Create a <xref:System.Speech.Recognition.GrammarBuilder> object.
2. Append constraints to the <xref:System.Speech.Recognition.GrammarBuilder>, such as <xref:System.String> objects, <xref:System.Speech.Recognition.Choices>, <xref:System.Speech.Recognition.SemanticResultKey>, <xref:System.Speech.Recognition.SemanticResultValue>, <xref:System.Speech.Recognition.DictationGrammar>, and other <xref:System.Speech.Recognition.GrammarBuilder> objects that define the constraints for the grammar.
3. Use one of the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructors to create a <xref:System.Speech.Recognition.Grammar> object from the completed <xref:System.Speech.Recognition.GrammarBuilder> grammar.
Authoring with <xref:System.Speech.Recognition.GrammarBuilder> is best suited to grammars that have a single rule containing lists, or perhaps lists of lists. To programmatically build grammars that have multiple rules, or that need to make internal rule references, use the classes of the <xref:System.Speech.Recognition.SrgsGrammar> namespace.
Instances of <xref:System.Speech.Recognition.GrammarBuilder> can also be obtained by implicit conversions from certain other classes or by combining a <xref:System.Speech.Recognition.GrammarBuilder> with a second object that contains constraints for a grammar.. For more information, see the <xref:System.Speech.Recognition.GrammarBuilder.op_Implicit%2A> and <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operators and the <xref:System.Speech.Recognition.GrammarBuilder.Add%2A> methods.
To add rules to an existing <xref:System.Speech.Recognition.GrammarBuilder>, use the <xref:System.Speech.Recognition.GrammarBuilder.Add%2A>, <xref:System.Speech.Recognition.GrammarBuilder.Append%2A>, <xref:System.Speech.Recognition.GrammarBuilder.AppendDictation%2A>, <xref:System.Speech.Recognition.GrammarBuilder.AppendRuleReference%2A>, and <xref:System.Speech.Recognition.GrammarBuilder.AppendWildcard%2A> methods.
> [!IMPORTANT]
> The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element.
To help with debugging, the <xref:System.Speech.Recognition.GrammarBuilder.DebugShowPhrases%2A> property returns the current status of the <xref:System.Speech.Recognition.GrammarBuilder> as a string.
## Examples
The following example uses <xref:System.Speech.Recognition.GrammarBuilder> and <xref:System.Speech.Recognition.Choices> objects to construct a grammar that can recognize either of the two phrases, "Make background *colorChoice*" or "Set background to *colorChoice*".
The example uses a <xref:System.Speech.Recognition.Choices> object to create a list of acceptable values for *colorChoice* from an array of <xref:System.String> objects. A <xref:System.Speech.Recognition.Choices> object is analogous to the `one-of` element in the SRGS specification, and contains a set of alternate phrases, any of which can be recognized when spoken. The example also uses a <xref:System.Speech.Recognition.Choices> object to group an array of two <xref:System.Speech.Recognition.GrammarBuilder> objects into a pair of alternative phrases that the resultant grammar can recognize. Alternate words or phrases are a component of most grammars, and the <xref:System.Speech.Recognition.Choices> object provides this functionality for grammars constructed with <xref:System.Speech.Recognition.GrammarBuilder>.
The example finally creates a <xref:System.Speech.Recognition.Grammar> object from a <xref:System.Speech.Recognition.GrammarBuilder> constructed from a <xref:System.Speech.Recognition.Choices> object.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase = new GrammarBuilder("Make background");
makePhrase.Append(colorElement);
GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
setPhrase.Append(colorElement);
// Create a Choices for the two alternative phrases, convert the Choices
// to a GrammarBuilder, and construct the grammar from the result.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
<Members>
<MemberGroup MemberName=".ctor">
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Instances of this class can also be obtained by implicit conversions from other classes or by combining a <xref:System.Speech.Recognition.GrammarBuilder> object with a second object to from a new <xref:System.Speech.Recognition.GrammarBuilder>. For more information, see the <xref:System.Speech.Recognition.GrammarBuilder.op_Implicit%2A> and <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> methods.
To add constraints to an existing <xref:System.Speech.Recognition.GrammarBuilder>, use the <xref:System.Speech.Recognition.GrammarBuilder.Add%2A>, <xref:System.Speech.Recognition.GrammarBuilder.Append%2A>, <xref:System.Speech.Recognition.GrammarBuilder.AppendDictation%2A>, <xref:System.Speech.Recognition.GrammarBuilder.AppendRuleReference%2A>, and <xref:System.Speech.Recognition.GrammarBuilder.AppendWildcard%2A> methods, and the <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operator.
> [!IMPORTANT]
> The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element.
For more information about building and using speech recognition grammars, see [Speech Recognition](https://learn.microsoft.com/previous-versions/office/developer/speech-technologies/hh361633(v=office.14)).
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
</MemberGroup>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor" />
<MemberSignature Language="VB.NET" Value="Public Sub New ()" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder();" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>Initializes a new, empty instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To add rules to an existing <xref:System.Speech.Recognition.GrammarBuilder> object, use the <xref:System.Speech.Recognition.GrammarBuilder.Add%2A>, <xref:System.Speech.Recognition.GrammarBuilder.Append%2A>, <xref:System.Speech.Recognition.GrammarBuilder.AppendDictation%2A>, <xref:System.Speech.Recognition.GrammarBuilder.AppendRuleReference%2A>, and <xref:System.Speech.Recognition.GrammarBuilder.AppendWildcard%2A> methods, and the <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operator.
> [!IMPORTANT]
> The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element.
## Examples
The following example uses <xref:System.Speech.Recognition.GrammarBuilder> and <xref:System.Speech.Recognition.Choices> objects to construct a grammar that can recognize either of the two phrases, "Make background *colorChoice*" or "Set background to *colorChoice*".
The example uses a <xref:System.Speech.Recognition.Choices> object to create a list of acceptable values for *colorChoice* from an array of <xref:System.String> objects. A <xref:System.Speech.Recognition.Choices> object is analogous to the `one-of` element in the SRGS specification, and contains a set of alternate phrases, any one of which can be recognized when spoken. The example also uses a <xref:System.Speech.Recognition.Choices> object to group an array of two <xref:System.Speech.Recognition.GrammarBuilder> objects into a pair of alternative phrases that the resultant grammar can recognize. Alternate words or phrases are a component of most grammars, and the <xref:System.Speech.Recognition.Choices> object provides this functionality for grammars constructed with <xref:System.Speech.Recognition.GrammarBuilder>.
The example finally creates a <xref:System.Speech.Recognition.Grammar> object from a <xref:System.Speech.Recognition.GrammarBuilder> constructed from a <xref:System.Speech.Recognition.Choices> object.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase = new GrammarBuilder("Make background");
makePhrase.Append(colorElement);
GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
setPhrase.Append(colorElement);
// Create a Choices for the two alternative phrases, convert the Choices
// to a GrammarBuilder, and construct the Grammar object from the result.
GrammarBuilder bothPhrases = new GrammarBuilder();
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
bothPhrases.Append(bothChoices);
Grammar grammar = new Grammar(bothPhrases);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (System.Speech.Recognition.Choices alternateChoices);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.Choices alternateChoices) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.Speech.Recognition.Choices)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (alternateChoices As Choices)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.Choices -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder alternateChoices" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::Speech::Recognition::Choices ^ alternateChoices);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="alternateChoices" Type="System.Speech.Recognition.Choices" />
</Parameters>
<Docs>
<param name="alternateChoices">The set of alternatives.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class from a set of alternatives.</summary>
<remarks>
<format type="text/markdown"><.
## Examples
The following example uses <xref:System.Speech.Recognition.GrammarBuilder> and <xref:System.Speech.Recognition.Choices> objects to construct a grammar that can recognize either of the two phrases, "Make background *colorChoice*" or "Set background to *colorChoice*".
The example uses a <xref:System.Speech.Recognition.Choices> object to create a list of acceptable values for *colorChoice* from an array of <xref:System.String> objects. A <xref:System.Speech.Recognition.Choices> object is analogous to the `one-of` element in the SRGS specification, and contains a set of alternate phrases, any of which can be recognized when spoken. The example also uses a <xref:System.Speech.Recognition.Choices> object to group an array of two <xref:System.Speech.Recognition.GrammarBuilder> objects into a pair of alternative phrases that the resultant grammar can recognize. Alternate words or phrases are a component of most grammars, and the <xref:System.Speech.Recognition.Choices> object provides this functionality for grammars constructed with <xref:System.Speech.Recognition.GrammarBuilder>.
The example finally creates a <xref:System.Speech.Recognition.Grammar> object from a <xref:System.Speech.Recognition.GrammarBuilder> constructed from a <xref:System.Speech.Recognition.Choices> object.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase = new GrammarBuilder("Make background");
makePhrase.Append(colorElement);
GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
setPhrase.Append(colorElement);
// Create a Choices for the two alternative phrases, convert the Choices
// to a GrammarBuilder, and construct the grammar from the result.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (System.Speech.Recognition.SemanticResultKey key);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.SemanticResultKey key) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.Speech.Recognition.SemanticResultKey)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (key As SemanticResultKey)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.SemanticResultKey -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder key" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::Speech::Recognition::SemanticResultKey ^ key);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="key" Type="System.Speech.Recognition.SemanticResultKey" />
</Parameters>
<Docs>
<param name="key">The semantic key.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class from a semantic key.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
When you create a <xref:System.Speech.Recognition.GrammarBuilder> instance from a <xref:System.Speech.Recognition.SemanticResultValue> object, you add semantic information to the grammar that can be returned in the recognition result. You can access the semantic information in the recognition result using the <xref:System.Speech.Recognition.RecognizedPhrase.Semantics%2A> property of <xref:System.Speech.Recognition.RecognizedPhrase>, which is available in the handler for the `SpeechRecognized` event. If the <xref:System.Speech.Recognition.GrammarBuilder> defines a <xref:System.Speech.Recognition.SemanticResultKey>, this can be used to retrieve the semantic information in a recognition result that is associated with the key. See the example for <xref:System.Speech.Recognition.GrammarBuilder.Append%28System.Speech.Recognition.SemanticResultKey%29>, and also see <xref:System.Speech.Recognition.SemanticResultValue> and <xref:System.Speech.Recognition.SemanticResultKey>.
> [!IMPORTANT]
> When you construct <xref:System.Speech.Recognition.GrammarBuilder> objects that contain <xref:System.Speech.Recognition.SemanticResultValue> or <xref:System.Speech.Recognition.SemanticResultKey> instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the <xref:System.Speech.Recognition.SemanticValue.Value%2A> property of a <xref:System.Speech.Recognition.SemanticValue> object. The speech recognizer can throw an exception if it encounters these circumstances.
## Examples
The following example creates a speech recognition grammar that can recognize the two phrases, "Make background *colorChoice*" and "Set background to *colorChoice*", where *colorChoice* is selected from a set of colors. The grammar lets a user speak any of several color names, and returns semantic information about the recognized color name to the application.
The example uses a single <xref:System.Speech.Recognition.SemanticResultKey> with which you can retrieve the <xref:System.Speech.Recognition.SemanticValue> that is associated with the color spoken by the user. For example, if the input contains the phrase, "Set background to red", the recognition result contains the semantic value of "#FF0000", which you can retrieve using a handler for the `SpeechRecognized` event.
The example uses <xref:System.String>, <xref:System.Speech.Recognition.Choices>, <xref:System.Speech.Recognition.SemanticResultKey>, <xref:System.Speech.Recognition.SemanticResultValue>, and <xref:System.Speech.Recognition.GrammarBuilder> objects to build the constraints that are all contained in the last <xref:System.Speech.Recognition.GrammarBuilder> object, `bothPhrases`. Finally, the example constructs a <xref:System.Speech.Recognition.Grammar> object from the completed <xref:System.Speech.Recognition.GrammarBuilder>.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
// Include semantic information about each of the colors.
Choices colorChoice = new Choices();
GrammarBuilder colorBuilder = new GrammarBuilder("red");
SemanticResultValue colorValue =
new SemanticResultValue(colorBuilder, "#FF0000");
colorChoice.Add(new GrammarBuilder(colorValue));
colorBuilder = new GrammarBuilder("green");
colorValue = new SemanticResultValue(colorBuilder, "#00FF00");
colorChoice.Add(new GrammarBuilder(colorValue));
colorBuilder = new GrammarBuilder("blue");
colorValue = new SemanticResultValue(colorBuilder, "#0000FF");
colorChoice.Add(new GrammarBuilder(colorValue));
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase = new GrammarBuilder("Make background");
makePhrase.Append(colorElement);
GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
setPhrase.Append(colorElement);
// Create a Choices object for the two alternative phrases.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);
// Create the semantic key for referencing the color information.
SemanticResultKey colorKey =
new SemanticResultKey("ColorCode", bothPhrases);
bothPhrases = new GrammarBuilder(colorKey);
// Construct the Grammar object from the GrammarBuilder.
Grammar grammar = new Grammar(bothPhrases);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (System.Speech.Recognition.SemanticResultValue value);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.SemanticResultValue value) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.Speech.Recognition.SemanticResultValue)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (value As SemanticResultValue)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.SemanticResultValue -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder value" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::Speech::Recognition::SemanticResultValue ^ value);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="value" Type="System.Speech.Recognition.SemanticResultValue" />
</Parameters>
<Docs>
<param name="value">The semantic value or name/value pair.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class from a semantic value.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
When you create a <xref:System.Speech.Recognition.GrammarBuilder> instance from a <xref:System.Speech.Recognition.SemanticResultValue> object, you add semantic information to the grammar that can be returned in the recognition result. You can access the semantic information in the recognition result using the <xref:System.Speech.Recognition.RecognizedPhrase.Semantics%2A> property of <xref:System.Speech.Recognition.RecognizedPhrase>, which is available in the handler for the `SpeechRecognized` event. If the <xref:System.Speech.Recognition.GrammarBuilder> defines a <xref:System.Speech.Recognition.SemanticResultKey>, this can be used to retrieve the semantic information in a recognition result that is associated with the key. See the example for <xref:System.Speech.Recognition.GrammarBuilder.Append%28System.Speech.Recognition.SemanticResultKey%29>, and also see <xref:System.Speech.Recognition.SemanticResultValue> and <xref:System.Speech.Recognition.SemanticResultKey>.
> [!IMPORTANT]
> When you construct <xref:System.Speech.Recognition.GrammarBuilder> objects that contain <xref:System.Speech.Recognition.SemanticResultValue> or <xref:System.Speech.Recognition.SemanticResultKey> instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the <xref:System.Speech.Recognition.SemanticValue.Value%2A> property of a <xref:System.Speech.Recognition.SemanticValue> object. The speech recognizer can throw an exception if it encounters these circumstances.
## Examples
The following example creates a speech recognition grammar that can recognize the two phrases, "Make background *colorChoice*" and "Set background to *colorChoice*", where *colorChoice* is selected from a set of colors. The grammar lets a user speak any of several color names, and returns semantic information about the recognized color name to the application.
The example uses a single <xref:System.Speech.Recognition.SemanticResultKey> with which you can retrieve the <xref:System.Speech.Recognition.SemanticValue> that is associated with the color spoken by the user. For example, if the input contains the phrase, "Set background to red", the recognition result contains the semantic value of "#FF0000", which you can retrieve using a handler for the `SpeechRecognized` event.
The example uses <xref:System.String>, <xref:System.Speech.Recognition.Choices>, <xref:System.Speech.Recognition.SemanticResultKey>, <xref:System.Speech.Recognition.SemanticResultValue>, and <xref:System.Speech.Recognition.GrammarBuilder> objects to build the constraints that are all contained in the last <xref:System.Speech.Recognition.GrammarBuilder> object, `bothPhrases`. Finally, the example constructs a <xref:System.Speech.Recognition.Grammar> object from the completed <xref:System.Speech.Recognition.GrammarBuilder>.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
// Include semantic information about each of the colors.
Choices colorChoice = new Choices();
GrammarBuilder colorBuilder = new GrammarBuilder("red");
SemanticResultValue colorValue =
new SemanticResultValue(colorBuilder, "#FF0000");
colorChoice.Add(new GrammarBuilder(colorValue));
colorBuilder = new GrammarBuilder("green");
colorValue = new SemanticResultValue(colorBuilder, "#00FF00");
colorChoice.Add(new GrammarBuilder(colorValue));
colorBuilder = new GrammarBuilder("blue");
colorValue = new SemanticResultValue(colorBuilder, "#0000FF");
colorChoice.Add(new GrammarBuilder(colorValue));
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase = new GrammarBuilder("Make background");
makePhrase.Append(colorElement);
GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
setPhrase.Append(colorElement);
// Create a Choices for the two alternative phrases.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);
// Create the semantic key for referencing the color information.
SemanticResultKey colorKey =
new SemanticResultKey("ColorCode", bothPhrases);
bothPhrases = new GrammarBuilder(colorKey);
// Construct the grammar from the grammar builder.
Grammar grammar = new Grammar(bothPhrases);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (string phrase);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string phrase) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.String)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (phrase As String)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : string -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder phrase" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::String ^ phrase);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="phrase" Type="System.String" />
</Parameters>
<Docs>
<param name="phrase">The sequence of words.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class from a sequence of words.</summary>
<remarks>
<format type="text/markdown"><.
## Examples
The following example uses <xref:System.Speech.Recognition.GrammarBuilder> and <xref:System.Speech.Recognition.Choices> objects to construct a grammar that can recognize either of the two phrases, "Make background *colorChoice*" or "Set background to *colorChoice*".
After creating a list of acceptable values for *colorChoice* using a <xref:System.Speech.Recognition.Choices> object, the example initializes two <xref:System.Speech.Recognition.GrammarBuilder> objects, `makePhrase` and `setPhrase`, using a string as an argument.
The example finally creates a <xref:System.Speech.Recognition.Grammar> object from a <xref:System.Speech.Recognition.Choices> object cast to a <xref:System.Speech.Recognition.GrammarBuilder> object.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase = new GrammarBuilder("Make background");
makePhrase.Append(colorElement);
GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
setPhrase.Append(colorElement);
// Create a Choices for the two alternative phrases, convert the Choices
// to a GrammarBuilder, and construct the Grammar object from the result.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
<altmember cref="T:System.Speech.Recognition.SemanticResultKey" />
<altmember cref="T:System.Speech.Recognition.SemanticResultValue" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (string phrase, System.Speech.Recognition.SubsetMatchingMode subsetMatchingCriteria);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string phrase, valuetype System.Speech.Recognition.SubsetMatchingMode subsetMatchingCriteria) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.String,System.Speech.Recognition.SubsetMatchingMode)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (phrase As String, subsetMatchingCriteria As SubsetMatchingMode)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : string * System.Speech.Recognition.SubsetMatchingMode -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder (phrase, subsetMatchingCriteria)" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::String ^ phrase, System::Speech::Recognition::SubsetMatchingMode subsetMatchingCriteria);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="phrase" Type="System.String" />
<Parameter Name="subsetMatchingCriteria" Type="System.Speech.Recognition.SubsetMatchingMode" />
</Parameters>
<Docs>
<param name="phrase">The sequence of words.</param>
<param name="subsetMatchingCriteria">The matching mode the speech recognition grammar uses to recognize the phrase.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class for a subset of a sequence of words.</summary>
<remarks>
<format type="text/markdown"><.
## Examples
The following example creates a speech recognition grammar for each <xref:System.Speech.Recognition.SubsetMatchingMode> value and a grammar for choosing between the matching mode grammars. If the value of `phrase` is "one two three four five six seven", then the Subsequence grammar recognizes the input "two three four", but not the input "one three five". However, the Ordered Subset grammar recognizes both of these inputs.
```csharp
private static IEnumerable<Grammar>
CreateMatchingModeGrammars(string phrase)
{
List<Grammar> grammars = new List<Grammar>(5);
Choices modeChoice = new Choices();
Type enumType = typeof(SubsetMatchingMode);
foreach (SubsetMatchingMode mode in Enum.GetValues(enumType))
{
string modeName = Enum.GetName(enumType, mode);
modeName = BreakAtCaps(modeName);
GrammarBuilder builder = new GrammarBuilder(phrase, mode);
Grammar modeGrammar = new Grammar(builder);
modeGrammar.Name = modeName;
modeGrammar.Enabled = false;
grammars.Add(modeGrammar);
modeChoice.Add(modeName);
}
Grammar choiceGrammar = new Grammar(modeChoice);
choiceGrammar.Name = "choice";
grammars.Add(choiceGrammar);
return grammars;
}
// Insert spaces preceding each uppercase letter in a string.
private static string BreakAtCaps(string item)
{
if (item == null || item.Length == 0)
{
return item;
}
StringBuilder sb = new StringBuilder(item[0].ToString());
for (int i = 1; i < item.Length; i++)
{
char c = item[i];
if (char.IsUpper(c))
{
sb.Append(" ");
}
sb.Append(c);
}
return sb.ToString();
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.SubsetMatchingMode" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (System.Speech.Recognition.GrammarBuilder builder, int minRepeat, int maxRepeat);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.GrammarBuilder builder, int32 minRepeat, int32 maxRepeat) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.Speech.Recognition.GrammarBuilder,System.Int32,System.Int32)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (builder As GrammarBuilder, minRepeat As Integer, maxRepeat As Integer)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.GrammarBuilder * int * int -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder (builder, minRepeat, maxRepeat)" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::Speech::Recognition::GrammarBuilder ^ builder, int minRepeat, int maxRepeat);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="builder" Type="System.Speech.Recognition.GrammarBuilder" />
<Parameter Name="minRepeat" Type="System.Int32" />
<Parameter Name="maxRepeat" Type="System.Int32" />
</Parameters>
<Docs>
<param name="builder">The repeated element.</param>
<param name="minRepeat">The minimum number of times that input matching the element defined by <paramref name="builder" /> must occur to constitute a match.</param>
<param name="maxRepeat">The maximum number of times that input matching the element defined by <paramref name="builder" /> can occur to constitute a match.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class from a repeated element.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
If the value of `minRepeat` is 0, then the new <xref:System.Speech.Recognition.GrammarBuilder> represents an optional element.
The value of `minRepeat` must be greater than or equal to 0 and less than or equal to the value of `maxRepeat`.
> [!IMPORTANT]
> When you specify repeats for <xref:System.Speech.Recognition.GrammarBuilder> objects that contain <xref:System.Speech.Recognition.SemanticResultValue> or <xref:System.Speech.Recognition.SemanticResultKey> instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the <xref:System.Speech.Recognition.SemanticValue.Value%2A> property of a <xref:System.Speech.Recognition.SemanticValue> object. The speech recognizer can throw an exception if it encounters these circumstances.
## Examples
The following example creates a speech recognition grammar for ordering a pizza. It starts with an optional, opening phrase, followed by one to four toppings, and closes with the word "pizza".
```csharp
private static Grammar CreatePizzaGrammar()
{
// Create a Choices object from a string array of alternative toppings.
Choices toppings = new Choices(new string[] {
"cheese", "mushroom", "tomato", "onion",
"anchovy", "chicken", "pepperoni"});
// Create a GrammarBuilder and append the Choices object.
GrammarBuilder andToppings = new GrammarBuilder("and", 0, 1);
andToppings.Append(toppings);
// Construct the phrase.
GrammarBuilder gb = new GrammarBuilder("I would like a", 0, 1);
gb.Append(toppings);
gb.Append(new GrammarBuilder(andToppings, 0, 3));
gb.Append("pizza");
// Create the Grammar from the GrammarBuilder.
Grammar grammar = new Grammar(gb);
grammar.Name = "Pizza Order";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public GrammarBuilder (string phrase, int minRepeat, int maxRepeat);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string phrase, int32 minRepeat, int32 maxRepeat) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.#ctor(System.String,System.Int32,System.Int32)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (phrase As String, minRepeat As Integer, maxRepeat As Integer)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.GrammarBuilder : string * int * int -> System.Speech.Recognition.GrammarBuilder" Usage="new System.Speech.Recognition.GrammarBuilder (phrase, minRepeat, maxRepeat)" />
<MemberSignature Language="C++ CLI" Value="public:
 GrammarBuilder(System::String ^ phrase, int minRepeat, int maxRepeat);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="phrase" Type="System.String" />
<Parameter Name="minRepeat" Type="System.Int32" />
<Parameter Name="maxRepeat" Type="System.Int32" />
</Parameters>
<Docs>
<param name="phrase">The repeated sequence of words.</param>
<param name="minRepeat">The minimum number of times that input matching the phrase must occur to constitute a match.</param>
<param name="maxRepeat">The maximum number of times that input matching the phrase can occur to constitute a match.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.GrammarBuilder" /> class from the sequence of words in a <see cref="T:System.String" /> and specifies how many times the <see cref="T:System.String" /> can be repeated.</summary>
<remarks>
<format type="text/markdown"><.
## Examples
The following example creates a speech recognition grammar for ordering a pizza. It starts with an optional, opening phrase, followed by one to four toppings, and closes with the word "pizza".
```csharp
private static Grammar CreatePizzaGrammar()
{
// Create a Choices object with alternatives for toppings.
Choices toppings = new Choices(new string[] {
"cheese", "mushroom", "tomato", "onion",
"anchovy", "chicken", "pepperoni"});
// Create a GrammarBuilder and append the Choices object.
GrammarBuilder andToppings = new GrammarBuilder("and", 0, 1);
andToppings.Append(toppings);
// Construct the phrase.
GrammarBuilder gb = new GrammarBuilder("I would like a", 0, 1);
gb.Append(toppings);
gb.Append(new GrammarBuilder(andToppings, 0, 3));
gb.Append("pizza");
// Create the Grammar from the GrammarBuilder.
Grammar grammar = new Grammar(gb);
grammar.Name = "Pizza Order";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.Choices" />
<altmember cref="T:System.Speech.Recognition.Grammar" />
</Docs>
</Member>
<MemberGroup MemberName="Add">
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Creates a new <see cref="T:System.Speech.Recognition.GrammarBuilder" /> that contains a sequence of two grammar elements.</summary>
<remarks>
<format type="text/markdown">< objects. For more information, see the <xref:System.Speech.Recognition.GrammarBuilder.op_Implicit%2A> and <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operators.
> [!IMPORTANT]
> The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element.
For more information about building and using speech recognition grammars, see [Speech Recognition](https://learn.microsoft.com/previous-versions/office/developer/speech-technologies/hh361633(v=office.14)).
]]></format>
</remarks>
</Docs>
</MemberGroup>
<Member MemberName="Add">
<MemberSignature Language="C#" Value="public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.Choices choices, System.Speech.Recognition.GrammarBuilder builder);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class System.Speech.Recognition.GrammarBuilder Add(class System.Speech.Recognition.Choices choices, class System.Speech.Recognition.GrammarBuilder builder) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.Add(System.Speech.Recognition.Choices,System.Speech.Recognition.GrammarBuilder)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Add (choices As Choices, builder As GrammarBuilder) As GrammarBuilder" />
<MemberSignature Language="F#" Value="static member Add : System.Speech.Recognition.Choices * System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.GrammarBuilder" Usage="System.Speech.Recognition.GrammarBuilder.Add (choices, builder)" />
<MemberSignature Language="C++ CLI" Value="public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::Choices ^ choices, System::Speech::Recognition::GrammarBuilder ^ builder);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Speech.Recognition.GrammarBuilder</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="choices" Type="System.Speech.Recognition.Choices" />
<Parameter Name="builder" Type="System.Speech.Recognition.GrammarBuilder" />
</Parameters>
<Docs>
<param name="choices">The first grammar element, which represents a set of alternatives.</param>
<param name="builder">The second grammar element.</param>
<summary>Creates a new <see cref="T:System.Speech.Recognition.GrammarBuilder" /> that contains a <see cref="T:System.Speech.Recognition.Choices" /> object followed by a <see cref="T:System.Speech.Recognition.GrammarBuilder" /> object.</summary>
<returns>A <see cref="T:System.Speech.Recognition.GrammarBuilder" /> for the sequence of the <paramref name="choices" /> element followed by the <paramref name="builder" /> element.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Speech.Recognition.GrammarBuilder> supports implicit conversions from the following classes:
- <xref:System.Speech.Recognition.Choices>
- <xref:System.Speech.Recognition.SemanticResultKey>
- <xref:System.Speech.Recognition.SemanticResultValue>
- <xref:System.String>
This method accepts the objects listed above for the `builder` parameter.
For more information, see the <xref:System.Speech.Recognition.GrammarBuilder.op_Implicit%2A> and <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operators.
> [!IMPORTANT]
> When you combine <xref:System.Speech.Recognition.Choices> and <xref:System.Speech.Recognition.GrammarBuilder> objects that contain <xref:System.Speech.Recognition.SemanticResultValue> or <xref:System.Speech.Recognition.SemanticResultKey> instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the <xref:System.Speech.Recognition.SemanticValue.Value%2A> property of a <xref:System.Speech.Recognition.SemanticValue> object. The speech recognizer can throw an exception if it encounters these circumstances. For more information about building a speech recognition grammar that contains semantic information, see [Add Semantics to a GrammarBuilder Grammar](https://msdn.microsoft.com/library/hh361581.aspx).
]]></format>
</remarks>
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.Append(System.Speech.Recognition.Choices)" />
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.op_Implicit(System.Speech.Recognition.Choices)~System.Speech.Recognition.GrammarBuilder" />
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.op_Addition(System.Speech.Recognition.Choices,System.Speech.Recognition.GrammarBuilder)" />
</Docs>
</Member>
<Member MemberName="Add">
<MemberSignature Language="C#" Value="public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.GrammarBuilder builder, System.Speech.Recognition.Choices choices);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class System.Speech.Recognition.GrammarBuilder Add(class System.Speech.Recognition.GrammarBuilder builder, class System.Speech.Recognition.Choices choices) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.Add(System.Speech.Recognition.GrammarBuilder,System.Speech.Recognition.Choices)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Add (builder As GrammarBuilder, choices As Choices) As GrammarBuilder" />
<MemberSignature Language="F#" Value="static member Add : System.Speech.Recognition.GrammarBuilder * System.Speech.Recognition.Choices -> System.Speech.Recognition.GrammarBuilder" Usage="System.Speech.Recognition.GrammarBuilder.Add (builder, choices)" />
<MemberSignature Language="C++ CLI" Value="public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::GrammarBuilder ^ builder, System::Speech::Recognition::Choices ^ choices);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Speech.Recognition.GrammarBuilder</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="builder" Type="System.Speech.Recognition.GrammarBuilder" />
<Parameter Name="choices" Type="System.Speech.Recognition.Choices" />
</Parameters>
<Docs>
<param name="builder">The first grammar element.</param>
<param name="choices">The second grammar element, which represents a set of alternatives.</param>
<summary>Creates a new <see cref="T:System.Speech.Recognition.GrammarBuilder" /> that contains a <see cref="T:System.Speech.Recognition.GrammarBuilder" /> object followed by a <see cref="T:System.Speech.Recognition.Choices" /> object.</summary>
<returns>A <see cref="T:System.Speech.Recognition.GrammarBuilder" /> for the sequence of the <paramref name="builder" /> element followed by the <paramref name="choices" /> element.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Speech.Recognition.GrammarBuilder> supports implicit conversions from the following classes:
- <xref:System.Speech.Recognition.Choices>
- <xref:System.Speech.Recognition.SemanticResultKey>
- <xref:System.Speech.Recognition.SemanticResultValue>
- <xref:System.String>
This method accepts the objects listed above for the `builder` parameter.
For more information, see the <xref:System.Speech.Recognition.GrammarBuilder.op_Implicit%2A> and <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operators.
> [!IMPORTANT]
> When you combine <xref:System.Speech.Recognition.Choices> and <xref:System.Speech.Recognition.GrammarBuilder> objects that contain <xref:System.Speech.Recognition.SemanticResultValue> or <xref:System.Speech.Recognition.SemanticResultKey> instances with other grammar elements, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the <xref:System.Speech.Recognition.SemanticValue.Value%2A> property of a <xref:System.Speech.Recognition.SemanticValue> object. The speech recognizer can throw an exception if it encounters these circumstances.
## Examples
The following example creates a speech recognition grammar that can recognize the two phrases, "Make background *color*" and "Set background to *color*", where *color* is selected from a set of colors. Various types are used to build the final grammar, such as [String](https://go.microsoft.com/fwlink/?LinkId=159733), <xref:System.Speech.Recognition.Choices>, and <xref:System.Speech.Recognition.GrammarBuilder> objects. The explicit cast operators in the calls to the <xref:System.Speech.Recognition.GrammarBuilder.Add%2A> methods are optional.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase =
GrammarBuilder.Add((GrammarBuilder)"Make background", colorChoice);
GrammarBuilder setPhrase =
GrammarBuilder.Add("Set background to", (GrammarBuilder)colorChoice);
// Create a Choices for the two alternative phrases, convert the Choices
// to a GrammarBuilder, and construct the grammar from the result.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);
Grammar grammar = new Grammar(bothPhrases);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.Append(System.Speech.Recognition.Choices)" />
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.op_Implicit(System.Speech.Recognition.Choices)~System.Speech.Recognition.GrammarBuilder" />
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.op_Addition(System.Speech.Recognition.Choices,System.Speech.Recognition.GrammarBuilder)" />
</Docs>
</Member>
<Member MemberName="Add">
<MemberSignature Language="C#" Value="public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.GrammarBuilder builder1, System.Speech.Recognition.GrammarBuilder builder2);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class System.Speech.Recognition.GrammarBuilder Add(class System.Speech.Recognition.GrammarBuilder builder1, class System.Speech.Recognition.GrammarBuilder builder2) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.Add(System.Speech.Recognition.GrammarBuilder,System.Speech.Recognition.GrammarBuilder)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Add (builder1 As GrammarBuilder, builder2 As GrammarBuilder) As GrammarBuilder" />
<MemberSignature Language="F#" Value="static member Add : System.Speech.Recognition.GrammarBuilder * System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.GrammarBuilder" Usage="System.Speech.Recognition.GrammarBuilder.Add (builder1, builder2)" />
<MemberSignature Language="C++ CLI" Value="public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::GrammarBuilder ^ builder1, System::Speech::Recognition::GrammarBuilder ^ builder2);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Speech.Recognition.GrammarBuilder</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="builder1" Type="System.Speech.Recognition.GrammarBuilder" />
<Parameter Name="builder2" Type="System.Speech.Recognition.GrammarBuilder" />
</Parameters>
<Docs>
<param name="builder1">The first grammar element.</param>
<param name="builder2">The second grammar element.</param>
<summary>Creates a new <see cref="T:System.Speech.Recognition.GrammarBuilder" /> that contains a sequence of two <see cref="T:System.Speech.Recognition.GrammarBuilder" /> objects.</summary>
<returns>A <see cref="T:System.Speech.Recognition.GrammarBuilder" /> for the sequence of the <paramref name="builder1" /> element followed by the <paramref name="builder2" /> element.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
<xref:System.Speech.Recognition.GrammarBuilder> supports implicit conversions from the following classes:
- <xref:System.Speech.Recognition.Choices>
- <xref:System.Speech.Recognition.SemanticResultKey>
- <xref:System.Speech.Recognition.SemanticResultValue>
- <xref:System.String>
This method accepts the objects listed above for the `builder1` or `builder2` parameter.
For more information, see the <xref:System.Speech.Recognition.GrammarBuilder.op_Implicit%2A> and <xref:System.Speech.Recognition.GrammarBuilder.op_Addition%2A> operators.
> [!IMPORTANT]
> When you combine <xref:System.Speech.Recognition.Choices> and <xref:System.Speech.Recognition.GrammarBuilder> objects that contain <xref:System.Speech.Recognition.SemanticResultValue> or <xref:System.Speech.Recognition.SemanticResultKey> instances with other grammar elements, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the <xref:System.Speech.Recognition.SemanticValue.Value%2A> property of a <xref:System.Speech.Recognition.SemanticValue> object. The speech recognizer can throw an exception if it encounters these circumstances.
## Examples
The following example creates a speech recognition grammar that can recognize the two phrases, "Make background *color*" and "Set background to *color*", where *color* is selected from a set of colors. Various types are used to build the final grammar, such as [String](https://go.microsoft.com/fwlink/?LinkId=159733), <xref:System.Speech.Recognition.Choices>, and <xref:System.Speech.Recognition.GrammarBuilder> objects. The explicit cast operators in the calls to the <xref:System.Speech.Recognition.GrammarBuilder.Add%2A> methods are optional.
```csharp
private Grammar CreateColorGrammar()
{
// Create a set of color choices.
Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
// Create grammar builders for the two versions of the phrase.
GrammarBuilder makePhrase =
GrammarBuilder.Add((GrammarBuilder)"Make background", colorChoice);
GrammarBuilder setPhrase =
GrammarBuilder.Add("Set background to", (GrammarBuilder)colorChoice);
// Create a Choices for the two alternative phrases, convert the Choices
// to a GrammarBuilder, and construct the grammar from the result.
Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);
Grammar grammar = new Grammar(bothPhrases);
grammar.Name = "backgroundColor";
return grammar;
}
```
]]></format>
</remarks>
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.Append(System.Speech.Recognition.Choices)" />
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.op_Implicit(System.Speech.Recognition.Choices)~System.Speech.Recognition.GrammarBuilder" />
<altmember cref="M:System.Speech.Recognition.GrammarBuilder.op_Addition(System.Speech.Recognition.Choices,System.Speech.Recognition.GrammarBuilder)" />
</Docs>
</Member>
<Member MemberName="Add">
<MemberSignature Language="C#" Value="public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.GrammarBuilder builder, string phrase);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class System.Speech.Recognition.GrammarBuilder Add(class System.Speech.Recognition.GrammarBuilder builder, string phrase) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.GrammarBuilder.Add(System.Speech.Recognition.GrammarBuilder,System.String)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Add (builder As GrammarBuilder, phrase As String) As GrammarBuilder" />
<MemberSignature Language="F#" Value="static member Add : System.Speech.Recognition.GrammarBuilder * string -> System.Speech.Recognition.GrammarBuilder" Usage="System.Speech.Recognition.GrammarBuilder.Add (builder, phrase)" />
<MemberSignature Language="C++ CLI" Value="public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::GrammarBuilder ^ builder, System::String ^ phrase);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Speech.Recognition.GrammarBuilder</ReturnType>
</ReturnValue>
<Parameters>