Skip to content

Commit 42c0f42

Browse files
samples: add recognize sample with profanity filter (#376)
* feat: added a new sample * fix: fixed region tag * fix: fixed indentation * fix: renamed test duplicate * fix: fixed consecutive capital letter style error * feat: relocated newly added sample and respective test * feat: added missing imports * feat: added another missing imports * fix: harmonized exception handling * fix: harmonized exception handling * fix: redirected system output back to standard output
1 parent db1e963 commit 42c0f42

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

speech/snippets/src/main/java/com/example/speech/InfiniteStreamRecognize.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ public void onError(Throwable t) {}
266266
if (bridgingOffset > finalRequestEndTime) {
267267
bridgingOffset = finalRequestEndTime;
268268
}
269-
int chunksFromMS =
269+
int chunksFromMs =
270270
(int) Math.floor((finalRequestEndTime - bridgingOffset) / chunkTime);
271271
// chunks from MS is number of chunks to resend
272272
bridgingOffset =
273-
(int) Math.floor((lastAudioInput.size() - chunksFromMS) * chunkTime);
273+
(int) Math.floor((lastAudioInput.size() - chunksFromMs) * chunkTime);
274274
// set bridging offset for next request
275-
for (int i = chunksFromMS; i < lastAudioInput.size(); i++) {
275+
for (int i = chunksFromMs; i < lastAudioInput.size(); i++) {
276276
request =
277277
StreamingRecognizeRequest.newBuilder()
278278
.setAudioContent(lastAudioInput.get(i))
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_transcribe_with_profanity_filter_gcs]
20+
import com.google.cloud.speech.v1.RecognitionAudio;
21+
import com.google.cloud.speech.v1.RecognitionConfig;
22+
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
23+
import com.google.cloud.speech.v1.RecognizeResponse;
24+
import com.google.cloud.speech.v1.SpeechClient;
25+
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
26+
import com.google.cloud.speech.v1.SpeechRecognitionResult;
27+
import java.io.IOException;
28+
import java.util.List;
29+
30+
public class SpeechProfanityFilter {
31+
32+
public void speechProfanityFilter() throws Exception {
33+
String uriPath = "gs://cloud-samples-tests/speech/brooklyn.flac";
34+
speechProfanityFilter(uriPath);
35+
}
36+
37+
/**
38+
* Transcribe a remote audio file with multi-channel recognition
39+
*
40+
* @param gcsUri the path to the audio file
41+
*/
42+
public static void speechProfanityFilter(String gcsUri) throws Exception {
43+
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
44+
try (SpeechClient speech = SpeechClient.create()) {
45+
46+
// Configure remote file request
47+
RecognitionConfig config =
48+
RecognitionConfig.newBuilder()
49+
.setEncoding(AudioEncoding.FLAC)
50+
.setLanguageCode("en-US")
51+
.setSampleRateHertz(16000)
52+
.setProfanityFilter(true)
53+
.build();
54+
55+
// Set the remote path for the audio file
56+
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
57+
58+
// Use blocking call to get audio transcript
59+
RecognizeResponse response = speech.recognize(config, audio);
60+
List<SpeechRecognitionResult> results = response.getResultsList();
61+
62+
for (SpeechRecognitionResult result : results) {
63+
// There can be several alternative transcripts for a given chunk of speech. Just use the
64+
// first (most likely) one here.
65+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
66+
System.out.printf("Transcription: %s\n", alternative.getTranscript());
67+
}
68+
}
69+
}
70+
}
71+
// [END speech_transcribe_with_profanity_filter_gcs]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
@RunWith(JUnit4.class)
31+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
32+
public class SpeechProfanityFilterTest {
33+
private static final String AUDIO_FILE = "gs://cloud-samples-tests/speech/brooklyn.flac";
34+
private ByteArrayOutputStream bout;
35+
private PrintStream stdout;
36+
private PrintStream out;
37+
38+
@Before
39+
public void setUp() {
40+
bout = new ByteArrayOutputStream();
41+
out = new PrintStream(bout);
42+
stdout = System.out;
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(stdout);
49+
}
50+
51+
@Test
52+
public void testSpeechProfanityFilter() throws Exception {
53+
SpeechProfanityFilter.speechProfanityFilter(AUDIO_FILE);
54+
String got = bout.toString();
55+
assertThat(got).contains("how old is the Brooklyn Bridge");
56+
}
57+
}

0 commit comments

Comments
 (0)