# Creating a recognition grammar

A typical speech recognizer uses static and dynamic grammars to recognize speech input and to match results. Static grammars are loaded at start up and are always available. Dynamic grammars are loaded for specific recognition sessions and are associated with a particular recognition session (and therefore, a specific recogHd).

Before streaming voice data to a recognizer, the application can define a grammar list that the recognizer can load and use to recognize speech input and match results. The application uses saiInitRecognitionList, saiAddRecognitionGrammar, and saiDefineGrammarRecognizer to send the grammar list to the recognizer. This provides a dynamic grammar that the recognizer can use for the entire recognizer session.

After the application invokes saiDefineGrammarRecognizer, the speech server attempts to compile the specified grammars and returns SAIEVN_ASR_DEFINE_GRAMMAR_DONE. The completion event indicates whether or not the grammar list was successfully compiled. If compilation fails, the cause of the failure is specified in the completionCause field of the SAIEVN_ASR_DEFINE_GRAMMAR_DONE event buffer.

Grammar lists can contain grammar rules in various formats, including XML formatted text or simple URLs. After the speech server compiles a grammar list, it saves the grammar in a server-side database. The speech server uses the grammar list whenever the application requests a speech recognition task until the recognizer resource is released with saiReleaseRecognizer. Each time the application invokes saiStartRecognizer, it specifies a pre-loaded grammar for the server to use to carry out the specified recognition task.

Note: Recognition grammar databases are only valid for a specific recognition session. Once the session is closed, the grammar list is not valid for other recognition sessions.

The following example shows how an application creates a recognition grammar:

DWORD grmListSize = 0x1000;
void *grmList = calloc( 1, grmListSize );
char dtmf_digit_grammar[] =
"<?xml version=\"1.0\"?>\n"
"<grammar xmlns=\"http://www.w3.org/2001/06/grammar\"\n"
" xml:lang=\"en-US\" version=\"1.0\" mode=\"dtmf\"\n"
" root=\"four-digits\">\n"
"\n"
" <rule id=\"digit\">\n"
" <one-of>\n"
" <item> 1 </item>\n"
" <item> 2 </item>\n"
" <item> 3 </item>\n"
" <item> 4 </item>\n"
" <item> 5 </item>\n"
" <item> 6 </item>\n"
" <item> 7 </item>\n"
" <item> 8 </item>\n"
" <item> 9 </item>\n"
" <item> 0 </item>\n"
" </one-of>\n"
" </rule>\n"
" <rule id=\"four-digits\" scope=\"public\">\n"
" <item>\n"
" <ruleref uri=\"#digit\"/>\n"
" <ruleref uri=\"#digit\"/>\n"
" <ruleref uri=\"#digit\"/>\n"
" <ruleref uri=\"#digit\"/>\n"
" </item>\n"
" </rule>\n"
"</grammar>\n";
rc = saiInitRecognitionList( grmList, grmListSize );
if (rc == SUCCESS)
{
rc = saiAddRecognitionGrammar(gramlist,SAI_GRAMMAR_TYPE_XML,NULL,dtmf_digit_grammar);
rc = saiDefineGrammarRecognizer (recogHd, gramlist, NULL );
}