Search code examples
javaandroidspeech-recognitiongoogle-speech-api

Android Speech Recognition: How to get results with highest confidence score?


I am trying to develop an android application with speech recognition. Please have a look at the below code.

 @Override
 public void onPartialResults(Bundle arg0) {
      Log.i(LOG_TAG, "onPartialResults");
      ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      float[] scores = arg0.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
      receiveWhatWasHeard(matches, scores);

      //Get result with highest score
 }

My problem here is to get the result with "Highest Confidence Score". How can I do that?


Solution

  • First element has the highest confidence score according to specification, so you can just take the first element of an array:

    String bestResult = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0);
    

    You do not need to retrieve confidence scores if you do not intend to use them.