- 使用SpeechRecognizer类进行语音识别,该类可以从设备的麦克风捕获音频并以文本形式返回结果。使用SpeechRecognizer时,可以设置最大识别结果数以及使用的词汇集。例如,以下代码就会把词汇集设置为英语(美国):
private static final Locale DEFAULT_LOCALE = Locale.US;
private static final String[] DEFAULT_PHRASES = {"hello", "world"};
private void startDictation() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, DEFAULT_LOCALE);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_PHRASES, DEFAULT_PHRASES);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String result = matches.get(0);
// 处理语音识别结果
}
}
- 使用自然语言处理库,例如OpenNLP或Stanford NLP,来处理语音识别结果。这些库可以对文本进行实体识别、命名实体识别、句法分析等操作。例如,以下代码使用OpenNLP来提取句子中的人名:
public static String extractPersonName(String input) throws IOException {
InputStream modelIn = new FileInputStream("path/to/en-ner-person.bin");
TokenNameFinderModel model = new TokenNameFinderModel(modelIn);
NameFinderME nameFinder = new NameFinderME(model);
String[] tokens = input.split("\\s+");
Span[] nameSpans = nameFinder.find(tokens);
StringBuilder sb = new StringBuilder();
for (Span span : nameSpans) {
for