2929import com .google .cloud .speech .v1p1beta1 .RecognizeResponse ;
3030import com .google .cloud .speech .v1p1beta1 .SpeakerDiarizationConfig ;
3131import com .google .cloud .speech .v1p1beta1 .SpeechClient ;
32-
3332import com .google .cloud .speech .v1p1beta1 .SpeechRecognitionAlternative ;
3433import com .google .cloud .speech .v1p1beta1 .SpeechRecognitionResult ;
3534import com .google .cloud .speech .v1p1beta1 .WordInfo ;
3635import com .google .protobuf .ByteString ;
37-
3836import java .nio .file .Files ;
3937import java .nio .file .Path ;
4038import 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 ("\n Speaker %d: %s" ,
198- wordInfo .getSpeakerTag (),
199- wordInfo .getWord ()));
197+ String .format ("\n Speaker %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 ("\n Speaker %d: %s" ,
268- wordInfo .getSpeakerTag (),
269- wordInfo .getWord ()));
267+ String .format ("\n Speaker %d: %s" , wordInfo .getSpeakerTag (), wordInfo .getWord ()));
270268 currentSpeakerTag = wordInfo .getSpeakerTag ();
271269 }
272270 }
0 commit comments