@@ -68,6 +68,7 @@ public static void main(String... args) throws Exception {
6868 + "\t syncrecognize | asyncrecognize | streamrecognize | micstreamrecognize \n "
6969 + "\t | wordoffsets | auto-punctuation | stream-punctuation \n "
7070 + "\t | enhanced-model | model-selection | multi-channel | multi-language\n "
71+ + "\t | word-level-confidence\n "
7172 + "Path:\n \t A file path (ex: ./resources/audio.raw) or a URI "
7273 + "for a Cloud Storage resource (gs://...)\n " ,
7374 Recognize .class .getCanonicalName ());
@@ -127,6 +128,12 @@ public static void main(String... args) throws Exception {
127128 } else {
128129 transcribeMultiLanguage (path );
129130 }
131+ } else if (command .equals ("word-level-confidence" )) {
132+ if (path .startsWith ("gs://" )) {
133+ transcribeWordLevelConfidenceGcs (path );
134+ } else {
135+ transcribeWordLevelConfidence (path );
136+ }
130137 }
131138 }
132139
@@ -1026,4 +1033,85 @@ public static void transcribeMultiLanguageGcs(String gcsUri) throws Exception {
10261033 }
10271034 }
10281035 // [END speech_transcribe_multilanguage_gcs]
1036+
1037+ // [START speech_transcribe_word_level_confidence]
1038+ /**
1039+ * Transcribe a local audio file with word level confidence
1040+ *
1041+ * @param fileName the path to the local audio file
1042+ */
1043+ public static void transcribeWordLevelConfidence (String fileName ) throws Exception {
1044+ Path path = Paths .get (fileName );
1045+ byte [] content = Files .readAllBytes (path );
1046+
1047+ try (SpeechClient speechClient = SpeechClient .create ()) {
1048+ RecognitionAudio recognitionAudio = RecognitionAudio .newBuilder ()
1049+ .setContent (ByteString .copyFrom (content ))
1050+ .build ();
1051+
1052+ RecognitionConfig config = RecognitionConfig .newBuilder ()
1053+ .setEncoding (AudioEncoding .LINEAR16 )
1054+ .setSampleRateHertz (16000 )
1055+ .setLanguageCode ("en-US" )
1056+ .setEnableWordConfidence (true )
1057+ .build ();
1058+
1059+ RecognizeResponse recognizeResponse = speechClient .recognize (config , recognitionAudio );
1060+
1061+ for (SpeechRecognitionResult result : recognizeResponse .getResultsList ()) {
1062+ // There can be several alternative transcripts for a given chunk of speech.
1063+ // Just use the first (most likely) one here.
1064+ SpeechRecognitionAlternative alternative = result .getAlternatives (0 );
1065+ System .out .format ("Transcript : %s\n " , alternative .getTranscript ());
1066+ System .out .format (
1067+ "First Word and Confidence : %s %s \n " ,
1068+ alternative .getWords (0 ).getWord (), alternative .getWords (0 ).getConfidence ());
1069+ }
1070+ }
1071+ }
1072+ // [END speech_transcribe_word_level_confidence]
1073+
1074+ // [START speech_transcribe_word_level_confidence_gcs]
1075+ /**
1076+ * Transcribe a remote audio file with word level confidence
1077+ *
1078+ * @param gcsUri path to the remote audio file
1079+ */
1080+ public static void transcribeWordLevelConfidenceGcs (String gcsUri ) throws Exception {
1081+ try (SpeechClient speechClient = SpeechClient .create ()) {
1082+
1083+ RecognitionConfig config = RecognitionConfig .newBuilder ()
1084+ .setEncoding (AudioEncoding .FLAC )
1085+ .setSampleRateHertz (16000 )
1086+ .setLanguageCode ("en-US" )
1087+ .setEnableWordConfidence (true )
1088+ .build ();
1089+
1090+ // Set the remote path for the audio file
1091+ RecognitionAudio audio = RecognitionAudio .newBuilder ()
1092+ .setUri (gcsUri )
1093+ .build ();
1094+
1095+ // Use non-blocking call for getting file transcription
1096+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
1097+ speechClient .longRunningRecognizeAsync (config , audio );
1098+
1099+ while (!response .isDone ()) {
1100+ System .out .println ("Waiting for response..." );
1101+ Thread .sleep (10000 );
1102+ }
1103+
1104+ SpeechRecognitionResult result = response .get ().getResultsList ().get (0 );
1105+
1106+ // There can be several alternative transcripts for a given chunk of speech.
1107+ // Just use the first (most likely) one here.
1108+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
1109+
1110+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
1111+ System .out .format (
1112+ "First Word and Confidence : %s %s \n " ,
1113+ alternative .getWords (0 ).getWord (), alternative .getWords (0 ).getConfidence ());
1114+ }
1115+ }
1116+ // [END speech_transcribe_word_level_confidence_gcs]
10291117}
0 commit comments