GrammarBuilder 构造函数
定义
初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class.
重载
| GrammarBuilder() |
初始化 GrammarBuilder 类的新的空实例。Initializes a new, empty instance of the GrammarBuilder class. |
| GrammarBuilder(Choices) |
从备选项的集合中初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a set of alternatives. |
| GrammarBuilder(SemanticResultKey) |
初始化语义钥中的 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a semantic key. |
| GrammarBuilder(SemanticResultValue) |
初始化语义值中的 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a semantic value. |
| GrammarBuilder(String) |
从字序列中初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a sequence of words. |
| GrammarBuilder(String, SubsetMatchingMode) |
根据字序列的子集初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class for a subset of a sequence of words. |
| GrammarBuilder(GrammarBuilder, Int32, Int32) |
从重复元素中初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a repeated element. |
| GrammarBuilder(String, Int32, Int32) |
初始化来自 GrammarBuilder 中的单词的序列的 String 类的新实例并指定 String 可以重复使用的次数。Initializes a new instance of the GrammarBuilder class from the sequence of words in a String and specifies how many times the String can be repeated. |
注解
此类的实例还可以通过隐式转换获取,也可以通过将 GrammarBuilder 对象与另一个对象组合到新的来获取 GrammarBuilder 。Instances of this class can also be obtained by implicit conversions from other classes or by combining a GrammarBuilder object with a second object to from a new GrammarBuilder. 有关详细信息,请参阅 Implicit 和 Addition 方法。For more information, see the Implicit and Addition methods.
若要向现有添加约束,请 GrammarBuilder 使用 Add 、 Append 、 AppendDictation 、 AppendRuleReference 、和 AppendWildcard 方法以及 Addition 运算符。To add constraints to an existing GrammarBuilder, use the Add, Append, AppendDictation, AppendRuleReference, and AppendWildcard methods, and the Addition operator.
重要
当使用的语音识别语法包含具有相同键名或多个可重复修改同一语义元素的值的语义元素时,语音识别器会引发异常。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.
GrammarBuilder()
初始化 GrammarBuilder 类的新的空实例。Initializes a new, empty instance of the GrammarBuilder class.
public:
GrammarBuilder();
public GrammarBuilder ();
Public Sub New ()
示例
下面的示例使用 GrammarBuilder 和 Choices 对象来构造语法,该语法可识别两个短语中的任意一个: "使背景 colorChoice" 或 "将背景设置为 colorChoice"。The following example uses GrammarBuilder and Choices objects to construct a grammar that can recognize either of the two phrases, "Make background colorChoice" or "Set background to colorChoice".
该示例使用 Choices 对象为对象数组中的 colorChoice 创建可接受值的列表 String 。The example uses a Choices object to create a list of acceptable values for colorChoice from an array of String objects. Choices对象类似于 one-of SRGS 规范中的元素,并包含一组替代短语,其中的任何一项都可以在口述时识别。A 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. 该示例还使用 Choices 对象将两个对象的数组分组为 GrammarBuilder 结果语法可以识别的一对替代短语。The example also uses a Choices object to group an array of two GrammarBuilder objects into a pair of alternative phrases that the resultant grammar can recognize. 替换字词或短语是大多数语法的组件, Choices 对象为用构造的语法提供此功能 GrammarBuilder 。Alternate words or phrases are a component of most grammars, and the Choices object provides this functionality for grammars constructed with GrammarBuilder.
该示例最终从通过 Grammar 对象构造的创建对象 GrammarBuilder Choices 。The example finally creates a Grammar object from a GrammarBuilder constructed from a Choices object.
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;
}
注解
若要将规则添加到现有 GrammarBuilder 对象,请使用 Add 、、 Append AppendDictation 、 AppendRuleReference 、和 AppendWildcard 方法以及 Addition 运算符。To add rules to an existing GrammarBuilder object, use the Add, Append, AppendDictation, AppendRuleReference, and AppendWildcard methods, and the Addition operator.
重要
当使用的语音识别语法包含具有相同键名或多个可重复修改同一语义元素的值的语义元素时,语音识别器会引发异常。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.
另请参阅
适用于
GrammarBuilder(Choices)
从备选项的集合中初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a set of alternatives.
public:
GrammarBuilder(System::Speech::Recognition::Choices ^ alternateChoices);
public GrammarBuilder (System.Speech.Recognition.Choices alternateChoices);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.Choices -> System.Speech.Recognition.GrammarBuilder
Public Sub New (alternateChoices As Choices)
参数
- alternateChoices
- Choices
其他注意事项的集合。The set of alternatives.
示例
下面的示例使用 GrammarBuilder 和 Choices 对象来构造语法,该语法可识别两个短语中的任意一个: "使背景 colorChoice" 或 "将背景设置为 colorChoice"。The following example uses GrammarBuilder and Choices objects to construct a grammar that can recognize either of the two phrases, "Make background colorChoice" or "Set background to colorChoice".
该示例使用 Choices 对象为对象数组中的 colorChoice 创建可接受值的列表 String 。The example uses a Choices object to create a list of acceptable values for colorChoice from an array of String objects. Choices对象类似于 one-of SRGS 规范中的元素,并包含一组替代短语,其中的任何一项都可以在口述时识别。A 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. 该示例还使用 Choices 对象将两个对象的数组分组为 GrammarBuilder 结果语法可以识别的一对替代短语。The example also uses a Choices object to group an array of two GrammarBuilder objects into a pair of alternative phrases that the resultant grammar can recognize. 替换字词或短语是大多数语法的组件, Choices 对象为用构造的语法提供此功能 GrammarBuilder 。Alternate words or phrases are a component of most grammars, and the Choices object provides this functionality for grammars constructed with GrammarBuilder.
该示例最终从通过 Grammar 对象构造的创建对象 GrammarBuilder Choices 。The example finally creates a Grammar object from a GrammarBuilder constructed from a Choices object.
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;
}
注解
有关生成包含替代项的语音识别语法的详细信息,请参阅 使用选项创建 GrammarBuilder 语法。For more information about building a speech recognition grammar that contains alternatives, see Using Choices to Create a GrammarBuilder Grammar.
另请参阅
适用于
GrammarBuilder(SemanticResultKey)
初始化语义钥中的 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a semantic key.
public:
GrammarBuilder(System::Speech::Recognition::SemanticResultKey ^ key);
public GrammarBuilder (System.Speech.Recognition.SemanticResultKey key);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.SemanticResultKey -> System.Speech.Recognition.GrammarBuilder
Public Sub New (key As SemanticResultKey)
参数
语义键。The semantic key.
示例
下面的示例创建一个语音识别语法,它可以识别两个短语,即 "使背景 colorChoice" 和 "将背景设置为 colorChoice",其中 colorChoice 是从一组颜色中选择的。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.
该示例使用单个, SemanticResultKey 你可以检索与用户所 SemanticValue 说的颜色关联的。The example uses a single SemanticResultKey with which you can retrieve the SemanticValue that is associated with the color spoken by the user. 例如,如果输入包含 "将背景设置为红色" 这一短语,则识别结果将包含 "#FF0000" 的语义值,你可以使用该事件的处理程序来检索这些值 SpeechRecognized 。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.
该示例使用 String 、 Choices 、 SemanticResultKey 、 SemanticResultValue 和 GrammarBuilder 对象来生成全部包含在最后一个对象中的约束 GrammarBuilder bothPhrases 。The example uses String, Choices, SemanticResultKey, SemanticResultValue, and GrammarBuilder objects to build the constraints that are all contained in the last GrammarBuilder object, bothPhrases. 最后,该示例 Grammar 从已完成的中构造一个对象 GrammarBuilder 。Finally, the example constructs a Grammar object from the completed GrammarBuilder.
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;
}
注解
GrammarBuilder从对象创建实例时,可以 SemanticResultValue 将语义信息添加到可在识别结果中返回的语法。When you create a GrammarBuilder instance from a SemanticResultValue object, you add semantic information to the grammar that can be returned in the recognition result. 您可以使用的 Semantics 属性(该属性 RecognizedPhrase 在事件的处理程序中提供)访问识别结果中的语义信息 SpeechRecognized 。You can access the semantic information in the recognition result using the Semantics property of RecognizedPhrase, which is available in the handler for the SpeechRecognized event. 如果 GrammarBuilder 定义了 SemanticResultKey ,则可以使用它来检索与密钥关联的识别结果中的语义信息。If the GrammarBuilder defines a SemanticResultKey, this can be used to retrieve the semantic information in a recognition result that is associated with the key. 请参阅的示例 Append(SemanticResultKey) ,并查看 SemanticResultValue 和 SemanticResultKey 。See the example for Append(SemanticResultKey), and also see SemanticResultValue and SemanticResultKey.
重要
构造 GrammarBuilder 包含或实例的对象 SemanticResultValue 时 SemanticResultKey ,请确保避免创建具有相同键名或多个可重复修改对象属性的语义元素的重复语义元素 Value SemanticValue 。When you construct GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the Value property of a SemanticValue object. 语音识别器在遇到这些情况时可能会引发异常。The speech recognizer can throw an exception if it encounters these circumstances.
另请参阅
适用于
GrammarBuilder(SemanticResultValue)
初始化语义值中的 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a semantic value.
public:
GrammarBuilder(System::Speech::Recognition::SemanticResultValue ^ value);
public GrammarBuilder (System.Speech.Recognition.SemanticResultValue value);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.SemanticResultValue -> System.Speech.Recognition.GrammarBuilder
Public Sub New (value As SemanticResultValue)
参数
- value
- SemanticResultValue
语义值或名称/值对。The semantic value or name/value pair.
示例
下面的示例创建一个语音识别语法,它可以识别两个短语,即 "使背景 colorChoice" 和 "将背景设置为 colorChoice",其中 colorChoice 是从一组颜色中选择的。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.
该示例使用单个, SemanticResultKey 你可以检索与用户所 SemanticValue 说的颜色关联的。The example uses a single SemanticResultKey with which you can retrieve the SemanticValue that is associated with the color spoken by the user. 例如,如果输入包含 "将背景设置为红色" 这一短语,则识别结果将包含 "#FF0000" 的语义值,你可以使用该事件的处理程序来检索这些值 SpeechRecognized 。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.
该示例使用 String 、 Choices 、 SemanticResultKey 、 SemanticResultValue 和 GrammarBuilder 对象来生成全部包含在最后一个对象中的约束 GrammarBuilder bothPhrases 。The example uses String, Choices, SemanticResultKey, SemanticResultValue, and GrammarBuilder objects to build the constraints that are all contained in the last GrammarBuilder object, bothPhrases. 最后,该示例 Grammar 从已完成的中构造一个对象 GrammarBuilder 。Finally, the example constructs a Grammar object from the completed GrammarBuilder.
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;
}
注解
GrammarBuilder从对象创建实例时,可以 SemanticResultValue 将语义信息添加到可在识别结果中返回的语法。When you create a GrammarBuilder instance from a SemanticResultValue object, you add semantic information to the grammar that can be returned in the recognition result. 您可以使用的 Semantics 属性(该属性 RecognizedPhrase 在事件的处理程序中提供)访问识别结果中的语义信息 SpeechRecognized 。You can access the semantic information in the recognition result using the Semantics property of RecognizedPhrase, which is available in the handler for the SpeechRecognized event. 如果 GrammarBuilder 定义了 SemanticResultKey ,则可以使用它来检索与密钥关联的识别结果中的语义信息。If the GrammarBuilder defines a SemanticResultKey, this can be used to retrieve the semantic information in a recognition result that is associated with the key. 请参阅的示例 Append(SemanticResultKey) ,并查看 SemanticResultValue 和 SemanticResultKey 。See the example for Append(SemanticResultKey), and also see SemanticResultValue and SemanticResultKey.
重要
构造 GrammarBuilder 包含或实例的对象 SemanticResultValue 时 SemanticResultKey ,请确保避免创建具有相同键名或多个可重复修改对象属性的语义元素的重复语义元素 Value SemanticValue 。When you construct GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the Value property of a SemanticValue object. 语音识别器在遇到这些情况时可能会引发异常。The speech recognizer can throw an exception if it encounters these circumstances.
另请参阅
适用于
GrammarBuilder(String)
从字序列中初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a sequence of words.
public:
GrammarBuilder(System::String ^ phrase);
public GrammarBuilder (string phrase);
new System.Speech.Recognition.GrammarBuilder : string -> System.Speech.Recognition.GrammarBuilder
Public Sub New (phrase As String)
参数
- phrase
- String
字符序列。The sequence of words.
示例
下面的示例使用 GrammarBuilder 和 Choices 对象来构造语法,该语法可识别两个短语中的任意一个: "使背景 colorChoice" 或 "将背景设置为 colorChoice"。The following example uses GrammarBuilder and Choices objects to construct a grammar that can recognize either of the two phrases, "Make background colorChoice" or "Set background to colorChoice".
使用对象创建 colorChoice 的可接受值的列表后 Choices ,该示例将初始化两个 GrammarBuilder 对象, makePhrase 并 setPhrase 使用字符串作为参数。After creating a list of acceptable values for colorChoice using a Choices object, the example initializes two GrammarBuilder objects, makePhrase and setPhrase, using a string as an argument.
该示例最终 Grammar 从 Choices 对象强制转换为对象创建对象 GrammarBuilder 。The example finally creates a Grammar object from a Choices object cast to a GrammarBuilder object.
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;
}
注解
此短语表示语音识别语法可识别的确切口述短语。The phrase represents an exact spoken phrase that the speech recognition grammar can recognize. 有关生成包含字符串的语音识别语法的详细信息,请参阅 使用字符串创建 GrammarBuilder 语法。For more information about building a speech recognition grammar that contains strings, see Using Strings to Create a GrammarBuilder Grammar.
另请参阅
适用于
GrammarBuilder(String, SubsetMatchingMode)
根据字序列的子集初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class for a subset of a sequence of words.
public:
GrammarBuilder(System::String ^ phrase, System::Speech::Recognition::SubsetMatchingMode subsetMatchingCriteria);
public GrammarBuilder (string phrase, System.Speech.Recognition.SubsetMatchingMode subsetMatchingCriteria);
new System.Speech.Recognition.GrammarBuilder : string * System.Speech.Recognition.SubsetMatchingMode -> System.Speech.Recognition.GrammarBuilder
Public Sub New (phrase As String, subsetMatchingCriteria As SubsetMatchingMode)
参数
- phrase
- String
字符序列。The sequence of words.
- subsetMatchingCriteria
- SubsetMatchingMode
用于识别短语的语法识别语法的匹配模式。The matching mode the speech recognition grammar uses to recognize the phrase.
示例
下面的示例为每个值创建了语音识别语法 SubsetMatchingMode ,并为在匹配模式语法之间选择的语法创建了语法。The following example creates a speech recognition grammar for each SubsetMatchingMode value and a grammar for choosing between the matching mode grammars. 如果的值 phrase 为 "1 2 3 4 5 6 7",则子序列语法会识别输入 "2 3 4",但不识别输入 "1 3 5"。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.
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();
}
注解
phrase参数表示语音识别语法可以识别的短语。The phrase parameter represents the phrase that the speech recognition grammar can recognize. subsetMatchingMode参数指定可用于实现整个短语成功识别的短语的子集。The subsetMatchingMode parameter specifies a subset of the phrase that can be spoken to achieve successful recognition of the entire phrase. 您可以使用此选项创建包含长名称的条目列表的语法,而不要求用户说出整个名称来匹配项。You can use this to create a grammar with a list of entries that have long names, without requiring users to speak an entire name to match an item.
有关匹配模式的详细信息,请参阅 SubsetMatchingMode 。For more information about the matching modes, see SubsetMatchingMode. 有关生成包含字符串的语音识别语法的详细信息,请参阅 使用字符串创建 GrammarBuilder 语法。For more information about building a speech recognition grammar that contains strings, see Using Strings to Create a GrammarBuilder Grammar.
另请参阅
适用于
GrammarBuilder(GrammarBuilder, Int32, Int32)
从重复元素中初始化 GrammarBuilder 类的新实例。Initializes a new instance of the GrammarBuilder class from a repeated element.
public:
GrammarBuilder(System::Speech::Recognition::GrammarBuilder ^ builder, int minRepeat, int maxRepeat);
public GrammarBuilder (System.Speech.Recognition.GrammarBuilder builder, int minRepeat, int maxRepeat);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.GrammarBuilder * int * int -> System.Speech.Recognition.GrammarBuilder
Public Sub New (builder As GrammarBuilder, minRepeat As Integer, maxRepeat As Integer)
参数
- builder
- GrammarBuilder
被重复元素。The repeated element.
- minRepeat
- Int32
必须发生构成匹配的输入匹配的要素定义的 builder 的最小次数。The minimum number of times that input matching the element defined by builder must occur to constitute a match.
- maxRepeat
- Int32
可能发生构成匹配的输入匹配的要素定义的 builder 的最大次数。The maximum number of times that input matching the element defined by builder can occur to constitute a match.
示例
以下示例创建了用于对比萨饼进行排序的语音识别语法。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".
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;
}
注解
如果的值 minRepeat 为0,则新的 GrammarBuilder 表示一个可选元素。If the value of minRepeat is 0, then the new GrammarBuilder represents an optional element.
的值 minRepeat 必须大于或等于0且小于或等于的值 maxRepeat 。The value of minRepeat must be greater than or equal to 0 and less than or equal to the value of maxRepeat.
重要
当您为 GrammarBuilder 包含或实例的对象指定重复 SemanticResultValue 项时 SemanticResultKey ,请确保避免使用相同的键名或多个可以重复修改对象属性的语义元素来创建重复的语义元素 Value SemanticValue 。When you specify repeats for GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the Value property of a SemanticValue object. 语音识别器在遇到这些情况时可能会引发异常。The speech recognizer can throw an exception if it encounters these circumstances.
另请参阅
适用于
GrammarBuilder(String, Int32, Int32)
初始化来自 GrammarBuilder 中的单词的序列的 String 类的新实例并指定 String 可以重复使用的次数。Initializes a new instance of the GrammarBuilder class from the sequence of words in a String and specifies how many times the String can be repeated.
public:
GrammarBuilder(System::String ^ phrase, int minRepeat, int maxRepeat);
public GrammarBuilder (string phrase, int minRepeat, int maxRepeat);
new System.Speech.Recognition.GrammarBuilder : string * int * int -> System.Speech.Recognition.GrammarBuilder
Public Sub New (phrase As String, minRepeat As Integer, maxRepeat As Integer)
参数
- phrase
- String
重复的字符序列。The repeated sequence of words.
- minRepeat
- Int32
必须发生构成匹配的输入匹配的短语的最小次数。The minimum number of times that input matching the phrase must occur to constitute a match.
- maxRepeat
- Int32
可能发生构成匹配的输入匹配的短语的最大次数。The maximum number of times that input matching the phrase can occur to constitute a match.
示例
以下示例创建了用于对比萨饼进行排序的语音识别语法。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".
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;
}
注解
如果的值 minRepeat 为0,则新的 GrammarBuilder 表示一个可选元素。If the value of minRepeat is 0, then the new GrammarBuilder represents an optional element.
的值 minRepeat 必须大于或等于0且小于或等于的值 maxRepeat 。The value of minRepeat must be greater than or equal to 0 and less than or equal to the value of maxRepeat. 有关生成包含字符串的语音识别语法的详细信息,请参阅 使用字符串创建 GrammarBuilder 语法。For more information about building a speech recognition grammar that contains strings, see Using Strings to Create a GrammarBuilder Grammar.