GrammarBuilder.AppendWildcard Method


Appends a recognition grammar element that matches any input to the current sequence of grammar elements.

 void AppendWildcard();
public void AppendWildcard ();
member this.AppendWildcard : unit -> unit
Public Sub AppendWildcard ()


The following example creates a grammar that accepts password input as a wildcard. The example attaches a Grammar.SpeechRecognized event handler to the grammar that validates the password input.

private Grammar CreatePasswordGrammar()  
  GrammarBuilder wildcardBuilder = new GrammarBuilder();  
  SemanticResultKey passwordKey =  
    new SemanticResultKey("Password", wildcardBuilder);  

  GrammarBuilder passwordBuilder =  
    new GrammarBuilder("My Password is");  

  Grammar passwordGrammar = new Grammar(passwordBuilder);  
  passwordGrammar.Name = "Password input";  

  passwordGrammar.SpeechRecognized +=  
    new EventHandler<SpeechRecognizedEventArgs>(  

  return passwordGrammar;  

// Handle the SpeechRecognized event for the password grammar.  
private void PasswordInputHandler(object sender, SpeechRecognizedEventArgs e)  
  if (e.Result == null) return;  

  RecognitionResult result = e.Result;  
  SemanticValue semantics = e.Result.Semantics;  

  if (semantics.ContainsKey("Password"))  
    RecognizedAudio passwordAudio =  
        result.Words[3], result.Words[result.Words.Count - 1]);  

    if (IsValidPassword(passwordAudio))  
      Console.WriteLine("Password accepted.");  

      // Add code to handle a valid password here.  
      Console.WriteLine("Invalid password.");  

      // Add code to handle an invalid password here.  

// Validate the password input.   
private bool IsValidPassword(RecognizedAudio passwordAudio)  
  Console.WriteLine("Validating password.");  

  // Add password validation code here.  

  return false;  


The wildcard element is added to the end of the current sequence of elements.

The wildcard element matches any spoken word. It does not match background noise or silence.

Applies to

See also