sphinx4 only recognize custom words

792 views Asked by At

I am developing a program using Sphinx4 in java.

I want to reduce the acoustic model and grammar to be very simple and concise that fits my use.

My use is just understanding couple words, recognizing word by word.

For example, the words would be {man, bye, good, yo} and when I say "man", I want the program to immediately recognize it as "man";

The program shouldn't wait for any other words once the program recognizes a word.

Could anyone guide me to a doc or an example/demo that I could reference to create such a thing?

Here is the code I have written so far.

private static void recognizeWord(LiveSpeechRecognizer recognizer) {
    String[] words = {"man", "bye", "good", "yo"};
    System.out.println("RECOGNIZING A WORD. AVAILABLE WORDS: " + Arrays.toString(words));

    recognizer.startRecognition(true);

    SpeechResult result;
    while ((result = recognizer.getResult()) != null ) {
        System.out.format("The word is: %s\n", recognizer.getResult().getResult().toString());
    }

    recognizer.stopRecognition();
}
public static void main(String[] args) throws Exception {

    Configuration configuration = new Configuration();

    // Set path to acoustic model.
    configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
    // Set path to dictionary.
    configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
    // Set language model.
    configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");

    LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);

    recognizeWord(recognizer);
}
1

There are 1 answers

0
Nikolay Shmyrev On

You can write JSGF grammar like this as described in tutorial:

#JSGF V1.0;

grammar hello;
public <greet> = man | bye | good | yo;

Put it in a folder src with a name hello.jsgf.

You can use it like this:

public static void main(String[] args) throws Exception {

    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");        
    configuration.setGrammarPath("file:src");
    configuration.setGrammarName("hello");
    configuration.setUseGrammar(true);

    LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);

    recognizer.startRecognition(true);

    SpeechResult result;
    while ((result = recognizer.getResult()) != null ) {
        System.out.format("The word is: %s\n", recognizer.getResult().getResult().toString());
    }

    recognizer.stopRecognition();
}