@@ -51,52 +51,54 @@ def make_channel(host, port):
5151 return implementations .secure_channel (host , port , composite_channel )
5252
5353
54- def main (input_uri , encoding , sample_rate ):
54+ def main (input_uri , encoding , sample_rate , language_code = 'en-US' ):
5555 channel = make_channel ('speech.googleapis.com' , 443 )
5656 service = cloud_speech_pb2 .beta_create_Speech_stub (channel )
5757 # The method and parameters can be inferred from the proto from which the
5858 # grpc client lib was generated. See:
5959 # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto
60- response = service .AsyncRecognize (cloud_speech_pb2 .AsyncRecognizeRequest (
60+ operation = service .AsyncRecognize (cloud_speech_pb2 .AsyncRecognizeRequest (
6161 config = cloud_speech_pb2 .RecognitionConfig (
6262 # There are a bunch of config options you can specify. See
6363 # https://goo.gl/KPZn97 for the full list.
6464 encoding = encoding , # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
6565 sample_rate = sample_rate , # the rate in hertz
66- # See
67- # https://g.co/cloud/speech/docs/best-practices#language_support
68- # for a list of supported languages.
69- language_code = 'en-US' , # a BCP-47 language tag
66+ # See https://g.co/cloud/speech/docs/languages for a list of
67+ # supported languages.
68+ language_code = language_code , # a BCP-47 language tag
7069 ),
7170 audio = cloud_speech_pb2 .RecognitionAudio (
7271 uri = input_uri ,
7372 )
7473 ), DEADLINE_SECS )
7574
7675 # Print the longrunning operation handle.
77- print (response )
76+ print (operation )
7877
7978 # Construct a long running operation endpoint.
8079 service = operations_grpc_pb2 .beta_create_Operations_stub (channel )
8180
82- name = response .name
81+ name = operation .name
8382
8483 while True :
8584 # Give the server a few seconds to process.
8685 print ('Waiting for server processing...' )
8786 time .sleep (1 )
88- # Get the long running operation with response.
89- response = service .GetOperation (
87+ operation = service .GetOperation (
9088 operations_grpc_pb2 .GetOperationRequest (name = name ),
9189 DEADLINE_SECS )
9290
93- if response .done :
91+ if operation .done :
9492 break
9593
96- # Print the recognition results.
97- results = cloud_speech_pb2 .AsyncRecognizeResponse ()
98- response .response .Unpack (results )
99- print (results )
94+ response = cloud_speech_pb2 .AsyncRecognizeResponse ()
95+ operation .response .Unpack (response )
96+ # Print the recognition result alternatives and confidence scores.
97+ for result in response .results :
98+ print ('Result:' )
99+ for alternative in result .alternatives :
100+ print (u' ({}): {}' .format (
101+ alternative .confidence , alternative .transcript ))
100102
101103
102104def _gcs_uri (text ):
0 commit comments