分析android语音识别结果中的命令

时间:2013-05-20 17:44:49

标签: android speech-recognition

我在Android中有一个语音识别应用程序,我想比较我得到的结果与我自己的字符串 这是我的代码

if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
        {

            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            for(String resultString: matches)
            {

            if(resultString.equalsIgnoreCase("go"))
                Toast.makeText(getBaseContext(), "go", Toast.LENGTH_SHORT);
            else if(resultString.equalsIgnoreCase("stop"))
                Toast.makeText(getBaseContext(), "stop", Toast.LENGTH_SHORT);
            else if(resultString.equalsIgnoreCase("back"))
                Toast.makeText(getBaseContext(), "back", Toast.LENGTH_SHORT);
            else if(resultString.equalsIgnoreCase("right"))
                Toast.makeText(getBaseContext(), "right", Toast.LENGTH_SHORT);
            else if(resultString.equalsIgnoreCase("left"))
                Toast.makeText(getBaseContext(), "left", Toast.LENGTH_SHORT);
            else
                Toast.makeText(getBaseContext(), "wrong command use go , stop , back , right or left", Toast.LENGTH_SHORT);


            }
        }

我也尝试过使用matches.contains("go"); 即使这似乎不起作用。请帮帮我

1 个答案:

答案 0 :(得分:1)

结果以空格分隔String s,因此您必须拆分resultString

使用这样的循环:

@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);
                    }
                }
            }
        }
    }
}

更好地使用GAST中的工具,这是一个开源项目,有一些语音识别工具。

具体而言,您至少需要WordMatcherWordList