Question

I have an application with the API Android VoiceRecognizer. It's work well but I can't get the confidence score of the result. For this I use RecognizerIntent.EXTRA_CONFIDENCE_SCORES but it's return no result. I have a device with the API 16 and I have specified on the manifest :

<uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />

This is my code

public class MainActivity extends Activity {
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

    private ListView mlvTextMatches;
    private Button mbtSpeak;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
        mbtSpeak = (Button) findViewById(R.id.btSpeak);
        checkVoiceRecognition();

        // Disable button if no recognition service is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() == 0)
        {
            mbtSpeak.setEnabled(false);
            mbtSpeak.setText("Recognizer not present");
        }
    }

    public void checkVoiceRecognition() {
        // Check if voice recognition is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() == 0) {
            mbtSpeak.setEnabled(false);
            mbtSpeak.setText("Voice recognizer not present");
            Toast.makeText(this, "Voice recognizer not present",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void speak(View view) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Straight talk please...");
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fr-FR");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
            if(resultCode == RESULT_OK) {
                ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                ArrayList<String> confidence = data.getStringArrayListExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES); ;
              if(confidence==null)
                  Log.d("VoiceRecognition","confidence null");
              else 
                  Log.d("VoiceRecognition","confidence "+confidence.size());

                mlvTextMatches.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
            }
            super.onActivityResult(requestCode, resultCode, data);
    }

}

Have you any ideas why I haven't result for the confidence score (I have 5 results for RecognizerIntent.EXTRA_RESULTS) ? Thank you for your help.

Was it helpful?

Solution

In the link to EXTRA_CONFIDENCE_SCORES you'll see that it returns a float array, not a string array.

Therefore you'll have to use something like this:

float [] confidence = data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);

Be aware though, there are some bugs identified with the confidence scores.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top