2016-03-03 20 views
0

hypothesis.getHypstr()始終是一個值,即使我更改了關鍵字!onPartialResult始終是一個值,即使在我更改關鍵字之後?

我使用pocketsphinx做語音識別,我讓用戶改變聽什麼。該值存儲在我的共享首選項中。我的問題是hypothesis.getHypstr()僅在使用前一個關鍵字時被調用。

例如:

如果它被設置爲默認的關鍵字(橙子和彩虹),然後識別工作正常。但是,如果用戶將其更改爲「hello computer」,則onPartialResult方法仍然只在用戶打招呼時才被調用,hypothesis.getHypstr()仍然是桔子和彩虹。

的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"); 

    } 

} 

爲什麼hypothesis.getHypstr()總是隻有一個值,即使在我更改addKeyphraseSearch的值後?

感謝,

Ruchir

編輯: 其實我停下來,每次用戶改變他們輸入時啓動該服務,所以onCreate()被稱爲每次用戶改變他們的數據的時間。

全碼:

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

+0

您需要顯示完整的代碼,而不僅僅是部分代碼。 –

+0

@NikolayShmyrev問題在於即使在更改用戶關鍵字後,'hypothesis.getHypstr();'總是'桔子和彩虹'。 –

+0

@NikolayShmyrev也許我需要刪除所有的資產文件之前,讓他們?有沒有像'assets.deleteSync'方法? –

回答

0

您不需要銷燬服務,您可以使用onCreate創建一次。

可以在onStartCommand設置命令:

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

從其他類,這是該服務的用戶,您有意向啓動服務:

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

有關詳細信息讀取documentation

0

你必須要改變的關鍵短語每次打電話mSpeechRecognizer.addKeyphraseSearch()

+0

但我有'mSpeechRecognizer.addKeyphraseSearch(「usersKeyword」,keyword.getString(「keyword」,「oranges and rainbows」));'在我的'setupRecognizer'方法中。不應該那樣工作?非常感謝! :) –

+0

對,但你只調用'setupRecognizer()'一次。 –

+0

每次用戶更改輸入時,我都會停止並啓動服務,因此每次用戶更改其數據時都會調用onCreate()。我編輯了這個問題。請讓我知道我現在應該做什麼。謝謝:) –

相關問題