0

hypothesis.getHypstr() is always one value, even after I change the keyword!

I am using pocketsphinx to do speech recognition, and I let the user change what to listen for. This value is stored in my shared preferences. My problem is that hypothesis.getHypstr() is only called when the previous keyword is spoken.

For example:

If it is set to default keyword (oranges and rainbows), then the recognition works fine. But, if the user changes it to "hello computer" then the onPartialResult method still only gets called when the user says hello, and hypothesis.getHypstr() is still oranges and rainbows.

onCreate:

try {
            Assets assets = new Assets(MyService.this);
            File assetDir = assets.syncAssets();
            setupRecognizer(assetDir);

            Log.v(TAG, "SET UP DIRECTORIES STARTING LISTENING!");
            mSpeechRecognizer.startListening("usersKeyword");

        } catch (IOException e) {
            e.printStackTrace();
            Log.v(TAG, e.toString());
        }

setupRecognizer()

public void setupRecognizer(File sphinxDir) {

    try {
        mSpeechRecognizer = defaultSetup()
                .setAcousticModel(new File(sphinxDir, "en-us-ptm"))
                .setDictionary(new File(sphinxDir, "cmudict-en-us.dict"))
                .setBoolean("-allphone_ci", true)
                .setKeywordThreshold(1e-40f)
                .getRecognizer();

    } catch (IOException e) {
        e.printStackTrace();
    }
    mSpeechRecognizer.addListener(this);
    mSpeechRecognizer.addKeyphraseSearch("usersKeyword", keyword.getString("keyword", "oranges and rainbows"));

}

onPartialResult:

@Override
public void onPartialResult(Hypothesis hypothesis) {

    if (hypothesis == null) { //no one spoke 
        return;
    }
    String text = hypothesis.getHypstr();
    Log.v(TAG, "TEXT: " + text + "hypothesis.getHypstr: " + hypothesis.getHypstr());

    if (text.equals(keyword.getString("keyword", "oranges and rainbows"))) { //Only happens when text is oranges and rainbows, even after changing preference value!!!

        Log.v(TAG, "Heard user keyword!");

        mSpeechRecognizer.cancel();
        mSpeechRecognizer.startListening("usersKeyword");

    }

}

Why is hypothesis.getHypstr() always only one value, even after I change the value of the addKeyphraseSearch?

Thanks,

Ruchir

EDIT: I actually stop and start the service every time the user changes their input, and so onCreate() is called every time the user changes their data.

FULL CODE:

https://gist.github.com/anonymous/47efc9c1ca08d808e0be

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

2 Answers2

0

You need to call mSpeechRecognizer.addKeyphraseSearch() every time you want to change the key phrase.

Alexander Solovets
  • 2,447
  • 15
  • 22
  • But I have `mSpeechRecognizer.addKeyphraseSearch("usersKeyword", keyword.getString("keyword", "oranges and rainbows"));` in my `setupRecognizer` method. Shouldn't that work? Thanks so much! :) – Ruchir Baronia Mar 03 '16 at 05:06
  • Right, but you only call `setupRecognizer()` once. – Alexander Solovets Mar 03 '16 at 06:02
  • I actually stop and start the service every time the user changes their input, and so `onCreate()` is called every time the user changes their data. I have edited the question. Please let me know what I should do now. Thanks :) – Ruchir Baronia Mar 03 '16 at 06:37
  • It's hard to say, probably there's the logic flow error. I can help if you post the full source to reproduce. – Alexander Solovets Mar 03 '16 at 06:46
  • Maybe I need to delete all the asset files before making them? Is there something like a `assets.deleteSync` method? – Ruchir Baronia Mar 04 '16 at 00:55
  • No, there's no such method. Why would you need that? The code that you wrote should work as expected, so I suspect the error is in the logic. – Alexander Solovets Mar 04 '16 at 17:23
0

You do not need to destroy the service, you create it once with onCreate.

You can set the command in onStartCommand:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     recognizer.cancel();
     recognizer.addKeyphraseSearch("usersKeywords", intent.getStringExtra("keyword"););
     recognizer.startListening("usersKeywords");
}

From the other class which is a user of the service you start service with intent:

 Intent i = new Intent(this, MyService);
 i.putExtra("keyword", "hello");
 startService(i);

For more details read documentation

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87