Example usage for android.speech RecognizerIntent EXTRA_CONFIDENCE_SCORES

List of usage examples for android.speech RecognizerIntent EXTRA_CONFIDENCE_SCORES

Introduction

In this page you can find the example usage for android.speech RecognizerIntent EXTRA_CONFIDENCE_SCORES.

Prototype

String EXTRA_CONFIDENCE_SCORES

To view the source code for android.speech RecognizerIntent EXTRA_CONFIDENCE_SCORES.

Click Source Link

Document

A float array of confidence scores of the recognition results when performing #ACTION_RECOGNIZE_SPEECH .

Usage

From source file:com.ipo.wiimote.SpeechRecognizer.java

/**
 * Handle the results from the recognition activity.
 *///from ww  w  .  j  a va  2  s. co m
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it could have heard
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        float[] confidence = data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);

        if (confidence != null) {
            Log.d(LOG_TAG, "confidence length " + confidence.length);
            Iterator<String> iterator = matches.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                Log.d(LOG_TAG, "Match = " + iterator.next() + " confidence = " + confidence[i]);
                i++;
            }
        } else {
            Log.d(LOG_TAG, "No confidence" + "");
        }

        ReturnSpeechResults(requestCode, matches);
    } else {
        // Failure - Let the caller know
        ReturnSpeechFailure(resultCode);
    }

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

From source file:conversandroid.SimpleASR.java

/**
 *  Shows the formatted best of N best recognition results (N-best list) from
 *  best to worst in the <code>ListView</code>. 
 *  For each match, it will render the recognized phrase and the confidence with 
 *  which it was recognized.//w  ww .  j a v  a2s . c  o  m
 */
@SuppressLint("InlinedApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ASR_CODE) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                //Retrieves the N-best list and the confidences from the ASR result
                ArrayList<String> nBestList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                float[] nBestConfidences = null;

                if (Build.VERSION.SDK_INT >= 14) //Checks the API level because the confidence scores are supported only from API level 14
                    nBestConfidences = data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);

                //Creates a collection of strings, each one with a recognition result and its confidence
                //following the structure "Phrase matched (conf: 0.5)"
                ArrayList<String> nBestView = new ArrayList<String>();

                for (int i = 0; i < nBestList.size(); i++) {
                    if (nBestConfidences != null) {
                        if (nBestConfidences[i] >= 0)
                            nBestView.add(nBestList.get(i) + " (conf: "
                                    + String.format("%.2f", nBestConfidences[i]) + ")");
                        else
                            nBestView.add(nBestList.get(i) + " (no confidence value available)");
                    } else
                        nBestView.add(nBestList.get(i) + " (no confidence value available)");
                }

                //Includes the collection in the ListView of the GUI
                setListView(nBestView);

                Log.i(LOGTAG, "There were : " + nBestView.size() + " recognition results");
            }
        } else {
            //Reports error in recognition error in log
            Log.e(LOGTAG, "Recognition was not successful");
        }

        //Enable button
        Button speak = (Button) findViewById(R.id.speech_btn);
        speak.setEnabled(true);
    }
}