0

At run time we are populating a list of files and folders. I want to add that list in the Gram file. I really have no idea how to change the gram file and load it again. I have gone through sphinx JSGF but because the lack of time I am not able to read it completely.

I am using sphinx4-1.0beta6 version.

halfer
  • 19,824
  • 17
  • 99
  • 186
tarun verma
  • 221
  • 3
  • 5
  • 14

1 Answers1

5

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:

  1. https://stackoverflow.com/a/15867226/1291122
  2. [http://cmusphinx.sourceforge.net/sphinx4/javadoc/edu/cmu/sphinx/jsgf/JSGFGrammar.html]
  3. http://cmusphinx.sourceforge.net/sphinx4/src/apps/edu/cmu/sphinx/demo/jsapi/jsgf/README.html

2

Community
  • 1
  • 1
rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • that i have already seen in the given links but i am not able to implement it. Because i am totally new to sphinx. I am using helloworld's gram file. I am modifying it at run time. When the user will say open window(that is already in the gram file). It will open the hard-coded folder and the sub-folders and files needs to be added in the gram file. I am able to modify the gram file by file operation. But it is not recognizing the new words(which are file/folder name). If you have any example please share with me. It will be less time taking. Thanks for your time. – tarun verma Jul 30 '13 at 10:08
  • Did u try my code above? it should surely work. It would be better if you go thru various demos on sphinx website and see their source file. – rahulserver Jul 30 '13 at 10:21