Create Microsoft Speech Compatible Grammar Files

⌘K
  1. Home
  2. Programmable Voice
  3. Speech Recognition
  4. Create Microsoft Speech Compatible Grammar Files

Create Microsoft Speech Compatible Grammar Files

The Voice Elements Developer API allows you to quickly and easily develop Telephony Applications that use Speech Recognition. In order to use Microsoft Speech Platform with the Voice Elements Developer API, you must first create grammar files that are compatible with the Microsoft Speech Platform.

How to setup Microsoft Speech SDK

Below are the steps so that you can develop your own Microsoft Compatible Grammar Files:

  1. Download and Install the Microsoft Speech SDK.
  2. Open up Visual Studio, and add a reference to C:\Program Files\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll
  3. Follow the instructions below to create grammar files

How to create a simple grammar file

When using Speech Recognition, you need to define the possible values that the Speech Recognition Engine will be able to detect. These values are stored in a specialized XML file that Microsoft’s Speech Platform uses.

You can use Microsoft Speech Platform SDK to generate these grammar files programmatically. The code example below shows how to create a grammar file that recognizes 4 colors.

// First create an instances of the Choices class -- This is part of the Microsoft.Speech.Recognition namespace
Microsoft.Speech.Recognition.Choices choices = new Microsoft.Speech.Recognition.Choices();
 
// Next, add the different words that you would like to be recognized.
choices.Add("Red");
choices.Add("Blue");
choices.Add("Green");
choices.Add("Yellow");
 
// The grammar builder class helps to dynamically create grammars
GrammarBuilder gb = new GrammarBuilder();
 
// Add all of the choices to the grammar builder
gb.Append(choices);
 
// Next you will want to create an instances of an Srgs Document
Microsoft.Speech.Recognition.SrgsGrammar.SrgsDocument doc = new Microsoft.Speech.Recognition.SrgsGrammar.SrgsDocument(gb);
 
// The SrgsDocument writes to an XML Writer -- By using this static method, you can specify which file you would like to create:
System.Xml.XmlWriter xWriter = System.Xml.XmlWriter.Create(@"C:\Test\Speech\mygrammar.xml");
 
// Write and close the document
doc.WriteSrgs(xWriter);
xWriter.Close();
 
// You can optionally compile your grammar file -- this is recommended if your files are static
// First you will create a reference to a new Filestream
FileStream compiledFile = new FileStream(@"C:\Test\Speech\mycompiledgrammar.cfg", FileMode.OpenOrCreate);
 
// Then you will need to call the Compile method
Microsoft.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile(@"C:\Test\Speech\mygrammar.xml", compiledFile);
compiledFile.Close();

How to create a grammar file that returns the same result for multiple words

Often, you will want to create grammar files that recognize multiple results but return a different word. For example:

Perhaps you are building a speech recognition application that should recognize “Technical Support”, “Support”, or “Help” as the same word.

Alternatively, you may prefer to return a unique ID, instead of someone’s name. For example, “Joe Smith” should return employee number “117”.

Using Choices for Words in Common

Below is a code example that shows how to create a grammar file that allows you to say common words for departments, but return back the correct department to the application:

            
Choices allChoices = new Choices();
 
// Create a list of the words that you would like to be recognized
Choices helpChoices = new Choices(new string[] { "Technical Support", "Support", "Help" });
Choices accountingChoices = new Choices(new string[] { "Accounting", "Bookkeeping", "Bean Counters" });
Choices salesChoices = new Choices(new string[] { "Sales", "Sales and Marketing", "Marketing" });
 
// Now tie these choices with the key that you would like to be returned
SemanticResultValue semanticHelpValue = new SemanticResultValue(helpChoices, "Technical Support");
SemanticResultValue semanticAccountingChoices = new SemanticResultValue(accountingChoices, "Accounting");
SemanticResultValue semanticSalesChoices = new SemanticResultValue(salesChoices, "Sales");
 
// Add each of the Semantic Result Values
allChoices.Add(semanticHelpValue);
allChoices.Add(semanticAccountingChoices);
allChoices.Add(semanticSalesChoices);
 
SemanticResultKey key = new SemanticResultKey("Lookup", allChoices);
 
// The grammar builder class helps to dynamically create grammars
GrammarBuilder gb = new GrammarBuilder(key);
 
// Next you will want to create an instances of an Srgs Document
Microsoft.Speech.Recognition.SrgsGrammar.SrgsDocument doc = new Microsoft.Speech.Recognition.SrgsGrammar.SrgsDocument(gb);
 
// The SrgsDocument writes to an XML Writer -- By using this static method, you can specify which file you would like to create:
System.Xml.XmlWriter xWriter = System.Xml.XmlWriter.Create(@"C:\Test\Speech\GrammarLookup.xml");
 
// Write and close the document
doc.WriteSrgs(xWriter);
xWriter.Close();
 
// You can optionally compile your grammar file -- this is recommended if your files are static
// First you will create a reference to a new Filestream
FileStream compiledFile = new FileStream(@"C:\Test\Speech\CompiledGrammarLookup.cfg", FileMode.OpenOrCreate);
 
// Then you will need to call the Compile method
Microsoft.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile(@"C:\Test\Speech\GrammarLookup.xml", compiledFile);
compiledFile.Close();
Was this article helpful to you? No Yes 15

How can we help?