Speech Recognizer get confidence below API 14

2019-09-06 14:11发布

问题:

I'm using the SpeechRecognizer with minSDK 14 and added a filter to get the most accurate result. This code I have in onActivityResult() of my Activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == VOICE_RECOGNITION && resultCode == RESULT_OK) {

        ArrayList<String> results = data
            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        // this is only available in API 14
        String confidenceExtra = RecognizerIntent.EXTRA_CONFIDENCE_SCORES; 
        float[] confidence = data.getFloatArrayExtra(confidenceExtra);

        // My filtering...
    }
}

Because the filtering is based on the confidence of every result I need this constant RecognizerIntent.EXTRA_CONFIDENCE_SCORES to be able to request the confidence. But sadly this is only available in API 14++ and AFAIK the SpeechRecognition is not available in the Support Package.

Is there a way to get the confidence for the results in lower API Levels? Or is there a work around to do some filtering based on other values?

回答1:

As with most of the Android speech recognition API, "This extra is optional and might not be provided." (quote from the spec).

I think it's a good idea to check for this float array even on lower API levels (just backport the EXTRA_CONFIDENCE_SCORES constant). If the float array corresponding to this extra is not present then just fall back to assuming that EXTRA_RESULTS is ordered by confidence (as the API documentation suggests).