Skip to content

Commit 4f95319

Browse files
speech: move samples out of branch (#2324)
1 parent 749a551 commit 4f95319

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_adaptation_beta]
20+
import com.google.cloud.speech.v1p1beta1.RecognitionAudio;
21+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
22+
import com.google.cloud.speech.v1p1beta1.RecognizeRequest;
23+
import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
24+
import com.google.cloud.speech.v1p1beta1.SpeechClient;
25+
import com.google.cloud.speech.v1p1beta1.SpeechContext;
26+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
27+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;
28+
29+
import java.io.IOException;
30+
31+
public class SpeechAdaptation {
32+
33+
public void speechAdaptation() throws IOException {
34+
String uriPath = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3";
35+
speechAdaptation(uriPath);
36+
}
37+
38+
public static void speechAdaptation(String uriPath) throws IOException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (SpeechClient speechClient = SpeechClient.create()) {
43+
44+
// Provides "hints" to the speech recognizer to favor specific words and phrases in the
45+
// results.
46+
// https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1p1beta1#google.cloud.speech.v1p1beta1.SpeechContext
47+
SpeechContext speechContext =
48+
SpeechContext.newBuilder().addPhrases("Brooklyn Bridge").setBoost(20.0F).build();
49+
// Configure recognition config to match your audio file.
50+
RecognitionConfig config =
51+
RecognitionConfig.newBuilder()
52+
.setEncoding(RecognitionConfig.AudioEncoding.MP3)
53+
.setSampleRateHertz(44100)
54+
.setLanguageCode("en-US")
55+
.addSpeechContexts(speechContext)
56+
.build();
57+
// Set the path to your audio file
58+
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(uriPath).build();
59+
60+
// Make the request
61+
RecognizeRequest request =
62+
RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
63+
64+
// Display the results
65+
RecognizeResponse response = speechClient.recognize(request);
66+
for (SpeechRecognitionResult result : response.getResultsList()) {
67+
// First alternative is the most probable result
68+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
69+
System.out.printf("Transcript: %s\n", alternative.getTranscript());
70+
}
71+
}
72+
}
73+
}
74+
// [END speech_adaptation_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class SpeechAdaptationTest {
34+
private static final String AUDIO_FILE = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3";
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
38+
@Before
39+
public void setUp() {
40+
bout = new ByteArrayOutputStream();
41+
out = new PrintStream(bout);
42+
System.setOut(out);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
System.setOut(null);
48+
}
49+
50+
@Test
51+
public void testTranscribeContextClasses() throws IOException {
52+
SpeechAdaptation.speechAdaptation(AUDIO_FILE);
53+
String got = bout.toString();
54+
assertThat(got).contains("Transcript:");
55+
}
56+
}

0 commit comments

Comments
 (0)