@@ -145,6 +145,82 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None,
145145 raise ValueError ('More than one result or none returned from API.' )
146146
147147
148+ def _stream_requests (sample , language_code = None , max_alternatives = None ,
149+ profanity_filter = None , speech_context = None ,
150+ single_utterance = None , interim_results = None ):
151+ """Generate stream of requests from sample.
152+
153+ :type sample: :class:`~google.cloud.speech.sample.Sample`
154+ :param sample: Instance of ``Sample`` containing audio information.
155+
156+ :type language_code: str
157+ :param language_code: (Optional) The language of the supplied audio as
158+ BCP-47 language tag. Example: ``'en-GB'``.
159+ If omitted, defaults to ``'en-US'``.
160+
161+ :type max_alternatives: int
162+ :param max_alternatives: (Optional) Maximum number of recognition
163+ hypotheses to be returned. The server may
164+ return fewer than maxAlternatives.
165+ Valid values are 0-30. A value of 0 or 1
166+ will return a maximum of 1. Defaults to 1
167+
168+ :type profanity_filter: bool
169+ :param profanity_filter: (Optional) If True, the server will attempt to
170+ filter out profanities, replacing all but the
171+ initial character in each filtered word with
172+ asterisks, e.g. ``'f***'``. If False or
173+ omitted, profanities won't be filtered out.
174+
175+ :type speech_context: list
176+ :param speech_context: (Optional) A list of strings (max 50) containing
177+ words and phrases "hints" so that the speech
178+ recognition is more likely to recognize them.
179+ This can be used to improve the accuracy for
180+ specific words and phrases. This can also be used to
181+ add new words to the vocabulary of the recognizer.
182+
183+ :type single_utterance: bool
184+ :param single_utterance: (Optional) If false or omitted, the recognizer
185+ will perform continuous recognition
186+ (continuing to process audio even if the user
187+ pauses speaking) until the client closes the
188+ output stream (gRPC API) or when the maximum
189+ time limit has been reached. Multiple
190+ SpeechRecognitionResults with the is_final
191+ flag set to true may be returned.
192+
193+ If true, the recognizer will detect a single
194+ spoken utterance. When it detects that the
195+ user has paused or stopped speaking, it will
196+ return an END_OF_UTTERANCE event and cease
197+ recognition. It will return no more than one
198+ SpeechRecognitionResult with the is_final flag
199+ set to true.
200+
201+ :type interim_results: bool
202+ :param interim_results: (Optional) If true, interim results (tentative
203+ hypotheses) may be returned as they become
204+ available (these interim results are indicated
205+ with the is_final=false flag). If false or
206+ omitted, only is_final=true result(s) are
207+ returned.
208+ """
209+ config_request = _make_streaming_request (
210+ sample , language_code = language_code , max_alternatives = max_alternatives ,
211+ profanity_filter = profanity_filter , speech_context = speech_context ,
212+ single_utterance = single_utterance , interim_results = interim_results )
213+
214+ # The config request MUST go first and not contain any audio data.
215+ yield config_request
216+
217+ while True :
218+ data = sample .content .read (sample .chunk_size )
219+ if not data :
220+ break
221+ yield StreamingRecognizeRequest (audio_content = data )
222+
223+
148224def _make_streaming_request (sample , language_code ,
149225 max_alternatives , profanity_filter ,
150226 speech_context , single_utterance ,
0 commit comments