Reputation: 81
I'm implementing a kind a calendar for Google Glass. In this calendar, I would like to be able to use speech recognition in order to select a calendar slot. For example it should be nice to say "first week of August" and android should be able to recognize the date. I'm sure I have already saw that on Android but I cannot find it anymore. Could you help me ? Thank you very much !
Upvotes: 2
Views: 1302
Reputation: 81
As Blacksad commented, Wit.AI API is a very good solution. It performs some nice natural language processing. I recommend you not to use the .jar but directly the code inside. Here is an example of Glass Speech recognition combined with Wit.API
MainActivity :
@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 });
}
And here id the 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) {
}
}
I hope it will help you !
Upvotes: 1