Last reviewed: 3/23/2024 10:00:11 AM

Dynamic Vocabularies

Some applications need the ability to create grammars at runtime to accommodate end-user provided information, database sources, and application processing states.

For tailoring and customizing grammars on end user systems, GrammarKit provides a grammar management class for compiling grammars within your application. For additional information about grammars, see GrammarKit.

For less complex dynamic processing needs, your application can set list choices within command and grammar vocabularies.

Choice Lists

Choice lists enable you to substitute a list of words or phrases at runtime. List placeholders are used in command and grammar vocabularies.

A list choice can optionally have a numeric identifier associated with it. This enables processing recognition results on the basis of a numeric identifier in addition to choice string.

In command vocabularies, define a list placeholder as a list name between a less than and greater than characters.


// Define the vocabulary
JChantCommandVocab _CommandVocab = _Recognizer.createCommandVocab("commands");
// Add commands to the vocabulary
_CommandVocab.addCommand("Open <filename>");
_CommandVocab.addCommand("Print <filename>");
_CommandVocab.addCommand("Close <filename>");
// Add choices to list
_CommandVocab.addChoiceToList("expenses", "filename");
_CommandVocab.addChoiceToList("status report", "filename");
// Enable the vocabulary
_CommandVocab.enable();
// Disable the vocabulary
_CommandVocab.disable();

// Define the vocabulary
NChantCommandVocab _CommandVocab = _Recognizer.CreateCommandVocab("commands");

// Add commands to the vocabulary _CommandVocab.AddCommand("Open <filename>"); _CommandVocab.AddCommand("Print <filename>"); _CommandVocab.AddCommand("Close <filename>"); // Add choices to list _CommandVocab.AddChoiceToList("expenses", "filename"); _CommandVocab.AddChoiceToList("status report", "filename"); // Enable the vocabulary _CommandVocab.Enable(); // Disable the vocabulary _CommandVocab.Disable();


// Define the vocabulary
CChantCommandVocab* _CommandVocab = _Recognizer->CreateCommandVocab(L"commands");
// Add commands to the vocabulary
_CommandVocab->AddCommand(L"Open <filename>");
_CommandVocab->AddCommand(L"Print <filename>");
_CommandVocab->AddCommand(L"Close <filename>");
// Add choices to list
_CommandVocab->AddChoiceToList(L"expenses", L"filename");
_CommandVocab->AddChoiceToList(L"status report", L"filename");
// Enable the vocabulary
_CommandVocab->Enable();
// Disable the vocabulary
_CommandVocab->Disable();

// Define the vocabulary
CChantCommandVocab* _CommandVocab = _Recognizer->CreateCommandVocab("commands");
// Add commands to the vocabulary
_CommandVocab->AddCommand("Open <filename>");
_CommandVocab->AddCommand("Print <filename>");
_CommandVocab->AddCommand("Close <filename>");
// Add choices to list
_CommandVocab->AddChoiceToList("expenses", "filename");
_CommandVocab->AddChoiceToList("status report", "filename");
// Enable the vocabulary
_CommandVocab->Enable();
// Disable the vocabulary
_CommandVocab->Disable();

var _CommandVocab: TChantCommandVocab;
// Define the vocabulary
_CommandVocab := _Recognizer.CreateCommandVocab('commands');
// Add commands to the vocabulary
_CommandVocab.AddCommand('Open <filename>');
_CommandVocab.AddCommand('Print <filename>');
_CommandVocab.AddCommand('Close <filename>');
// Add choices to list
_CommandVocab.AddChoiceToList('expenses', 'filename');
_CommandVocab.AddChoiceToList('status report', 'filename');
// Enable the vocabulary
_CommandVocab.Enable();
// Disable the vocabulary
_CommandVocab.Disable();

// Define the vocabulary
JChantCommandVocab _CommandVocab = _Recognizer.createCommandVocab("commands");
// Add commands to the vocabulary
_CommandVocab.addCommand("Open <filename>");
_CommandVocab.addCommand("Print <filename>");
_CommandVocab.addCommand("Close <filename>");
// Add choices to list
_CommandVocab.addChoiceToList("expenses", "filename");
_CommandVocab.addChoiceToList("status report", "filename");
// Enable the vocabulary
_CommandVocab.enable();
// Disable the vocabulary
_CommandVocab.disable();

@property (strong, nonatomic) SPChantCommandVocab* commandVocab;
// Define the vocabulary
_commandVocab = [_recognizer createCommandVocab:@"commands"];
// Add commands to the vocabulary
[_commandVocab addCommand:@"Open <filename>"];
[_commandVocab addCommand:@"Print <filename>"];
[_commandVocab addCommand:@"Close <filename>"];
// Add choices to list
[_commandVocab addChoiceToList:@"expenses" listName:@"filename"];
[_commandVocab addChoiceToList:@"status report" listName:@"filename"];
// Enable the vocabulary
[_commandVocab enable];
// Disable the vocabulary
[_commandVocab disable];

