2424 python beta_snippets.py diarization resources/commercial_mono.wav
2525 python beta_snippets.py multi-channel resources/commercial_mono.wav
2626 python beta_snippets.py multi-language resources/multi.wav en-US es
27+ python beta_snippets.py word-level-conf resources/commercial_mono.wav
2728"""
2829
2930import argparse
@@ -240,6 +241,39 @@ def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
240241 # [END speech_transcribe_multilanguage]
241242
242243
244+ def transcribe_file_with_word_level_confidence (speech_file ):
245+ """Transcribe the given audio file synchronously with
246+ word level confidence."""
247+ # [START speech_transcribe_word_level_confidence]
248+ from google .cloud import speech_v1p1beta1 as speech
249+ client = speech .SpeechClient ()
250+
251+ # TODO(developer): Uncomment and set to a path to your audio file.
252+ # speech_file = 'path/to/file.wav'
253+
254+ with open (speech_file , 'rb' ) as audio_file :
255+ content = audio_file .read ()
256+
257+ audio = speech .types .RecognitionAudio (content = content )
258+
259+ config = speech .types .RecognitionConfig (
260+ encoding = speech .enums .RecognitionConfig .AudioEncoding .LINEAR16 ,
261+ sample_rate_hertz = 16000 ,
262+ language_code = 'en-US' ,
263+ enable_word_confidence = True )
264+
265+ response = client .recognize (config , audio )
266+
267+ for i , result in enumerate (response .results ):
268+ alternative = result .alternatives [0 ]
269+ print ('-' * 20 )
270+ print ('First alternative of result {}' .format (i ))
271+ print (u'Transcript: {}' .format (alternative .transcript ))
272+ print (u'First Word and Confidence: ({}, {})' .format (
273+ alternative .words [0 ].word , alternative .words [0 ].confidence ))
274+ # [END speech_transcribe_word_level_confidence]
275+
276+
243277if __name__ == '__main__' :
244278 parser = argparse .ArgumentParser (
245279 description = __doc__ ,
@@ -248,9 +282,11 @@ def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
248282 parser .add_argument (
249283 'path' , help = 'File for audio file to be recognized' )
250284 parser .add_argument (
251- 'first' , help = 'First language in audio file to be recognized' )
285+ 'first' , help = 'First language in audio file to be recognized' ,
286+ nargs = '?' )
252287 parser .add_argument (
253- 'second' , help = 'Second language in audio file to be recognized' )
288+ 'second' , help = 'Second language in audio file to be recognized' ,
289+ nargs = '?' )
254290
255291 args = parser .parse_args ()
256292
@@ -266,3 +302,5 @@ def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
266302 transcribe_file_with_multichannel (args .path )
267303 elif args .command == 'multi-language' :
268304 transcribe_file_with_multilanguage (args .path , args .first , args .second )
305+ elif args .command == 'word-level-conf' :
306+ transcribe_file_with_word_level_confidence (args .path )
0 commit comments