Search code examples
javaandroidspeech-recognitionpocketsphinxpocketsphinx-android

how to use pocketsphinx in android for multiple keyword activation without going to menu


I want pocketsphinx to listen for certain keywords and do respective actions instead of going to the menu like their demo app. I have three keywords

private static final String KEYPHRASE1 = "good morning";
private static final String KEYPHRASE2 = "good evening";
private static final String KEYPHRASE3 = "good night";

This is my onPartialResult()

 @Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
   }

I want to call some methods directly when it recognizes the three keywords. I also don't know whether I need to use switchSearch() and KWS_SEARCH like in the demo app. I am new to android and I searched days for the answer. Someone, please help. This speech recognition is necessary for my app.


Solution

  • You have asked two question so I will answer them both in points.

    • I want to call some methods directly when it recognizes the three keywords
      If you look at the demo app's source code, you will notice that there are two functions that are getting control and performing computation on the hypothesis (recognized keywords) - onPartialResult() and onResult. The former is used to get quick updates about the words being spoken while the latter is used to get the predictions after the recognizer has been stopped, i.e. after all the keywords have been spoken and the user has stopped the recognizer.
      So, first you need to decide whether you want to use onPartialResult() or onResult(). After that, notice that both methods perform some operations on the recognized text. onPartialResult() calls switchSearch() while onResult() calls makeText(). In your case too, you want to call some function when it recognizes your three keywords. Create a function in the same class and call it as you'd normally call the function anywhere else in your program!
    • I also don't know whether I need to use switchSearch() and KWS_SEARCH like in the demo app.
      In that case, you should look at the answer suggested by Nikolay Shmyrev in the comments above. It explicitly details how to recognize several keywords in Android.