问题
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 String
s, 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