Change in gram file and reloading in sphinx

2019-08-31 05:46发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-08-31 05:50

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

查看更多
祖国的老花朵
3楼-- · 2019-08-31 06:00

If you want to change in your gram file then you need to add them in dictionary file acoustic model.

  • Create a txt file “words.txt”, Write all the names of cities and states in it and save.
  • Open this link : http://www.speech.cs.cmu.edu/tools/lmtool.html
  • On that page, go to “Sentence corpus file:” section, Browse to “words.txt” file and click “Compile Knowledge Base”.
  • On next page, Click on “Dictionary” link and save that .DIC file.
  • Extract WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar file.
  • Go to edu\cmu\sphinx\model\acoustic\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz\dict folder.
  • Open “cmudict.0.6d” file in that folder
  • Copy data from .DIC file, you have downloaded in PART ONE, paste it in “cmudict.0.6d” file and save. Zip the extracted hierarchy back as it was and Zip file named should be same as JAR file.
查看更多
登录 后发表回答