Search code examples
javaspeech-recognitionspeech-to-textcmusphinx

How to use sphinx dictionary as a grammar file


I am developing Java application for speech to text conversion. have used sphinx library and demo helloworld works fine. I have edited grammar file and appended more grammar in it and it works fine.

Now what I want is that it should accept all the input words which exists in real world dictionary so what should I do for that?

Shall I need to add all those words in my Grammar file by creating new rule?


Solution

  • You can directly use acoustic model and dictionary with live speech recognizer without involving in to the stuff of grammar file. As to create a grammar file with full dictionary will useless as well as time consuming. Also for decoding also it will take lots of time to scan each and every word of your grammar file then find match and then selecting the best. Simple solutions is to use acoustic modal and dictionary directly it self without specifying the grammar. You can have look on demo given by sphinx.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import edu.cmu.sphinx.api.Configuration;
    import edu.cmu.sphinx.api.SpeechResult;
    import edu.cmu.sphinx.api.StreamSpeechRecognizer;
    
    public class TranscriberDemo {       
    
        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
                    .setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
    LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
    // Start recognition process pruning previously cached data.
    recognizer.startRecognition(true);
    SpeechResult result = recognizer.getResult();
    // Pause recognition process. It can be resumed then with startRecognition(false).
    recognizer.stopRecognition();
            SpeechResult result;
            while ((result = recognizer.getResult()) != null) {
                System.out.format("Hypothesis: %s\n", result.getHypothesis());
            }
            recognizer.stopRecognition();
        }
    }