var _CommandVocab: SPChantCommandVocab? = nil
// Define the vocabulary
_CommandVocab = _Recognizer.createCommandVocab(name:"commands")
// Add commands to the vocabulary
_CommandVocab!.addCommand(command:"Open <filename>")
_CommandVocab!.addCommand(command:"Print <filename>")
_CommandVocab!.addCommand(command:"Close <filename>")
// Add choices to list
_CommandVocab!.addChoiceToList(choice:"expenses", listName:"filename")
_CommandVocab!.addChoiceToList(choice:"status report", listName:"filename")
// Enable the vocabulary
_CommandVocab!.enable()
// Disable the vocabulary
_CommandVocab!.disable()

' Define the vocabulary
Dim _CommandVocab As NChantCommandVocab
_CommandVocab = _Recognizer.CreateCommandVocab("commands")
' Add commands to the vocabulary
_CommandVocab.AddCommand("Open <filename>");
_CommandVocab.AddCommand("Print <filename>");
_CommandVocab.AddCommand("Close <filename>");
' Add choices to list
_CommandVocab.AddChoiceToList("expenses", "filename")
_CommandVocab.AddChoiceToList("status report", "filename")
' Enable the vocabulary
_CommandVocab.Enable()
' Disable the vocabulary
_CommandVocab.Disable()

In grammar vocabularies, define a list placeholder using the supported grammar syntax. See the GrammarKit Grammar Syntax Quick Reference.

Grammar SyntaxList Declaration
SAPI 5 XML Grammar

<grammar langid="409"> 
	<rule toplevel="active" name="commands">
		<l propname="commandprop">
			<p >Open <ruleref name="filename"/></p>
			<p >Print <ruleref name="filename"/></p>
			<p >Close <ruleref name="filename"/></p>
		</l>  
	</rule>
	<rule name="filename" dynamic="True">
		<p>placeholder</p>
	</rule>
W3C SRGS XML

<grammar xmlns="http://www.w3.org/2001/06/grammar"
  xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
  xml:lang="En" version="1.0" mode="voice" root="commands" tag-format="semantics/1.0-literals">
	<rule id="commands">
		<one-of>
			<item>Open <ruleref uri="#filename"/></item> 
			<item>Print <ruleref uri="#filename"/></item> 
			<item>Close <ruleref uri="#filename"/></item> 
		</one-of>
	</rule>
	<rule id="filename" sapi:dynamic="true">
		<item>placeholder</item> 
	</rule>
</grammar>

// Not supported on Android
    

// Define the vocabulary
NChantGrammarVocab _GrammarVocab = _Recognizer.CreateGrammarVocab("mygrammar.xml");
// Add choices to list
_GrammarVocab.AddChoiceToList("expenses", "filename");
_GrammarVocab.AddChoiceToList("status report", "filename");
// Enable the vocabulary
_GrammarVocab.Enable();
// Disable the vocabulary
_GrammarVocab.Disable();
    

// Define the vocabulary
CChantGrammarVocab* _GrammarVocab = _Recognizer->CreateGrammarVocab(L"mygrammar.xml");
// Add choices to list
_GrammarVocab->AddChoiceToList(L"expenses", L"filename");
_GrammarVocab->AddChoiceToList(L"status report", L"filename");
// Enable the vocabulary
_GrammarVocab->Enable();
// Disable the vocabulary
_GrammarVocab->Disable();
    

// Define the vocabulary
CChantGrammarVocab* _GrammarVocab = _Recognizer->CreateGrammarVocab("mygrammar.xml");
// Add choices to list
_GrammarVocab->AddChoiceToList("expenses", "filename");
_GrammarVocab->AddChoiceToList("status report", "filename");
// Enable the vocabulary
_GrammarVocab->Enable();
// Disable the vocabulary
_GrammarVocab->Disable();
    

var _GrammarVocab: TChantGrammarVocab;
// Define the vocabulary
_GrammarVocab := _Recognizer.CreateGrammarVocab('mygrammar.xml');
// Add choices to list
_GrammarVocab.AddChoiceToList('expenses', 'filename');
_GrammarVocab.AddChoiceToList('status report', 'filename');
// Enable the vocabulary
_GrammarVocab.Enable();
// Disable the vocabulary
_GrammarVocab.Disable();
    

// Define the vocabulary
JChantGrammarVocab _GrammarVocab = _Recognizer.createGrammarVocab(JChantSR1, "mygrammar.xml");
// Add choices to list
_GrammarVocab.addChoiceToList("expenses", "filename");
_GrammarVocab.addChoiceToList("status report", "filename");
// Enable the vocabulary
_GrammarVocab.enable();
// Disable the vocabulary
_GrammarVocab.disable();
    

// Not supported on iOS and macOS

// Not supported on iOS and macOS
    

' Define the vocabulary
Dim _GrammarVocab As NChantGrammarVocab
_GrammarVocab = _Recognizer.CreateGrammarVocab("mygrammar.xml")
' Add choices to list
_GrammarVocab.AddChoiceToList("expenses", "filename")
_GrammarVocab.AddChoiceToList("status report", "filename")
' Enable the vocabulary
_GrammarVocab.Enable()
' Disable the vocabulary
_GrammarVocab.Disable()