Search in sources :

Example 1 with SpeechContext

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());
        }
    }
}
Also used : SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) SpeechContext(com.google.cloud.speech.v1p1beta1.SpeechContext) RecognizeRequest(com.google.cloud.speech.v1p1beta1.RecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1p1beta1.RecognizeResponse) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)

Example 2 with SpeechContext

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());
        }
    }
}
Also used : SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1.RecognitionAudio) SpeechContext(com.google.cloud.speech.v1.SpeechContext) RecognizeRequest(com.google.cloud.speech.v1.RecognizeRequest) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) RecognizeResponse(com.google.cloud.speech.v1.RecognizeResponse) SpeechRecognitionResult(com.google.cloud.speech.v1.SpeechRecognitionResult)

Aggregations

RecognitionAudio (com.google.cloud.speech.v1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)1 RecognizeRequest (com.google.cloud.speech.v1.RecognizeRequest)1 RecognizeResponse (com.google.cloud.speech.v1.RecognizeResponse)1 SpeechClient (com.google.cloud.speech.v1.SpeechClient)1 SpeechContext (com.google.cloud.speech.v1.SpeechContext)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1.SpeechRecognitionResult)1 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)1 RecognizeRequest (com.google.cloud.speech.v1p1beta1.RecognizeRequest)1 RecognizeResponse (com.google.cloud.speech.v1p1beta1.RecognizeResponse)1 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)1 SpeechContext (com.google.cloud.speech.v1p1beta1.SpeechContext)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)1