0

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?

Steve Benett
  • 12,843
  • 7
  • 59
  • 79

1 Answers1

2

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).

Kaarel
  • 10,554
  • 4
  • 56
  • 78
  • ATM I do exactly this, but the float array always is null below API 14. Therefore I check the SDK versions to decide which code should be executed. I'm just asking for a workaround because the most accurate result sometimes isn't the result I expect. – Steve Benett Sep 11 '13 at 09:48
  • 1
    I think your original question is somewhat incomplete: (1) based on the statement "the float array always is null below API 14" I'm assuming that you are using a certain implementation of the speech API (e.g. Google Voice Search). If so, then specify which implementation and version, and why are you confident that its next version would not contain confidence scores also at lower API levels. (2) explain the reason behind "the most accurate result sometimes isn't the result I expect". – Kaarel Sep 11 '13 at 12:42
  • Based on your comment I asked a new [question](http://stackoverflow.com/questions/18765419/android-speechrecognizer-confidence-values-are-confusing). Maybe you can have a look? – Steve Benett Sep 12 '13 at 14:02
  • 1
    Well, I had a look at your new question, but didn't find answers to my questions (1) and (2)... Again, the API spec does not prescribe that every value should be higher than 0. It does not even prescribe that the confidence array is non `null`. It's all up to the app that implements the speech recognizer and I don't know which app it is in your case... (If it's Google Voice Search, then ask Google directly. To my knowledge there is no public documentation of this app and it's not open source either.) – Kaarel Sep 12 '13 at 15:06
  • AFAIK I call the build in SpeechRecognizer via an Intent. Like it's supposed in Reto Meiers book. I don't know in which way this call can differ in which App is called by the Intent. IMO it's just the default Speech Recognition. Or I'm wrong? – Steve Benett Sep 12 '13 at 15:12
  • 1
    There is no notion of "default speech recognition" on Android. There are apps which provide the speech recognition service. Sometimes there are more than one of such apps installed and then the end user decides which of them is the "default". The API spec is not very detailed and prescribing regarding what the apps should do. Maybe also read this answer: http://stackoverflow.com/a/18657832/12547 – Kaarel Sep 12 '13 at 15:32