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.

4

1 回答 1

5

以下是在运行时更改语法所涉及的步骤:

获取 JSFGgrammr 组件。JSGFDemo 构造函数展示了如何从 ConfigurationManager 中检索 JSGFGrammar 组件(在演示中称为 jsgfGrammarManager):

URL url = JSGFDemo.class.getResource("jsgf.config.xml");
        ConfigurationManager cm = new ConfigurationManager(url);
        jsgfGrammarManager = (JSGFGrammar) cm.lookup("jsgfGrammar");

使用 JSGFGrammar 组件加载新的 JSGF 语法 - 可以通过 loadJSGF 方法加载新的 JSGF 语法。可以在“loadAndRecognize”方法中找到如何完成此操作的示例。

private void loadAndRecognize(String grammarName) throws IOException, GrammarException  {
        jsgfGrammarManager.loadJSGF(grammarName);
        dumpSampleSentences(grammarName);
        recognizeAndReport();
     }

使用 RuleGrammar 添加新规则 - 可以直接从 Java 代码操作 JSGF 语法。可以为您的应用程序添加、启用和禁用规则。'loadAndRecognizeMusic' 和 'addRule' 方法演示了您的应用程序如何添加新规则。下面是 'addRule' 方法,它显示了如何将新规则添加到 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);
    }

你可以看到这些资源:

  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

于 2013-07-29T12:10:14.730 回答