如何在 CMUSphinx 中为 StreamSpeechRecognizer 配置语法

How to config grammar for StreamSpeechRecognizer in CMUSphinx

我有这样的语法:

#JSGF V1.0;

grammar music;

public <command> = play | pause | next | previous;

当我将此语法用于 LiveSpeechRegconizer 时,它工作正常。

现在我想在 StreamSpeechRegconizer 中使用它。在使用 StreamSpeechRecognizer 的 github 源 CMUSphinx 示例的转录器演示中,它仅显示了使用整个字典检测语音的方法。有没有办法只配置我自己的语法?

要使用语法,您需要正确创建配置对象:

Configuration configuration = new Configuration();

configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");

 // a folder in classpath or in filesystem containing the grammars
configuration.setGrammarPath("resource:/com/example/grammars");
// A grammar name corresponding to a file music.jsgf
configuration.setGrammarName("music"); 
configuration.setUseGrammar(true);

StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);