Microsoft sdk-formal
Version 1.0
Microsoft .NET Speech Homepage


Debugging Dynamic Grammars

This topic describes a simple application that demonstrates debugging a dynamic grammar. Running this application requires vs, and sdk-formal.

Create a new Speech Web Application in vs
Open vs and create a new project. In the left panel of the New Project wizard, select C# project, and in the right panel, select Speech Web Application. Change the project name from SpeechWebApplication1 to DebugDynamicGrammar and click OK.
Specify the application properties
In the left panel of the Project Wizard window, click the Application Resources link and clear the Create new prompt project check box. Click the Finish button to create the application.
Add markup to the default page
Click the HTML button on Default.aspx to view the file in HTML mode. Replace the empty form tags with this markup:
<form id=Form1 method=post runat="server">

  <speech:answercall id=AnswerCall1 runat="server">
  </speech:answercall>
  
  <speech:semanticmap id=SemanticMap runat="server">
    <speech:SemanticItem id="siState" runat="server" 
      TargetAttribute="value" TargetElement="TextBox1">
    </speech:SemanticItem>
  </speech:semanticmap>
  
  <asp:TextBox id=TextBox1 runat="server"></asp:TextBox>

  <speech:QA ID="QA1" runat="server" >
    <Prompt ID="p1" InlinePrompt="Please say the name of a nation." >
    </Prompt>
    <Reco >
      <Grammars>
        <speech:Grammar ID="G1" runat="server" Src="CreateNationGrammar.aspx">
        </speech:Grammar>
      </Grammars>
    </Reco>
    <Answers>
      <speech:Answer SemanticItem="siState" XpathTrigger="/SML/NationRule" >
      </speech:Answer>
    </Answers>
  </speech:QA>
  
</form>
The Grammar tag references the dynamic grammar CreateNationGrammar.aspx. When the QA control that contains this tag is activated, ASP.NET will load CreateNationGrammar.aspx and its code-behind module, CreateNationGrammar.aspx.cs. When the name of a nation has been recognized, the QA control will place the name in TextBox1.
Add the CreateNationGrammar page to the project
Under the Project menu, select Add New Item, and select the WebForm template. Change the file name in the Name text box from WebForm1.aspx to CreateNationGrammar.aspx and click Open. This displays the empty CreateNationGrammar.aspx form in Designer view.
Add markup to the CreateNationGrammar page
Click the HTML button on the CreateNationGrammar.aspx file to display the HTML view. Replace the entire text of the file with the following markup:
<?xml version="1.0"?>
<%@ Page language="c#" Codebehind="CreateNationGrammar.aspx.cs" AutoEventWireup="false" Inherits="DebugDynamicGrammar.CreateNationGrammar" %>

<grammar root="NationRule" xml:lang="en-US" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics-ms/1.0"> 
  <rule id="NationRule" scope="public">
    <one-of>
       <%= CreateGrammar() %>
      </one-of>
  </rule>
</grammar>
Show code-behind modules
To display the code-behind modules in the project, click the Show All Files button on the Solution Explorer toolbar.
Add code to the CreateNationGrammar page
In Solution Explorer, double-click the file CreateNationGrammar.aspx.cs. Paste the following markup into the module, beneath the Page_Load routine:
//
// <item> <tag> $.value="albania" </tag> albania </item>
//
private string OneItem(string whatever) 
{
  return("<item><tag> $.value=\"" + whatever + "\" </tag> " + whatever + " </item>\r\n");
}

protected string CreateGrammar() 
{
  // As if records in a recordset
  string grm = "\r\n";
  grm += OneItem("Albania");  
  grm += OneItem("Algeria");
  grm += OneItem("Argentina");
  grm += OneItem("Australia");
  // . . .
  grm += OneItem("Zimbabwe");
  return grm;
}
Try to run the solution
In Solution Explorer, right-click the Default.aspx node and select View in Browser. In the Debugging Console, type the name Australia. The Output pane of the Debugging Console will show results like the following:
<SML confidence="1.000" text="australia" utteranceConfidence="1.000">
  <value>Australia</value>
</SML>
But the application will hang, and the nation name will not appear in TextBox1. This suggests that there may be a problem with the dynamic grammar.
View and save the dynamic grammar
While the application is hanging, open Internet Explorer and type the address of the dynamic grammar page:
//localhost/DebugDynamicGrammar/CreateNationGrammar.aspx
This will display the content of the dynamic grammar. To view and validate the grammar in the gets, using Internet Explorer, save the file as CreateNationGrammar.grxml, and then open it in vs or double-click its icon in Windows Explorer.

In the case of this application, the problem is easy to determine: The dynamic grammar returns names in a mode with an XPath of /SML/value, but the QA control is searching for data with an XPath of /SML/NationRule. To fix the application, change the QA control's XPathTrigger property to /SML/value.

To summarize the technique for debugging dynamic grammars:












Last Updated: April 28, 2004



©2004 Microsoft Corporation. All rights reserved.  Terms of Use |Privacy Statement

Microsoft