Search code examples
speech-recognitionspeech-to-textcmusphinx

How to config grammar for StreamSpeechRecognizer in CMUSphinx


I have a grammar like this :

#JSGF V1.0;

grammar music;

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

When I used this grammar for the LiveSpeechRegconizer , it works fine .

And now I want to use this in the StreamSpeechRegconizer . In the Transcriber Demo on the source CMUSphinx example on github which used the StreamSpeechRecognizer,it's only show the way to detect speech with the whole dictionary . Is there a way to config just my own grammar?


Solution

  • To use grammar you need to properly create a Configuration object:

    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);