gpt4 book ai didi

android - 带日期的语音识别

转载 作者:行者123 更新时间:2023-11-29 01:41:40 25 4
gpt4 key购买 nike

我正在为 Google Glass 实现一种日历。在此日历中,我希望能够使用语音识别来选择日历槽。例如,说“八月的第一周”应该很好,android 应该能够识别日期。我确定我已经在 Android 上看到过它,但我再也找不到了。你可以帮帮我吗 ?非常感谢!

最佳答案

正如 Blacksad 评论的那样,Wit.AI API 是一个非常好的解决方案。它执行一些很好的自然语言处理。我建议你不要使用 .jar 而是直接使用里面的代码。下面是 Glass 语音识别结合 Wit.API 的例子

主要 Activity :

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
displaySpeechRecognizer();
}

private static final int SPEECH_REQUEST = 0;

private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent, SPEECH_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST && resultCode == RESULT_OK) {
List<String> results = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
Card c=new Card(getApplicationContext());
c.setText("requesting wit api...");
setContentView(c.getView());
process(spokenText);

}
super.onActivityResult(requestCode, resultCode, data);
}

public void process(String text) {
ConnexionTask c = new ConnexionTask() {
protected void onPostExecute(String result) {
try {
WitResponse response = null;
Gson gson = new Gson();
response = (WitResponse) gson.fromJson(result,
WitResponse.class);
//process the response here
} catch (Exception e) {
System.out.println(e);
}
}
};
c.execute(new String[] { text });
}

这里是 AsyncTask ConnexionTask:

public class connexion extends AsyncTask<String, String, String> {

private String _accessToken = "YOUR TOKEN HERE";

protected String doInBackground(String... text) {
// TODO Auto-generated method stub
String response = null;
try {
System.out.println("Requesting ...." + text[0]);
String getUrl = String.format(
"%s%s",
new Object[] { "https://api.wit.ai/message?q=",
URLEncoder.encode(text[0], "utf-8") });
System.out.println(getUrl);
URL url = new URL(getUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.addRequestProperty("Authorization", String.format(
"Bearer %s", new Object[] { this._accessToken }));
try {
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
response = IOUtils.toString(in);
in.close();
} finally {
urlConnection.disconnect();
}
} catch (Exception e) {
System.out.println(e);
System.out
.println("An error occurred during the request, did you set your token correctly?");
}
return response;
}

protected void onPostExecute(String result) {
}

}

希望对您有所帮助!

关于android - 带日期的语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com