Note
Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.
GrammarBuilder Constructor (String, SubsetMatchingMode)
Initializes a new instance of the GrammarBuilder class from a subset of a sequence of words.
Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in Microsoft.Speech.dll)
Syntax
'Declaration
Public Sub New ( _
phrase As String, _
subsetMatchingCriteria As SubsetMatchingMode _
)
'Usage
Dim phrase As String
Dim subsetMatchingCriteria As SubsetMatchingMode
Dim instance As New GrammarBuilder(phrase, _
subsetMatchingCriteria)
public GrammarBuilder(
string phrase,
SubsetMatchingMode subsetMatchingCriteria
)
Parameters
- phrase
Type: System.String
The sequence of words.
- subsetMatchingCriteria
Type: Microsoft.Speech.Recognition.SubsetMatchingMode
The matching mode the speech recognition grammar uses to recognize the phrase.
Remarks
The phrase parameter represents the phrase that the speech recognition grammar can recognize. 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.
For more information about the matching modes, see SubsetMatchingMode. For more information about building a speech recognition grammar that contains strings, see Use a String to Create a GrammarBuilder (Microsoft.Speech).
Examples
The following example creates a speech recognition grammar for each 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.
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();
}