Analyzing commands in android speech recognition results

强颜欢笑 提交于 2019-12-12 10:12:51

问题


i have a speech recognition app in android and i want to compare the results that i get with my own strings this is my code

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


            }
        }

i have also tried using matches.contains("go"); even that doesnt seem to work. please help me


回答1:


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



来源:https://stackoverflow.com/questions/16655162/analyzing-commands-in-android-speech-recognition-results

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