|
| 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_context_classes] |
| 20 | +import com.google.cloud.speech.v1.RecognitionAudio; |
| 21 | +import com.google.cloud.speech.v1.RecognitionConfig; |
| 22 | +import com.google.cloud.speech.v1.RecognizeRequest; |
| 23 | +import com.google.cloud.speech.v1.RecognizeResponse; |
| 24 | +import com.google.cloud.speech.v1.SpeechClient; |
| 25 | +import com.google.cloud.speech.v1.SpeechContext; |
| 26 | +import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
| 27 | +import com.google.cloud.speech.v1.SpeechRecognitionResult; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +class TranscribeContextClasses { |
| 32 | + |
| 33 | + void transcribeContextClasses() throws IOException { |
| 34 | + // TODO(developer): Replace these variables before running the sample. |
| 35 | + String storageUri = "gs://YOUR_BUCKET_ID/path/to/your/file.wav"; |
| 36 | + transcribeContextClasses(storageUri); |
| 37 | + } |
| 38 | + |
| 39 | + // Provides "hints" to the speech recognizer to favor specific classes of words in the results. |
| 40 | + static void transcribeContextClasses(String storageUri) throws IOException { |
| 41 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 42 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 43 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 44 | + try (SpeechClient speechClient = SpeechClient.create()) { |
| 45 | + // SpeechContext: to configure your speech_context see: |
| 46 | + // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#speechcontext |
| 47 | + // Full list of supported phrases (class tokens) here: |
| 48 | + // https://cloud.google.com/speech-to-text/docs/class-tokens |
| 49 | + SpeechContext speechContext = SpeechContext.newBuilder().addPhrases("$TIME").build(); |
| 50 | + |
| 51 | + // RecognitionConfig: to configure your encoding and sample_rate_hertz, see: |
| 52 | + // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#recognitionconfig |
| 53 | + RecognitionConfig config = |
| 54 | + RecognitionConfig.newBuilder() |
| 55 | + .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) |
| 56 | + .setSampleRateHertz(8000) |
| 57 | + .setLanguageCode("en-US") |
| 58 | + .addSpeechContexts(speechContext) |
| 59 | + .build(); |
| 60 | + |
| 61 | + // Set the path to your audio file |
| 62 | + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build(); |
| 63 | + |
| 64 | + // Build the request |
| 65 | + RecognizeRequest request = |
| 66 | + RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build(); |
| 67 | + |
| 68 | + // Perform the request |
| 69 | + RecognizeResponse response = speechClient.recognize(request); |
| 70 | + |
| 71 | + for (SpeechRecognitionResult result : response.getResultsList()) { |
| 72 | + // First alternative is the most probable result |
| 73 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 74 | + System.out.printf("Transcript: %s\n", alternative.getTranscript()); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +// [END speech_context_classes] |
0 commit comments