Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions src/android/SpeechRecognition.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ else if (ACTION_SPEECH_RECOGNIZE_START.equals(action)) {
}

String lang = args.optString(0, "en");
boolean interimResults = args.optBoolean(1);

this.speechRecognizerCallbackContext = callbackContext;

final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,lang);
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,interimResults);

intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);

Expand Down Expand Up @@ -128,15 +130,42 @@ private void fireRecognitionEvent(ArrayList<String> transcripts, float[] confide
JSONArray results = new JSONArray();
try {
for(int i=0; i<transcripts.size(); i++) {
JSONArray alternatives = new JSONArray();
JSONObject result = new JSONObject();
result.put("transcript", transcripts.get(i));
result.put("final", true);
JSONObject alternative = new JSONObject();
result.put("isFinal", true);
result.put("length",1);
result.put("0",alternative);
alternative.put("transcript", transcripts.get(i));
if (confidences != null) {
result.put("confidence", confidences[i]);
alternative.put("confidence", confidences[i]);
}
alternatives.put(result);
results.put(alternatives);
results.put(result);
}
event.put("type", "result");
event.put("emma", null);
event.put("interpretation", null);
event.put("results", results);
} catch (JSONException e) {
// this will never happen
}
PluginResult pr = new PluginResult(PluginResult.Status.OK, event);
pr.setKeepCallback(true);
this.speechRecognizerCallbackContext.sendPluginResult(pr);
}

private void fireInterimRecognitionEvent(ArrayList<String> transcripts) {
Log.d(LOG_TAG,"hey, this is me. This is an interim result");
JSONObject event = new JSONObject();
JSONArray results = new JSONArray();
try {
for(int i=0; i<transcripts.size(); i++) {
JSONObject result = new JSONObject();
JSONObject alternative = new JSONObject();
result.put("isFinal", false);
result.put("length",1);
result.put("0",alternative);
alternative.put("transcript", transcripts.get(i));
results.put(result);
}
event.put("type", "result");
event.put("emma", null);
Expand Down Expand Up @@ -213,8 +242,12 @@ public void onEvent(int eventType, Bundle params) {

@Override
public void onPartialResults(Bundle partialResults) {
Log.d(LOG_TAG, "partial results");
}
Log.d(LOG_TAG, "partial results");
ArrayList<String> partial = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(partial.size() > 0){
fireInterimRecognitionEvent(partial);
}
}

@Override
public void onReadyForSpeech(Bundle params) {
Expand Down
2 changes: 1 addition & 1 deletion www/SpeechRecognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ SpeechRecognition.prototype.start = function() {
}
};

exec(successCallback, errorCallback, "SpeechRecognition", "start", [this.lang]);
exec(successCallback, errorCallback, "SpeechRecognition", "start", [this.lang,this.interimResults]);
};

SpeechRecognition.prototype.stop = function() {
Expand Down