2013-10-24 82 views
0

我有一個API Android VoiceRecognizer的應用程序。它運作良好,但我無法得到結果的信心評分。爲此,我使用RecognizerIntent.EXTRA_CONFIDENCE_SCORES,但它不返回任何結果。我與API 16的設備,我已經在清單中指定:沒有結果RecognizerIntent.EXTRA_CONFIDENCE_SCORES

​​

這是我的代碼

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); 
    } 

} 

你有什麼想法,爲什麼我還沒有導致的置信度(我有5項結果爲RecognizerIntent.EXTRA_RESULTS)? 謝謝你的幫助。

回答

2

在鏈接EXTRA_CONFIDENCE_SCORES你會看到它返回一個浮點數組,而不是一個字符串數組。

因此,你將不得不使用這樣的事情:

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

要知道,雖然,有一些bugs identified與信心分數。