|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.speech; |
| 18 | + |
| 19 | +// [START speech_adaptation_beta] |
| 20 | +import com.google.cloud.speech.v1p1beta1.RecognitionAudio; |
| 21 | +import com.google.cloud.speech.v1p1beta1.RecognitionConfig; |
| 22 | +import com.google.cloud.speech.v1p1beta1.RecognizeRequest; |
| 23 | +import com.google.cloud.speech.v1p1beta1.RecognizeResponse; |
| 24 | +import com.google.cloud.speech.v1p1beta1.SpeechClient; |
| 25 | +import com.google.cloud.speech.v1p1beta1.SpeechContext; |
| 26 | +import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative; |
| 27 | +import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +public class SpeechAdaptation { |
| 32 | + |
| 33 | + public void speechAdaptation() throws IOException { |
| 34 | + String uriPath = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"; |
| 35 | + speechAdaptation(uriPath); |
| 36 | + } |
| 37 | + |
| 38 | + public static void speechAdaptation(String uriPath) throws IOException { |
| 39 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 40 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 41 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 42 | + try (SpeechClient speechClient = SpeechClient.create()) { |
| 43 | + |
| 44 | + // Provides "hints" to the speech recognizer to favor specific words and phrases in the |
| 45 | + // results. |
| 46 | + // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1p1beta1#google.cloud.speech.v1p1beta1.SpeechContext |
| 47 | + SpeechContext speechContext = |
| 48 | + SpeechContext.newBuilder().addPhrases("Brooklyn Bridge").setBoost(20.0F).build(); |
| 49 | + // Configure recognition config to match your audio file. |
| 50 | + RecognitionConfig config = |
| 51 | + RecognitionConfig.newBuilder() |
| 52 | + .setEncoding(RecognitionConfig.AudioEncoding.MP3) |
| 53 | + .setSampleRateHertz(44100) |
| 54 | + .setLanguageCode("en-US") |
| 55 | + .addSpeechContexts(speechContext) |
| 56 | + .build(); |
| 57 | + // Set the path to your audio file |
| 58 | + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uriPath).build(); |
| 59 | + |
| 60 | + // Make the request |
| 61 | + RecognizeRequest request = |
| 62 | + RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build(); |
| 63 | + |
| 64 | + // Display the results |
| 65 | + RecognizeResponse response = speechClient.recognize(request); |
| 66 | + for (SpeechRecognitionResult result : response.getResultsList()) { |
| 67 | + // First alternative is the most probable result |
| 68 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 69 | + System.out.printf("Transcript: %s\n", alternative.getTranscript()); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +// [END speech_adaptation_beta] |
0 commit comments