Skip to content

Commit f1d9952

Browse files
averikitschlesv
andauthored
samples: update shared config (#2443)
* update shared config * Update to 1.0.13 * lint * Fix linting * lint * fix imports Co-authored-by: Les Vogel <[email protected]>
1 parent b26a8cd commit f1d9952

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
3030
import com.google.cloud.speech.v1p1beta1.SpeakerDiarizationConfig;
3131
import com.google.cloud.speech.v1p1beta1.SpeechClient;
32-
3332
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
3433
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;
3534
import com.google.cloud.speech.v1p1beta1.WordInfo;
3635
import com.google.protobuf.ByteString;
37-
3836
import java.nio.file.Files;
3937
import java.nio.file.Path;
4038
import java.nio.file.Paths;
@@ -156,14 +154,16 @@ public static void transcribeDiarization(String fileName) throws Exception {
156154
RecognitionAudio recognitionAudio =
157155
RecognitionAudio.newBuilder().setContent(ByteString.copyFrom(content)).build();
158156

159-
SpeakerDiarizationConfig speakerDiarizationConfig = SpeakerDiarizationConfig.newBuilder()
157+
SpeakerDiarizationConfig speakerDiarizationConfig =
158+
SpeakerDiarizationConfig.newBuilder()
160159
.setEnableSpeakerDiarization(true)
161160
.setMinSpeakerCount(2)
162161
.setMaxSpeakerCount(2)
163162
.build();
164163

165164
// Configure request to enable Speaker diarization
166-
RecognitionConfig config = RecognitionConfig.newBuilder()
165+
RecognitionConfig config =
166+
RecognitionConfig.newBuilder()
167167
.setEncoding(AudioEncoding.LINEAR16)
168168
.setLanguageCode("en-US")
169169
.setSampleRateHertz(8000)
@@ -175,16 +175,16 @@ public static void transcribeDiarization(String fileName) throws Exception {
175175

176176
// Speaker Tags are only included in the last result object, which has only one alternative.
177177
SpeechRecognitionAlternative alternative =
178-
recognizeResponse.getResults(
179-
recognizeResponse.getResultsCount() - 1).getAlternatives(0);
178+
recognizeResponse.getResults(recognizeResponse.getResultsCount() - 1).getAlternatives(0);
180179

181180
// The alternative is made up of WordInfo objects that contain the speaker_tag.
182181
WordInfo wordInfo = alternative.getWords(0);
183182
int currentSpeakerTag = wordInfo.getSpeakerTag();
184183

185184
// For each word, get all the words associated with one speaker, once the speaker changes,
186185
// add a new line with the new speaker and their spoken words.
187-
StringBuilder speakerWords = new StringBuilder(
186+
StringBuilder speakerWords =
187+
new StringBuilder(
188188
String.format("Speaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));
189189

190190
for (int i = 1; i < alternative.getWordsCount(); i++) {
@@ -194,9 +194,7 @@ public static void transcribeDiarization(String fileName) throws Exception {
194194
speakerWords.append(wordInfo.getWord());
195195
} else {
196196
speakerWords.append(
197-
String.format("\nSpeaker %d: %s",
198-
wordInfo.getSpeakerTag(),
199-
wordInfo.getWord()));
197+
String.format("\nSpeaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));
200198
currentSpeakerTag = wordInfo.getSpeakerTag();
201199
}
202200
}
@@ -214,7 +212,8 @@ public static void transcribeDiarization(String fileName) throws Exception {
214212
*/
215213
public static void transcribeDiarizationGcs(String gcsUri) throws Exception {
216214
try (SpeechClient speechClient = SpeechClient.create()) {
217-
SpeakerDiarizationConfig speakerDiarizationConfig = SpeakerDiarizationConfig.newBuilder()
215+
SpeakerDiarizationConfig speakerDiarizationConfig =
216+
SpeakerDiarizationConfig.newBuilder()
218217
.setEnableSpeakerDiarization(true)
219218
.setMinSpeakerCount(2)
220219
.setMaxSpeakerCount(2)
@@ -244,17 +243,18 @@ public static void transcribeDiarizationGcs(String gcsUri) throws Exception {
244243
// Speaker Tags are only included in the last result object, which has only one alternative.
245244
LongRunningRecognizeResponse longRunningRecognizeResponse = response.get();
246245
SpeechRecognitionAlternative alternative =
247-
longRunningRecognizeResponse.getResults(
248-
longRunningRecognizeResponse.getResultsCount() - 1)
249-
.getAlternatives(0);
246+
longRunningRecognizeResponse
247+
.getResults(longRunningRecognizeResponse.getResultsCount() - 1)
248+
.getAlternatives(0);
250249

251250
// The alternative is made up of WordInfo objects that contain the speaker_tag.
252251
WordInfo wordInfo = alternative.getWords(0);
253252
int currentSpeakerTag = wordInfo.getSpeakerTag();
254253

255254
// For each word, get all the words associated with one speaker, once the speaker changes,
256255
// add a new line with the new speaker and their spoken words.
257-
StringBuilder speakerWords = new StringBuilder(
256+
StringBuilder speakerWords =
257+
new StringBuilder(
258258
String.format("Speaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));
259259

260260
for (int i = 1; i < alternative.getWordsCount(); i++) {
@@ -264,9 +264,7 @@ public static void transcribeDiarizationGcs(String gcsUri) throws Exception {
264264
speakerWords.append(wordInfo.getWord());
265265
} else {
266266
speakerWords.append(
267-
String.format("\nSpeaker %d: %s",
268-
wordInfo.getSpeakerTag(),
269-
wordInfo.getWord()));
267+
String.format("\nSpeaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));
270268
currentSpeakerTag = wordInfo.getSpeakerTag();
271269
}
272270
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.cloud.speech.v1p1beta1.SpeechContext;
2626
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
2727
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;
28-
2928
import java.io.IOException;
3029

3130
public class SpeechAdaptation {

speech/snippets/src/test/java/com/example/speech/SpeechAdaptationTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.ByteArrayOutputStream;
2222
import java.io.IOException;
2323
import java.io.PrintStream;
24-
2524
import org.junit.After;
2625
import org.junit.Before;
2726
import org.junit.Test;

0 commit comments

Comments
 (0)