Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Compile SRGS Grammars (Microsoft.Speech)

Compiling your XML grammar to a binary grammar file with the .cfg extension can reduce the time consumed by searches for a match, especially in grammars that require recognition of a large number of words and phrases.

The SrgsGrammarCompiler class contains three overloaded Compile() methods that compile a grammar to a binary grammar file with the .cfg extension. These methods can be used to:

  • Compile a dynamically-created grammar contained in an SrgsDocument instance, using Compile(SrgsDocument, Stream).

    The first parameter represents the grammar to compile, and the second is a FileStream object that represents the location to which the compiled grammar will be written.

  • Compile an existing XML grammar, using Compile(String, Stream).

    The first parameter represents the existing XML grammar to compile, and the second is a FileStream object that represents the location to which the compiled grammar will be written.

  • Compile an XmlReader grammar that can reside in memory or in physical file, using Compile(XmlReader, Stream).

    The first parameter represents the XmlReader instance to compile, and the second is a FileStream object that represents the location to which the compiled grammar will be written.

Compile an SrgsDocument

The first example in the following group of four examples uses the Compile(SrgsDocument, Stream) method to compile an SrgsDocument instance. The other examples include an event handler for the SpeechRecognized event and helper methods that the first example calls to perform three tasks:

  • Create an SrgsDocument instance

  • Create a grammar

  • Compile the grammar

The grammar that is created in the CreateGrammar method can be used to recognize phrases such as "I want to fly from Miami to Chicago." The two semantic values in the grammar are the departure city and the destination city. The semantic keys for these semantic values are "LeavingFrom" and "GoingTo."

Note

The SpeechRecognitionEngine instance, sre, is declared outside this method.

private void Form1_Load(object sender, EventArgs e)
{
  sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
  sre.SetInputToDefaultAudioDevice();

  // Create an SrgsDocument instance and compile it.
  SrgsDocument srgsDoc = new SrgsDocument();
  srgsDoc = CreateGrammar();
  CompileGrammar(srgsDoc, @"C:\test\AirportCodes.cfg");
  Grammar g = new Grammar(@"C:\test\AirportCodes.cfg", "flightBooker");

  // Load the grammar to the SpeechRecognitionEngine.
  sre.LoadGrammar(g);

  // Attach a handler for the SpeechRecognized event.
  sre.SpeechRecognized += 
    new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);

  // Start asynchronous recognition.
  sre.RecognizeAsync();
  Console.WriteLine("Starting asynchronous recognition...\n");

  // Keep the console window open.
  Console.ReadLine();
}

The following example is the handler for the SpeechRecognized event. This handler uses the semantic keys "LeavingFrom" and "GoingTo" as indexes into the e.Result.Semantics collection.

void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
  string recoString = e.Result.Text;
  string departCity = e.Result.Semantics["LeavingFrom"].Value.ToString();
  string arriveCity = e.Result.Semantics["GoingTo"].Value.ToString(); 
  
  MessageBox.Show(String.Format("Recognized phrase: {0}\nDeparting from: {1}\nArriving at: {2}", recoString, departCity, arriveCity));
}

The following method is a helper whose job is to create the SrgsDocument instance, which makes up the bulk of the grammar used in this example. The SrgsDocument instance contains two rules whose identifiers are "flightBooker" and "cities". The rule named "cities" recognizes city names and returns the three-letter airport code for a recognized city. The "flightBooker" rule recognizes phrases of the form "I want to fly from <rule reference> to <rule reference>," where each rule reference points to the rule named "cities". The example uses SrgsSemanticInterpretationTag objects after the SrgsRuleRef objects in the "flightBooker" rule to assign semantic keys to the recognition result from the "cities" rule. The keys identify each city as the departure city or as the arrival city.

private SrgsDocument CreateGrammar()
{
  SrgsDocument srgsDoc = new SrgsDocument();

  // Create a rule for cities, assign a semantic value to each city.
  SrgsRule cities = new SrgsRule("flightCities");
  SrgsItem chi = new SrgsItem("Chicago");
  chi.Add(new SrgsSemanticInterpretationTag("$ = \"ORD\";"));
  SrgsItem bos = new SrgsItem("Boston");
  bos.Add(new SrgsSemanticInterpretationTag("$ = \"BOS\";"));
  SrgsItem mia = new SrgsItem("Miami");
  mia.Add(new SrgsSemanticInterpretationTag("$ = \"MIA\";"));
  SrgsItem dal = new SrgsItem("Dallas");
  dal.Add(new SrgsSemanticInterpretationTag("$ = \"DFW\";"));
  SrgsOneOf airports = new SrgsOneOf(chi, bos, mia, dal);
  cities.Add(airports);
  cities.Scope = SrgsRuleScope.Private;

  // Create a rule reference to the rule for cities.
  SrgsRuleRef cityRef = new SrgsRuleRef(cities);

  // Create the root rule for the grammar.
  SrgsRule bookFlight = new SrgsRule("flightBooker");
  bookFlight.Add(new SrgsItem("I want to fly from"));
  bookFlight.Add(cityRef);
  bookFlight.Add(new SrgsSemanticInterpretationTag("$.LeavingFrom=$$;"));
  bookFlight.Add(new SrgsItem("to"));
  bookFlight.Add(cityRef);
  bookFlight.Add(new SrgsSemanticInterpretationTag("$.GoingTo=$$;"));
  bookFlight.Scope = SrgsRuleScope.Public;

  // Add the rules to the SrgsDocument object and set the root rule.
  srgsDoc.Rules.Add(cities);
  srgsDoc.Rules.Add(bookFlight);
  srgsDoc.Root = bookFlight;

  // Write the completed grammar to an XML-format SRGS grammar file.
  System.Xml.XmlWriter writer =
    System.Xml.XmlWriter.Create("c:\\test\\AirportCodes.grxml");
  srgsDoc.WriteSrgs(writer);
  writer.Close();

  return srgsDoc;
}

Note

See SrgsSemanticInterpretationTag for more information about specifying semantics for grammars authored programmatically using members of the Microsoft.Speech.Recognition.SrgsGrammar namespace.

The final example in this section is a helper method. It creates a new FileStream object, and then calls the static method Compile(SrgsDocument, Stream) to compile the grammar created in the call to the previous example, CreateGrammar.

private void CompileGrammar(SrgsDocument srgsDoc, string cfgPath)
{
  FileStream fs = new FileStream(cfgPath, FileMode.Create);
  SrgsGrammarCompiler.Compile(srgsDoc, (Stream)fs);
  fs.Close();
}

Compile an XML Grammar in an External File

The Compile(String, Stream) method can be used to compile an existing grammar file that conforms to the Speech Recognition Grammar Specification (SRGS) Version 1.0.

The next example shows the SRGS grammar that is used as input in the C# example that follows it. The grammar recognizes sentences of the form "I would like to fly from <starting city> to <destination city>." The semantic values returned by this grammar are the three-letter airport codes.

<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="flightBooker" 
tag-format="semantics-ms/1.0" version="1.0" 
xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="flightBooker" scope="public">
    <item> I want to fly from </item>
    <ruleref uri="#flightCities" />
    <tag> $.LeavingFrom=$$; </tag>
    <item> to </item>
    <ruleref uri="#flightCities" />
    <tag> $.GoingTo=$$; </tag>
  </rule>

  <rule id="flightCities" scope="private">
    <one-of>
      <item> Chicago <tag> $="ORD"; </tag></item>
      <item> Boston <tag> $="BOS"; </tag></item>
      <item> Miami <tag> $="MIA"; </tag></item>
      <item> Dallas <tag> $="DFW"; </tag></item>
    </one-of>
  </rule>

</grammar>

Note

The example above uses tag elements to add semantic information to the grammar. For more information about using tag elements, see tag Element (Microsoft.Speech) and Using the tag Element (Microsoft.Speech).

The following example compiles the XML grammar shown in the preceding example to a binary grammar file with the .cfg extension, sending the compiled version to the location specified by the fs parameter in the call to the Compile(String, Stream) method. After the binary grammar file is created, it is used to create and initialize the Grammar instance named gr. The binary grammar file is then loaded into the speech recognizer by a call to the LoadGrammar(Grammar) method.

Note

The SpeechRecognitionEngine instance, sre, is declared outside this method.

private void Form1_Load(object sender, EventArgs e)
{
  sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
  string grammarPath = @"C:\test\";
  
  FileStream fs = new FileStream(grammarPath + "AirportCodes.cfg", FileMode.Create);
  SrgsGrammarCompiler.Compile(grammarPath + "AirportCodes.grxml", (Stream)fs);
  fs.Close();
  Grammar gr = new Grammar(grammarPath + "AirportCodes.cfg", "flightBooker");
  
  sre.LoadGrammar(gr);

  sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
  sre.SetInputToDefaultAudioDevice();
  sre.RecognizeAsync(RecognizeMode.Multiple);
}

Compile an XmlReader Grammar

The third Compile(XmlReader, Stream) overloaded method takes an XmlReader object as its input and it is similar to the Compile(XmlReader, Stream) overloaded method that takes an XML file as input. The difference between these two overloaded methods is that the XmlReader can exist in memory or as an external file.

The following example shows the steps involved in using this Compile(XmlReader, Stream) overloaded method. The first step is to create a FileStream instance that identifies where the compiled binary grammar file will be written. Next, a XmlReader instance is created, using the static Create method on the XmlReader class. In this example, the XmlReader instance that is created comes from an XML grammar file named "AirportCodes.grxml." At this point, the contents of the XmlReader instance are compiled and sent to the location designated by the outStream parameter, after which the FileStream instance is closed. Finally, a Grammar instance is created from the binary grammar file with the .cfg extension, and the grammar is loaded into the speech recognizer.

Note

The SpeechRecognitionEngine instance, sre, is declared outside this method.

string grammarPath = @"C:\test\";
string xmlGrammar = grammarPath + "AirportCodes.grxml";
string cfgGrammar = grammarPath + "AirportCodes.cfg";
 
FileStream fs = new FileStream(cfgGrammar, FileMode.Create);
XmlReader reader = XmlReader.Create(xmlGrammar);

SrgsGrammarCompiler.Compile(reader, (Stream)fs);
fs.Close();
Grammar g = new Grammar(cfgGrammar, "flightBooker");
sre.LoadGrammar(g);

See Also

Reference

SrgsDocument

SrgsRuleRef

SrgsSemanticInterpretationTag

Concepts

tag Element (Microsoft.Speech)

Using the tag Element (Microsoft.Speech)

Referencing Grammar Rule Variables (Microsoft.Speech)