Analyzing commands in android speech recognition results

邮差的信 提交于 2019-12-06 13:17:41

The results are space separated Strings, so you have to split your resultString

Use a loop like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
    {
        if (resultCode == RESULT_OK)
        {
            List<String> heard = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            Set<String> matchThese = new HashSet<String>();
            matchThese.add("tree");
            matchThese.add("car");

            for (String said : heard)
            {
                String[] saidWords = said.split("\\s");
                for (String wordSaid : saidWords)
                {
                    if (matchThese.contains(wordSaid))
                    {
                        tts.speak("You said the correct word",
                            TextToSpeech.QUEUE_FLUSH, null);
                    }
                }
            }
        }
    }
}

Better yet use the tools within GAST, an open source project which has some speech recognition tools.

Specifically, you at least need WordMatcher and WordList

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!