从Android语音注册获取号码 [英] Get numbers from Android Voice Regnition
问题描述
我已经实现了这样的识别器意图.
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);
这样的回报
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
}
显然,此代码无法正常工作,但我想知道是否有人对此有解决方案,因为Google搜索不会产生任何结果.谢谢您的帮助
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
推荐答案
您不需要语法.
看看我如何在这段代码中做到这一点.
Check out how I do it in this code.
该库中的代码基本上遍历了调用此方法的所有可能的识别结果中的所有单词:
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...
这篇关于从Android语音注册获取号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!