使用PocketSphinx识别多个关键字

我已经安装了PocketSphinx演示程序,并且在Ubuntu和Eclipse下运行良好,但是尽管尝试了一下,但仍无法弄清楚如何添加多个单词的识别。


我只想让代码识别单个单词,然后我就可以在代码中识别它们switch(),例如“上”,“下”,“左”,“右”。我不想识别句子,只能识别单个单词。


任何帮助,将不胜感激。我发现其他用户也有类似的问题,但到目前为止,没人知道答案。


让我感到困惑的一件事是,为什么我们根本需要使用“唤醒”常量?


private static final String KWS_SEARCH = "wakeup";

private static final String KEYPHRASE = "oh mighty computer";

.

.

.

recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);

有wakeup什么关系吗?


我已经取得了一些进步(?):使用,addGrammarSearch我可以使用.gram文件列出我的单词,例如up,down,left,right,forwards,backwards,如果我只说那些特定的单词,这似乎很好用。但是,任何其他字词都将导致系统将所陈述的内容与“最近”字词进行匹配。理想情况下,如果.gram文件中没有说出的单词,我不希望被识别...


开心每一天1111
浏览 1232回答 3
3回答

aluckdog

您可以使用addKeywordSearch哪个用于归档密钥短语。每行一个短语,例如//中的每个短语都有阈值up /1.0/down /1.0/left /1.0/right /1.0/forwards /1e-1/必须选择阈值以避免错误警报。

萧十郎

正在为PocketSphinx演示更新Antinous修订,以使其能够在Android Studio上运行。这就是我到目前为止//Note: change MainActivity to PocketSphinxActivity for demo use...public class MainActivity extends Activity implements RecognitionListener {private static final String DIGITS_SEARCH = "digits";private SpeechRecognizer recognizer;/* Used to handle permission request */private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;@Overridepublic void onCreate(Bundle state) {&nbsp; &nbsp; super.onCreate(state);&nbsp; &nbsp; setContentView(R.layout.main);&nbsp; &nbsp; ((TextView) findViewById(R.id.caption_text))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setText("Preparing the recognizer");&nbsp; &nbsp; // Check if user has given permission to record audio&nbsp; &nbsp; int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);&nbsp; &nbsp; if (permissionCheck != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; new AsyncTask<Void, Void, Exception>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Exception doInBackground(Void... params) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assets assets = new Assets(MainActivity.this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File assetDir = assets.syncAssets();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setupRecognizer(assetDir);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return e;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPostExecute(Exception result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((TextView) findViewById(R.id.caption_text))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setText("Failed to init recognizer " + result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }.execute();&nbsp; &nbsp; ((TextView) findViewById(R.id.caption_text)).setText("Say one, two, three, four, five, six...");}/**&nbsp;* In partial result we get quick updates about current hypothesis. In&nbsp;* keyword spotting mode we can react here, in other modes we need to wait&nbsp;* for final result in onResult.&nbsp;*/@Overridepublic void onPartialResult(Hypothesis hypothesis) {&nbsp; &nbsp; if (hypothesis == null) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; } else if (hypothesis != null) {&nbsp; &nbsp; &nbsp; &nbsp; if (recognizer != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //recognizer.rapidSphinxPartialResult(hypothesis.getHypstr());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = hypothesis.getHypstr();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (text.equals(DIGITS_SEARCH)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recognizer.cancel();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; performAction();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recognizer.startListening(DIGITS_SEARCH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Toast.makeText(getApplicationContext(),"Partial result = " +text,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}@Overridepublic void onResult(Hypothesis hypothesis) {&nbsp; &nbsp; ((TextView) findViewById(R.id.result_text)).setText("");&nbsp; &nbsp; if (hypothesis != null) {&nbsp; &nbsp; &nbsp; &nbsp; String text = hypothesis.getHypstr();&nbsp; &nbsp; &nbsp; &nbsp; makeText(getApplicationContext(), "Hypothesis" +text, Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }else if(hypothesis == null){&nbsp; &nbsp; &nbsp; &nbsp; makeText(getApplicationContext(), "hypothesis = null", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }}@Overridepublic void onDestroy() {&nbsp; &nbsp; super.onDestroy();&nbsp; &nbsp; recognizer.cancel();&nbsp; &nbsp; recognizer.shutdown();}@Overridepublic void onBeginningOfSpeech() {}@Overridepublic void onEndOfSpeech() {&nbsp; &nbsp;reset();}@Overridepublic void onTimeout() {}private void setupRecognizer(File assetsDir) throws IOException {&nbsp; &nbsp; // The recognizer can be configured to perform multiple searches&nbsp; &nbsp; // of different kind and switch between them&nbsp; &nbsp; recognizer = defaultSetup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setAcousticModel(new File(assetsDir, "en-us-ptm"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // .setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getRecognizer();&nbsp; &nbsp; recognizer.addListener(this);&nbsp; &nbsp; File digitsGrammar = new File(assetsDir, "digits.gram");&nbsp; &nbsp; recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);}private void reset(){&nbsp; &nbsp; recognizer.stop();&nbsp; &nbsp; recognizer.startListening(DIGITS_SEARCH);}@Overridepublic void onError(Exception error) {&nbsp; &nbsp; ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());}public void performAction() {&nbsp; &nbsp; // do here whatever you want&nbsp; &nbsp; makeText(getApplicationContext(), "performAction done... ", Toast.LENGTH_SHORT).show();}}请注意:此工作正在进行中。过一会再来检查。建议将不胜感激。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android