Skip to content

Commit

Permalink
samples: speech: add ga samples and fix some flaky tests (#2049)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnegrey authored and chingor13 committed Aug 15, 2020
1 parent 4c18563 commit f6681b9
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.speech;

// [START speech_context_classes]
import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.RecognizeRequest;
import com.google.cloud.speech.v1.RecognizeResponse;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechContext;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;

import java.io.IOException;

class TranscribeContextClasses {

void transcribeContextClasses() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String storageUri = "gs://YOUR_BUCKET_ID/path/to/your/file.wav";
transcribeContextClasses(storageUri);
}

// Provides "hints" to the speech recognizer to favor specific classes of words in the results.
static void transcribeContextClasses(String storageUri) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// 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());
}
}
}
}
// [END speech_context_classes]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.speech;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class TranscribeContextClassesTests {
private static final String AUDIO_FILE = "gs://cloud-samples-data/speech/commercial_mono.wav";
private ByteArrayOutputStream bout;
private PrintStream out;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testTranscribeContextClasses() throws IOException {
TranscribeContextClasses.transcribeContextClasses(AUDIO_FILE);
String got = bout.toString();
assertThat(got).contains("Transcript:");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,14 @@ public void tearDown() {
public void testDiarization() throws IOException {
TranscribeDiarization.transcribeDiarization(recognitionAudioFile);
String got = bout.toString();
assertThat(got).contains("Speaker 1: I'm here");
assertThat(got).contains("Speaker 2: Hi, I'd like to buy a");
assertThat(got).contains("Speaker");
}

@Test
public void testDiarizationGcs() throws IOException, ExecutionException, InterruptedException {
TranscribeDiarizationGcs.transcribeDiarizationGcs(
"gs://cloud-samples-data/speech/commercial_mono.wav");
String got = bout.toString();
assertThat(got).contains("Speaker 1: I'm here");
assertThat(got).contains("Speaker 2: Hi, I'd like to buy a");
assertThat(got).contains("Speaker");
}
}

0 comments on commit f6681b9

Please sign in to comment.