首页 > 解决方案 > 在java中添加对谷歌云语音的boost适配

问题描述

我正在使用谷歌的云语音 api 通过麦克风进行语音到文本的转换,谁能告诉我如何为我的 java 代码添加增强适应,以便我可以为某些短语分配适当的增强值。我需要在java中做到这一点。我已经阅读了一个谷歌文档,其中显示了如何在 json 请求中添加值:这是该链接:“ https://cloud.google.com/speech-to-text/docs/speech-adaptation ”(在底部)

我需要在java中做同样的事情。

标签: javaspeech-recognitionspeech-to-textgoogle-speech-apigoogle-cloud-speech

解决方案


您可以setBoostSpeechContextbuilder 和builderaddSpeechContexts中使用RecognitionConfig

List<String> phrases = new ArrayList<String>();
phrases.add("Sheryar");
SpeechContext item = SpeechContext.newBuilder().addAllPhrases(phrases).setBoost(20f).build();
RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.LINEAR16).setLanguageCode("en-IN").addSpeechContexts(item).setSampleRateHertz(48000).setEnableWordTimeOffsets(true);

您需要执行以下导入:

import com.google.cloud.speech.v1p1beta1.RecognitionAudio;
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
import com.google.cloud.speech.v1p1beta1.RecognizeRequest;
import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
import com.google.cloud.speech.v1p1beta1.SpeechClient;
import com.google.cloud.speech.v1p1beta1.SpeechContext;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;

您需要将以下依赖项添加到您的 pom 中:

<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-speech -->
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-speech</artifactId>
    <version>1.22.2</version>
</dependency>

推荐阅读