|
18 | 18 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext |
19 | 19 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig |
20 | 20 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio |
| 21 | +from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import ( |
| 22 | + StreamingRecognitionConfig) |
| 23 | +from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import ( |
| 24 | + StreamingRecognizeRequest) |
| 25 | + |
21 | 26 |
|
22 | 27 | from google.cloud.speech.transcript import Transcript |
23 | 28 |
|
@@ -138,3 +143,84 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None, |
138 | 143 | for alternative in alternatives] |
139 | 144 | else: |
140 | 145 | raise ValueError('More than one result or none returned from API.') |
| 146 | + |
| 147 | + |
| 148 | +def _make_streaming_request(sample, language_code, |
| 149 | + max_alternatives, profanity_filter, |
| 150 | + speech_context, single_utterance, |
| 151 | + interim_results): |
| 152 | + """Build streaming request. |
| 153 | +
|
| 154 | + :type sample: :class:`~google.cloud.speech.sample.Sample` |
| 155 | + :param sample: Instance of ``Sample`` containing audio information. |
| 156 | +
|
| 157 | + :type language_code: str |
| 158 | + :param language_code: The language of the supplied audio as |
| 159 | + BCP-47 language tag. Example: ``'en-GB'``. |
| 160 | + If omitted, defaults to ``'en-US'``. |
| 161 | +
|
| 162 | + :type max_alternatives: int |
| 163 | + :param max_alternatives: Maximum number of recognition |
| 164 | + hypotheses to be returned. The server may |
| 165 | + return fewer than maxAlternatives. |
| 166 | + Valid values are 0-30. A value of 0 or 1 |
| 167 | + will return a maximum of 1. Defaults to 1 |
| 168 | +
|
| 169 | + :type profanity_filter: bool |
| 170 | + :param profanity_filter: If True, the server will attempt to filter |
| 171 | + out profanities, replacing all but the |
| 172 | + initial character in each filtered word with |
| 173 | + asterisks, e.g. ``'f***'``. If False or |
| 174 | + omitted, profanities won't be filtered out. |
| 175 | +
|
| 176 | + :type speech_context: list |
| 177 | + :param speech_context: A list of strings (max 50) containing words and |
| 178 | + phrases "hints" so that the speech recognition |
| 179 | + is more likely to recognize them. This can be |
| 180 | + used to improve the accuracy for specific words |
| 181 | + and phrases. This can also be used to add new |
| 182 | + words to the vocabulary of the recognizer. |
| 183 | +
|
| 184 | + :type single_utterance: bool |
| 185 | + :param single_utterance: If false or omitted, the recognizer |
| 186 | + will perform continuous recognition |
| 187 | + (continuing to process audio even if the user |
| 188 | + pauses speaking) until the client closes the |
| 189 | + output stream (gRPC API) or when the maximum |
| 190 | + time limit has been reached. Multiple |
| 191 | + SpeechRecognitionResults with the is_final |
| 192 | + flag set to true may be returned. |
| 193 | +
|
| 194 | + If true, the recognizer will detect a single |
| 195 | + spoken utterance. When it detects that the |
| 196 | + user has paused or stopped speaking, it will |
| 197 | + return an END_OF_UTTERANCE event and cease |
| 198 | + recognition. It will return no more than one |
| 199 | + SpeechRecognitionResult with the is_final flag |
| 200 | + set to true. |
| 201 | +
|
| 202 | + :type interim_results: bool |
| 203 | + :param interim_results: If true, interim results (tentative |
| 204 | + hypotheses) may be returned as they become |
| 205 | + available (these interim results are indicated |
| 206 | + with the is_final=false flag). If false or |
| 207 | + omitted, only is_final=true result(s) are |
| 208 | + returned. |
| 209 | +
|
| 210 | + :rtype: |
| 211 | + :class:`~grpc.speech.v1beta1.cloud_speech_pb2.StreamingRecognizeRequest` |
| 212 | + :returns: Instance of ``StreamingRecognizeRequest``. |
| 213 | + """ |
| 214 | + config = RecognitionConfig( |
| 215 | + encoding=sample.encoding, sample_rate=sample.sample_rate, |
| 216 | + language_code=language_code, max_alternatives=max_alternatives, |
| 217 | + profanity_filter=profanity_filter, speech_context=speech_context) |
| 218 | + |
| 219 | + streaming_config = StreamingRecognitionConfig( |
| 220 | + config=config, single_utterance=single_utterance, |
| 221 | + interim_results=interim_results) |
| 222 | + |
| 223 | + config_request = StreamingRecognizeRequest( |
| 224 | + streaming_config=streaming_config) |
| 225 | + |
| 226 | + return config_request |
0 commit comments