Export (0) Print
Expand All

GrammarBuilder Constructor (String, SubsetMatchingMode)

Initializes a new instance of the GrammarBuilder class for a subset of a sequence of words.

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public GrammarBuilder(
	string phrase,
	SubsetMatchingMode subsetMatchingCriteria
)

Parameters

phrase
Type: System.String

The sequence of words.

subsetMatchingCriteria
Type: System.Speech.Recognition.SubsetMatchingMode

The matching mode the speech recognition grammar uses to recognize the phrase.

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 Using Strings to Create a GrammarBuilder Grammar.

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();
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft