问题
I have implemented a recognizer intent like this.
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tell me stuff");
startActivityForResult(intent, REQUEST_CODE);
With a return like this
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
}
What I would like to do with this data is implement simple grammar rules with numbers. For example something like this
if(matches.contains("my number is"))
{
string number = matches.getNextWord();
//Then parse the string into an integer
}
Obviously this code doesn't work but I'm wondering if anyone has a solution for this as a Google search yielded absolutely nothing. Thanks for any help
回答1:
You don't need a grammar.
Check out how I do it in this code.
https://github.com/gast-lib/gast-lib/blob/master/app/src/root/gast/playground/speech/food/command/AskForCalories.java
The code within that library basically loops over all the words of all of the possible recognition results calling this method:
private boolean isNumber(String word)
{
boolean isNumber = false;
try
{
Integer.parseInt(word);
isNumber = true;
} catch (NumberFormatException e)
{
isNumber = false;
}
return isNumber;
}
You may also want to have your code accept other words that sounds like numbers such as "too" "tree" "for" etc...
来源:https://stackoverflow.com/questions/13465285/get-numbers-from-android-voice-regnition