20
20
Example usage:
21
21
python beta_snippets.py enhanced-model resources/commercial_mono.wav
22
22
python beta_snippets.py metadata resources/commercial_mono.wav
23
+ python beta_snippets.py punctuation resources/commercial_mono.wav
23
24
"""
24
25
25
26
import argparse
@@ -99,6 +100,32 @@ def transcribe_file_with_metadata(path):
99
100
# [END speech_transcribe_file_with_metadata]
100
101
101
102
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
+
102
129
if __name__ == '__main__' :
103
130
parser = argparse .ArgumentParser (
104
131
description = __doc__ ,
@@ -113,3 +140,5 @@ def transcribe_file_with_metadata(path):
113
140
transcribe_file_with_enhanced_model (args .path )
114
141
elif args .command == 'metadata' :
115
142
transcribe_file_with_metadata (args .path )
143
+ elif args .command == 'punctuation' :
144
+ transcribe_file_with_auto_punctuation (args .path )
0 commit comments