use of com.google.cloud.speech.v1.SpeechContext in project java-speech by googleapis.
the class SpeechAdaptation method speechAdaptation.
public static void speechAdaptation(String uriPath) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SpeechClient speechClient = SpeechClient.create()) {
// Provides "hints" to the speech recognizer to favor specific words and phrases in the
// results.
// https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1p1beta1#google.cloud.speech.v1p1beta1.SpeechContext
SpeechContext speechContext = SpeechContext.newBuilder().addPhrases("Brooklyn Bridge").setBoost(20.0F).build();
// Configure recognition config to match your audio file.
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.MP3).setSampleRateHertz(44100).setLanguageCode("en-US").addSpeechContexts(speechContext).build();
// Set the path to your audio file
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uriPath).build();
// Make the request
RecognizeRequest request = RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
// Display the results
RecognizeResponse response = speechClient.recognize(request);
for (SpeechRecognitionResult result : response.getResultsList()) {
// First alternative is the most probable result
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript: %s\n", alternative.getTranscript());
}
}
}
use of com.google.cloud.speech.v1.SpeechContext in project java-speech by googleapis.
the class TranscribeContextClasses method transcribeContextClasses.
// Provides "hints" to the speech recognizer to favor specific classes of words in the results.
static void transcribeContextClasses(String storageUri) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SpeechClient speechClient = SpeechClient.create()) {
// SpeechContext: to configure your speech_context see:
// https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#speechcontext
// Full list of supported phrases (class tokens) here:
// https://cloud.google.com/speech-to-text/docs/class-tokens
SpeechContext speechContext = SpeechContext.newBuilder().addPhrases("$TIME").build();
// RecognitionConfig: to configure your encoding and sample_rate_hertz, see:
// https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#recognitionconfig
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.LINEAR16).setSampleRateHertz(8000).setLanguageCode("en-US").addSpeechContexts(speechContext).build();
// Set the path to your audio file
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build();
// Build the request
RecognizeRequest request = RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
// Perform the request
RecognizeResponse response = speechClient.recognize(request);
for (SpeechRecognitionResult result : response.getResultsList()) {
// First alternative is the most probable result
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript: %s\n", alternative.getTranscript());
}
}
}
Aggregations