-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: speech: add ga samples and fix some flaky tests (#2049)
- Loading branch information
Showing
3 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
speech/snippets/src/main/java/com/example/speech/TranscribeContextClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
56 changes: 56 additions & 0 deletions
56
speech/snippets/src/test/java/com/example/speech/TranscribeContextClassesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters