Add recognized vocabulary to HelloWorld demo with different “grammars”
Published by admin July 14th, 2006 in NotesAs it comes with the Sphinx-4 package, the HelloWorld.jar example only recognizes the following words:
( Bhiksha | Evandro | Paul | Philip | Rita | Will )
To expand this vocabulary, we need to modify a grammar file that the JSGFGrammar class imports, and then we need to rebuild the HelloWorld.jar using ant.
In the sphinx4-1.0beta/demo/sphinx/helloworld/ directory that came with the Sphinx-4 package, you should see a file called hello.gram. If you open this grammar file, you see the following code:
/**
* JSGF Grammar for Hello World example
*/
grammar hello;
public <greet> = (Good morning | Hello)
( Bhiksha | Evandro | Paul | Philip | Rita | Will );
So, this grammar file will tell the JSGFGrammar in this application to look for phrases of two words. To change what words our application can recognize, we simply add to these word groups. For instance, if we want to be able to say congratulations to all these people, we simply change the public
( Bhiksha | Evandro | Paul | Philip | Rita | Will );
Once this change is made, you may also want to mirror this change in the HelloWorld.java program so that it prints the new word possibilities to the terminal in its println statement. Then to run the application, simply change directory to the demo/helloworld/ in Terminal and type:
Your system must have ant installed for this to work, but this command finds the “build.xml” file in that directory which lets the machine know where all the necessary files are for building the HelloWorld.jar. Once the program has been built, you should be able to go to sphinx4-1.0beta/bin/ and run java -mx312m -jar HelloWorld.jar and say Congratulations Paul (or whoever).
Using grammars like this obviously provides little flexibility in what the program understands. There are instances where this could be advantageous, however, such as when we want menu navigation, a survey, etc. JSGF grammar files can be quite extensive though. Take a look at the developer’s guide to see how they can import rules from other grammar files, reference rules within rules, weight certain words above others, and other functionalities.
But, I don’t see the grammar file or the JSGFGrammar class in the HelloWorld.java file. Where do these get called?
All of this is specified in the “helloworld.config.xml” file that the application loads and the Configuration Manager takes action on. In that xml file, you will see in the section commented as “The Grammar Configuration”:
<property name=”dictionary” value=”dictionary”/>
<property name=”grammarLocation”
value=”resource:/demo.sphinx.helloworld.HelloWorld!/demo/sphinx/helloworld/”/>
<property name=”grammarName” value=”hello”/>
<property name=”logMath” value=”logMath”/>
</component>
So, this xml tells the application where to find the hello.gram file in the .jar’s resource path as well as the grammarName that was specified by the line “grammar hello;” in the grammar file.