Google Cloud Speech API add SpeechContext

∥☆過路亽.° 提交于 2019-12-23 05:42:08

问题


I would like to add some keywords to my app, so the API can recognize more efficiently the spoken words. For example Im having trouble recognizing the some Italian words that starts with E ,(E` per me) for example. Or in German (er geht).

Here is my code:

public void recognize (int sampleRate) {
    if (mApi == null) {
        Log.w(TAG, "API not ready. Ignoring the request.");
        return;
    }
    // Configure the API
    mRequestObserver = mApi.streamingRecognize(mResponseObserver);
    mRequestObserver.onNext(StreamingRecognizeRequest.newBuilder()
            .setStreamingConfig(StreamingRecognitionConfig.newBuilder()
                    .setConfig(RecognitionConfig.newBuilder()
                            .setLanguageCode(getDefaultLanguageCode())
                            .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                            .setSampleRateHertz(sampleRate)
                            .build())
                    .setInterimResults(true)
                    .setSingleUtterance(true)
                    .build())
            .build());
}

Setting the language for different cases :

private String getDefaultLanguageCode() {

    SharedPreferences getLangSharedPrefs = getSharedPreferences("langSelected",0);
    String selectedLanguage = getLangSharedPrefs.getString("langSelected", null);
    switch (selectedLanguage) {
        case "German":
            langaugeCode = "de-DE";
            break;
        case "Italian":
            langaugeCode = "it-IT";
            break;
        case "Spanish" :
            langaugeCode = "es-ES";
            break;
        case "French" :
            langaugeCode = "fr-FR";
            break;
    }
    return langaugeCode;
}

回答1:


I found the solution :

 public void startRecognizing(int sampleRate) {
    if (mApi == null) {
        Log.w(TAG, "API not ready. Ignoring the request.");
        return;
    }
    // Configure the API
    mRequestObserver = mApi.streamingRecognize(mResponseObserver);
    SpeechContext.Builder speechBuilder = SpeechContext.newBuilder();
    speechBuilder.addPhrases("E per me");
    speechBuilder.addPhrases("E");
    mRequestObserver.onNext(StreamingRecognizeRequest.newBuilder()
            .setStreamingConfig(StreamingRecognitionConfig.newBuilder()
                    .setConfig(RecognitionConfig.newBuilder()
                            .setLanguageCode(getDefaultLanguageCode())
                            .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                            .setSampleRateHertz(sampleRate)
                            .addSpeechContexts(speechBuilder)
                            .build())
                    .setInterimResults(true)
                    .setSingleUtterance(true)
                    .build())
            .build());
}


来源:https://stackoverflow.com/questions/45545278/google-cloud-speech-api-add-speechcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!