Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

How to Build Grammars for Speech-enabled Applications : Page 3

Speech-enabled applications require specialized grammars that clearly define the types of input they're expected to parse and understand. Find out how to build grammars by walking through the process of building a grammar for an order status retrieval system that lets callers retrieve orders by voice.


advertisement
Building Dynamic Grammars
Because most applications use data from relational databases in which the data is constantly changing, grammars may need to be dynamic as well. For instance, suppose your application requires customers to speak their names. For the application to recognize the name, all customer names would need to be part of a grammar file. Most companies maintain databases with hundreds if not thousands of customers. It would be impossible to expect the application developer to manually update a grammar file every time a new customer is added to the database. This is where a dynamic grammar comes in handy.

Because grammar files are XML-based, it is simple to create a string programmatically that contains the XML associated with each grammar. The sample application contains a class file named DynamicGrammar.cs. The class contains a public method named LoadGrammarFile (partially shown below) that accepts two string parameters. The first parameter, name, should contain a value for the grammar item. The second parameter, sql, should contain the actual SQL string used to query the database. The method returns a string containing the XML-based grammar.

public string LoadGrammarFile(string name,string sql) { StringBuilder s = new StringBuilder(); s.Append(@"<grammar xml:lang=""en-US"" " ); s.Append(@"tag-format=""semantics-ms/1.0"" "); s.Append(@"version=""1.0"" root=""" ); s.Append(name + @"Rule"" mode=""voice"" "); s.Append( @"xmlns=""http://www.w3.org/2001/06/grammar"">"); s.Append(@"<rule id=""" + name); s.Append(@"Rule"" scope=""public"">") ; s.Append("<one-of>") ; SqlDataReader dr = SqlHelper.ExecuteReader( ConfigurationSettings.AppSettings [ "ConnectionStr" ], System.Data.CommandType.Text, sql ); while ( dr.Read() ) { string GrammarItem; GrammarItem = dr.GetString( 0 ); s.Append("<item>") ; s.Append("<item>") ; s.Append(GrammarItem) ; s.Append("</item>") ; s.Append("<tag>$." + name + @" = """ ); s.Append(GrammarItem + @"""</tag>") ; s.Append("</item>" ); } s.Append("</one-of>") ; s.Append("</rule>") ; s.Append("</grammar>") ; if ( !( dr.IsClosed ) ) { dr.Close(); } dr = null; return s.ToString(); }

You can assign the resulting XML-based grammar string to the InlineGrammar property for a specific QA control. The InlineGrammar property value can be either a URI (Universal Resource Identifier) or a string value. In the sample application, the OnInit method for the Default web page contains the InlineGrammar property set code:

Microsoft.Speech.Web.UI.Grammar gram = new Grammar(); gram.InlineGrammar = Convert.ToString(Session["ContactGrammar"]); AskContactNameQA.Reco.Grammars.Add(gram);

The preceding code starts by creating a new grammar item and then uses a session variable to assign the InlineGrammar property value. The application uses a Session variable in an effort to improve application scalability. Rather than calling the LoadDynamicGrammar method each time the Web page is initialized, storing the value in a Session variable means the page needs to call the method only when the application is first initialized. So, the code that calls the LoadDynamicGrammar method is located in the Session_Start method of the Global.asax file. The contents of the method are as follows:



protected void Session_Start(Object sender, EventArgs e) { CDynamicGrammar _DynamicGrammar; //Build the dynamic grammar string listing all //contact names in the Northwind database. Then //store this grammar string in a session variable _DynamicGrammar = new CDynamicGrammar(); string s = _DynamicGrammar.LoadGrammarFile( "ContactName", "SELECT DISTINCT ContactName FROM Customers"); Session["ContactGrammar"] = s;

 
Figure 3. Speech Debugging Console: You use this console to debug dynamic grammars and user input at runtime. The figure shows the results after entering the name "howard snyder" in the "Input" textbox in the sample application.
_DynamicGrammar = null; }
Because dynamic grammars are not built with the grammar editor, they cannot be tested at design time, like the static grammar test dialog shown earlier. Instead, you'll need to test these grammars while executing the application. When you start a speech-based application using Visual Studio, it executes the Speech Debugging Console automatically (see Figure 3). From there, you can enter input using text or audio. As the application collects user input, the output tab displays the results collected from the recognition engine, letting you see the resulting SML used to value semantic items.

Grammars are a critical component of speech-based applications. If they do not contain all the valid potential words or phrases that a caller may speak, then the application will not be successful in meeting the callers' needs. As you've seen in this article, you can build grammars manually using the GUI tool provided with the built-in Grammar Editor, or dynamically, using code. The most effective way to ensure that your grammar meets a caller's needs is through user testing and review. Even applications that have been successfully tested and deployed need to be reviewed periodically to ensure that their grammar files are effectively designed.



Sara Morgan Rea is a 2007 Microsoft MVP for Office Communications Server. Her first book, Building Intelligent .NET Applications, was published in 2005. In addition to co-authoring several Microsoft Training Kits, she recently published Programming Microsoft Robotics Studio. She currently works as a robotic software engineer at CoroWare.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date