Vidhya Sampath Vidhya Sampath -4 years ago 163
Java Question

how to get only the words recognised by sphinx

i am using live speech recogniser
it recognises some word but i need only these worlds as output but the console shows
please do say how to avoid it.

public class Indian {

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");
configuration.setUseGrammar(false);

LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);

recognizer.startRecognition(true);
System.out.println("Start speaking. \n");
SpeechResult result;
long timer=0;

while (timer < 2*60*1000) {
result = recognizer.getResult();
if (result != null) {
for(WordResult word : result.getWords()) {
System.out.println("You said: " + word + '\n');
}
}
else {
System.out.println("I can't hear what you said.\n");
}
}
recognizer.stopRecognition();
}
}


my current output

You said: {<sil>, 0.999, [1060:2170]}

You said: {known, 0.999, [2180:2660]}


Required output:

You said: i am a java student

Answer Source

You just need to reformat the output. Right now you are printing them on each line, and you are using the generic toString of the class WordResult.

Try something like the following :

public class Indian
{
    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");
        configuration.setUseGrammar(false);

        LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);

        recognizer.startRecognition(true);
        System.out.println("Start speaking. \n");
        SpeechResult result;
        long timer = 0;

        while (timer < 2 * 60 * 1000)
        {
            result = recognizer.getResult();
            if (result != null)
            {
                System.out.print("You said: ");
                for (WordResult word : result.getWords())
                {
                    if (!word.isFiller())
                        System.out.print(word.getWord() + " ");
                }
                System.out.println();
            }
            else
            {
                System.out.println("I can't hear what you said.\n");
            }
        }
        recognizer.stopRecognition();
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download