Here are the steps involved for changing grammar at runtime:
Get the JSGFGrammr component. the JSGFDemo constructor shows how the JSGFGrammar component (called the jsgfGrammarManager in the demo) can be retrieved from the ConfigurationManager:
URL url = JSGFDemo.class.getResource("jsgf.config.xml");
ConfigurationManager cm = new ConfigurationManager(url);
jsgfGrammarManager = (JSGFGrammar) cm.lookup("jsgfGrammar");
Use the JSGFGrammar component to load new JSGF Grammars - The new JSGF gramamrs can be loaded via the loadJSGF method. An example of how this is done can be found in the 'loadAndRecognize' method.
private void loadAndRecognize(String grammarName) throws IOException, GrammarException {
jsgfGrammarManager.loadJSGF(grammarName);
dumpSampleSentences(grammarName);
recognizeAndReport();
}
Add new rules using a RuleGrammar - a JSGF grammar can be manipulated directly from Java code. Rules can be added, enabled and disabled for your application. The methods 'loadAndRecognizeMusic' and 'addRule' demonstrate how your application can add new rules. Here's the 'addRule' method that shows how a new rule can be added to a ruleGrammar.
private void addRule(RuleGrammar ruleGrammar, String ruleName,
String jsgf) throws GrammarException {
Rule newRule = ruleGrammar.ruleForJSGF(jsgf);
ruleGrammar.setRule(ruleName, newRule, true);
ruleGrammar.setEnabled(ruleName, true);
}
You can see these resources:
- https://stackoverflow.com/a/15867226/1291122
- [http://cmusphinx.sourceforge.net/sphinx4/javadoc/edu/cmu/sphinx/jsgf/JSGFGrammar.html]
- http://cmusphinx.sourceforge.net/sphinx4/src/apps/edu/cmu/sphinx/demo/jsapi/jsgf/README.html
2