0

我有一个带有 API Android VoiceRecognizer 的应用程序。它运作良好,但我无法获得结果的置信度得分。为此,我使用了 RecognizerIntent.EXTRA_CONFIDENCE_SCORES 但它没有返回任何结果。我有一个带有 API 16 的设备,我在清单上指定了:

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

这是我的代码

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 结果)?谢谢您的帮助。

4

1 回答 1

2

EXTRA_CONFIDENCE_SCORES的链接中,您会看到它返回一个浮点数组,而不是字符串数组。

因此你必须使用这样的东西:

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

但请注意,置信度分数中存在一些错误。

于 2013-10-30T01:50:30.670 回答