2

我在 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"); ,即使这似乎不起作用。请帮我

4

1 回答 1

1

结果是空格分隔String的,所以你必须拆分你的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

于 2013-05-22T17:58:11.953 回答