2020Example usage:
2121 python beta_snippets.py enhanced-model resources/commercial_mono.wav
2222 python beta_snippets.py metadata resources/commercial_mono.wav
23+ python beta_snippets.py punctuation resources/commercial_mono.wav
2324"""
2425
2526import argparse
@@ -99,6 +100,32 @@ def transcribe_file_with_metadata(path):
99100# [END speech_transcribe_file_with_metadata]
100101
101102
103+ # [START speech_transcribe_file_with_auto_punctuation]
104+ def transcribe_file_with_auto_punctuation (path ):
105+ """Transcribe the given audio file with auto punctuation enabled."""
106+ client = speech .SpeechClient ()
107+
108+ with io .open (path , 'rb' ) as audio_file :
109+ content = audio_file .read ()
110+
111+ audio = speech .types .RecognitionAudio (content = content )
112+ config = speech .types .RecognitionConfig (
113+ encoding = speech .enums .RecognitionConfig .AudioEncoding .LINEAR16 ,
114+ sample_rate_hertz = 8000 ,
115+ language_code = 'en-US' ,
116+ # Enable automatic punctuation
117+ enable_automatic_punctuation = True )
118+
119+ response = client .recognize (config , audio )
120+
121+ for i , result in enumerate (response .results ):
122+ alternative = result .alternatives [0 ]
123+ print ('-' * 20 )
124+ print ('First alternative of result {}' .format (i ))
125+ print ('Transcript: {}' .format (alternative .transcript ))
126+ # [END speech_transcribe_file_with_auto_punctuation]
127+
128+
102129if __name__ == '__main__' :
103130 parser = argparse .ArgumentParser (
104131 description = __doc__ ,
@@ -113,3 +140,5 @@ def transcribe_file_with_metadata(path):
113140 transcribe_file_with_enhanced_model (args .path )
114141 elif args .command == 'metadata' :
115142 transcribe_file_with_metadata (args .path )
143+ elif args .command == 'punctuation' :
144+ transcribe_file_with_auto_punctuation (args .path )
0 commit comments