| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,6 +178,7 @@ | |||
| 178 | 178 | Client <speech-client> | |
| 179 | 179 | speech-encoding | |
| 180 | 180 | speech-operation | |
| 181 | + speech-result | ||
| 181 | 182 | speech-sample | |
| 182 | 183 | speech-alternative | |
| 183 | 184 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + Speech Result | ||
| 2 | + ============= | ||
| 3 | + | ||
| 4 | + .. automodule:: google.cloud.speech.result | ||
| 5 | + :members: | ||
| 6 | + :undoc-members: | ||
| 7 | + :show-inheritance: | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,10 +171,10 @@ speech data to possible text alternatives on the fly. | |||
| 171 | 171 | ... sample = client.sample(content=stream, | |
| 172 | 172 | ... encoding=speech.Encoding.LINEAR16, | |
| 173 | 173 | ... sample_rate=16000) | |
| 174 | - ... alternatives = list(client.streaming_recognize(sample)) | ||
| 175 | - >>> print(alternatives[0].transcript) | ||
| 174 | + ... results = list(client.streaming_recognize(sample)) | ||
| 175 | + >>> print(results[0].alternatives[0].transcript) | ||
| 176 | 176 | 'hello' | |
| 177 | - >>> print(alternatives[0].confidence) | ||
| 177 | + >>> print(results[0].alternatives[0].confidence) | ||
| 178 | 178 | 0.973458576 | |
| 179 | 179 | ||
| 180 | 180 | ||
@@ -196,10 +196,10 @@ See: `Single Utterance`_ | |||
| 196 | 196 | ... sample_rate=16000) | |
| 197 | 197 | ... responses = client.streaming_recognize(sample, | |
| 198 | 198 | ... single_utterance=True) | |
| 199 | - ... alternatives = list(responses) | ||
| 200 | - >>> print(alternatives[0].transcript) | ||
| 199 | + ... results = list(responses) | ||
| 200 | + >>> print(results[0].alternatives[0].transcript) | ||
| 201 | 201 | hello | |
| 202 | - >>> print(alternatives[0].confidence) | ||
| 202 | + >>> print(results[0].alternatives[0].confidence) | ||
| 203 | 203 | 0.96523453546 | |
| 204 | 204 | ||
| 205 | 205 | ||
@@ -214,20 +214,28 @@ If ``interim_results`` is set to :data:`True`, interim results | |||
| 214 | 214 | ... sample = client.sample(content=stream, | |
| 215 | 215 | ... encoding=speech.Encoding.LINEAR16, | |
| 216 | 216 | ... sample_rate=16000) | |
| 217 | - ... for alternatives in client.streaming_recognize(sample, | ||
| 218 | - ... interim_results=True): | ||
| 217 | + ... for results in client.streaming_recognize(sample, | ||
| 218 | + ... interim_results=True): | ||
| 219 | 219 | ... print('=' * 20) | |
| 220 | - ... print(alternatives[0].transcript) | ||
| 221 | - ... print(alternatives[0].confidence) | ||
| 220 | + ... print(results[0].alternatives[0].transcript) | ||
| 221 | + ... print(results[0].alternatives[0].confidence) | ||
| 222 | + ... print(results[0].is_final) | ||
| 223 | + ... print(results[0].stability) | ||
| 222 | 224 | ==================== | |
| 223 | 225 | 'he' | |
| 224 | 226 | None | |
| 227 | + False | ||
| 228 | + 0.113245 | ||
| 225 | 229 | ==================== | |
| 226 | 230 | 'hell' | |
| 227 | 231 | None | |
| 232 | + False | ||
| 233 | + 0.132454 | ||
| 228 | 234 | ==================== | |
| 229 | 235 | 'hello' | |
| 230 | 236 | 0.973458576 | |
| 237 | + True | ||
| 238 | + 0.982345 | ||
| 231 | 239 | ||
| 232 | 240 | ||
| 233 | 241 | .. _Single Utterance: https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#streamingrecognitionconfig | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ | |||
| 27 | 27 | from google.cloud.speech.connection import Connection | |
| 28 | 28 | from google.cloud.speech.encoding import Encoding | |
| 29 | 29 | from google.cloud.speech.operation import Operation | |
| 30 | + from google.cloud.speech.result import StreamingSpeechResult | ||
| 30 | 31 | from google.cloud.speech.sample import Sample | |
| 31 | 32 | ||
| 32 | 33 | ||
@@ -170,7 +171,8 @@ def streaming_recognize(self, sample, language_code=None, | |||
| 170 | 171 | Streaming recognition requests are limited to 1 minute of audio. | |
| 171 | 172 | See: https://cloud.google.com/speech/limits#content | |
| 172 | 173 | ||
| 173 | - Yields: list of :class:`~google.cloud.speech.alternative.Alternatives` | ||
| 174 | + Yields: Instance of | ||
| 175 | + :class:`~google.cloud.speech.result.StreamingSpeechResult` | ||
| 174 | 176 | containing results and metadata from the streaming request. | |
| 175 | 177 | ||
| 176 | 178 | :type sample: :class:`~google.cloud.speech.sample.Sample` | |
@@ -242,8 +244,7 @@ def streaming_recognize(self, sample, language_code=None, | |||
| 242 | 244 | for response in responses: | |
| 243 | 245 | for result in response.results: | |
| 244 | 246 | if result.is_final or interim_results: | |
| 245 | - yield [Alternative.from_pb(alternative) | ||
| 246 | - for alternative in result.alternatives] | ||
| 247 | + yield StreamingSpeechResult.from_pb(result) | ||
| 247 | 248 | ||
| 248 | 249 | def sync_recognize(self, sample, language_code=None, | |
| 249 | 250 | max_alternatives=None, profanity_filter=None, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,54 @@ | |||
| 1 | + # Copyright 2016 Google Inc. | ||
| 2 | + # | ||
| 3 | + # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + # you may not use this file except in compliance with the License. | ||
| 5 | + # You may obtain a copy of the License at | ||
| 6 | + # | ||
| 7 | + # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + # | ||
| 9 | + # Unless required by applicable law or agreed to in writing, software | ||
| 10 | + # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + # See the License for the specific language governing permissions and | ||
| 13 | + # limitations under the License. | ||
| 14 | + | ||
| 15 | + """Speech result representations.""" | ||
| 16 | + | ||
| 17 | + from google.cloud.speech.alternative import Alternative | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + class StreamingSpeechResult(object): | ||
| 21 | + """Streaming speech result representation. | ||
| 22 | + | ||
| 23 | + :type alternatives: list | ||
| 24 | + :param alternatives: List of | ||
| 25 | + :class:`~google.cloud.speech.alternative.Alternative`. | ||
| 26 | + | ||
| 27 | + :type is_final: bool | ||
| 28 | + :param is_final: Boolean indicator of results finality. | ||
| 29 | + | ||
| 30 | + :type stability: float | ||
| 31 | + :param stability: 0.0-1.0 stability score for the results returned. | ||
| 32 | + """ | ||
| 33 | + def __init__(self, alternatives, is_final=False, stability=0.0): | ||
| 34 | + self.alternatives = alternatives | ||
| 35 | + self.is_final = is_final | ||
| 36 | + self.stability = stability | ||
| 37 | + | ||
| 38 | + @classmethod | ||
| 39 | + def from_pb(cls, response): | ||
| 40 | + """Factory: construct instance of ``StreamingSpeechResult``. | ||
| 41 | + | ||
| 42 | + :type response: :class:`~google.cloud.grpc.speech.v1beta1\ | ||
| 43 | + .cloud_speech_pb2.StreamingRecognizeResult` | ||
| 44 | + :param response: Instance of ``StreamingRecognizeResult`` protobuf. | ||
| 45 | + | ||
| 46 | + :rtype: :class:`~google.cloud.speech.result.StreamingSpeechResult` | ||
| 47 | + :returns: Instance of ``StreamingSpeechResult``. | ||
| 48 | + """ | ||
| 49 | + alternatives = [Alternative.from_pb(alternative) | ||
| 50 | + for alternative in response.alternatives] | ||
| 51 | + is_final = response.is_final | ||
| 52 | + stability = response.stability | ||
| 53 | + return cls(alternatives=alternatives, is_final=is_final, | ||
| 54 | + stability=stability) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,7 +28,7 @@ def _make_result(alternatives=()): | |||
| 28 | 28 | ) | |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | - def _make_streaming_result(alternatives=(), is_final=True): | ||
| 31 | + def _make_streaming_result(alternatives=(), is_final=True, stability=1.0): | ||
| 32 | 32 | from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 | |
| 33 | 33 | ||
| 34 | 34 | return cloud_speech_pb2.StreamingRecognitionResult( | |
@@ -39,6 +39,7 @@ def _make_streaming_result(alternatives=(), is_final=True): | |||
| 39 | 39 | ) for alternative in alternatives | |
| 40 | 40 | ], | |
| 41 | 41 | is_final=is_final, | |
| 42 | + stability=stability, | ||
| 42 | 43 | ) | |
| 43 | 44 | ||
| 44 | 45 | ||
@@ -477,6 +478,7 @@ def test_stream_recognize_interim_results(self): | |||
| 477 | 478 | ||
| 478 | 479 | from google.cloud.speech import _gax | |
| 479 | 480 | from google.cloud.speech.encoding import Encoding | |
| 481 | + from google.cloud.speech.client import StreamingSpeechResult | ||
| 480 | 482 | ||
| 481 | 483 | stream = BytesIO(b'Some audio data...') | |
| 482 | 484 | credentials = _Credentials() | |
@@ -492,11 +494,13 @@ def test_stream_recognize_interim_results(self): | |||
| 492 | 494 | 'confidence': 0.0123456, | |
| 493 | 495 | }] | |
| 494 | 496 | first_response = _make_streaming_response( | |
| 495 | - _make_streaming_result([], is_final=False)) | ||
| 497 | + _make_streaming_result([], is_final=False, stability=0.122435)) | ||
| 496 | 498 | second_response = _make_streaming_response( | |
| 497 | - _make_streaming_result(alternatives, is_final=False)) | ||
| 499 | + _make_streaming_result(alternatives, is_final=False, | ||
| 500 | + stability=0.1432343)) | ||
| 498 | 501 | last_response = _make_streaming_response( | |
| 499 | - _make_streaming_result(alternatives, is_final=True)) | ||
| 502 | + _make_streaming_result(alternatives, is_final=True, | ||
| 503 | + stability=0.9834534)) | ||
| 500 | 504 | responses = [first_response, second_response, last_response] | |
| 501 | 505 | ||
| 502 | 506 | channel_args = [] | |
@@ -522,15 +526,28 @@ def speech_api(channel=None): | |||
| 522 | 526 | ||
| 523 | 527 | results = list(client.streaming_recognize(sample, | |
| 524 | 528 | interim_results=True)) | |
| 525 | - self.assertEqual(results[0], []) | ||
| 526 | - self.assertEqual(results[1][0].transcript, | ||
| 529 | + | ||
| 530 | + self.assertEqual(len(results), 3) | ||
| 531 | + self.assertIsInstance(results[0], StreamingSpeechResult) | ||
| 532 | + self.assertEqual(results[0].alternatives, []) | ||
| 533 | + self.assertFalse(results[0].is_final) | ||
| 534 | + self.assertEqual(results[0].stability, 0.122435) | ||
| 535 | + self.assertEqual(results[1].stability, 0.1432343) | ||
| 536 | + self.assertFalse(results[1].is_final) | ||
| 537 | + self.assertEqual(results[1].alternatives[0].transcript, | ||
| 527 | 538 | alternatives[0]['transcript']) | |
| 528 | - self.assertEqual(results[1][0].confidence, | ||
| 539 | + self.assertEqual(results[1].alternatives[0].confidence, | ||
| 529 | 540 | alternatives[0]['confidence']) | |
| 530 | - self.assertEqual(results[1][1].transcript, | ||
| 541 | + self.assertEqual(results[1].alternatives[1].transcript, | ||
| 531 | 542 | alternatives[1]['transcript']) | |
| 532 | - self.assertEqual(results[1][1].confidence, | ||
| 543 | + self.assertEqual(results[1].alternatives[1].confidence, | ||
| 533 | 544 | alternatives[1]['confidence']) | |
| 545 | + self.assertTrue(results[2].is_final) | ||
| 546 | + self.assertEqual(results[2].stability, 0.9834534) | ||
| 547 | + self.assertEqual(results[2].alternatives[0].transcript, | ||
| 548 | + alternatives[0]['transcript']) | ||
| 549 | + self.assertEqual(results[2].alternatives[0].confidence, | ||
| 550 | + alternatives[0]['confidence']) | ||
| 534 | 551 | ||
| 535 | 552 | def test_stream_recognize(self): | |
| 536 | 553 | from io import BytesIO | |
@@ -583,9 +600,9 @@ def speech_api(channel=None): | |||
| 583 | 600 | ||
| 584 | 601 | results = list(client.streaming_recognize(sample)) | |
| 585 | 602 | self.assertEqual(len(results), 1) | |
| 586 | - self.assertEqual(results[0][0].transcript, | ||
| 603 | + self.assertEqual(results[0].alternatives[0].transcript, | ||
| 587 | 604 | alternatives[0]['transcript']) | |
| 588 | - self.assertEqual(results[0][0].confidence, | ||
| 605 | + self.assertEqual(results[0].alternatives[0].confidence, | ||
| 589 | 606 | alternatives[0]['confidence']) | |
| 590 | 607 | ||
| 591 | 608 | def test_stream_recognize_no_results(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -128,15 +128,15 @@ def _make_streaming_request(self, file_obj, single_utterance=True, | |||
| 128 | 128 | interim_results=interim_results, | |
| 129 | 129 | speech_context=['hello', 'google']) | |
| 130 | 130 | ||
| 131 | - def _check_results(self, results, num_results=1): | ||
| 132 | - self.assertEqual(len(results), num_results) | ||
| 133 | - top_result = results[0] | ||
| 131 | + def _check_results(self, alternatives, num_results=1): | ||
| 132 | + self.assertEqual(len(alternatives), num_results) | ||
| 133 | + top_result = alternatives[0] | ||
| 134 | 134 | self.assertIsInstance(top_result, Alternative) | |
| 135 | 135 | self.assertEqual(top_result.transcript, | |
| 136 | 136 | 'hello ' + self.ASSERT_TEXT) | |
| 137 | 137 | self.assertGreater(top_result.confidence, 0.90) | |
| 138 | 138 | if num_results == 2: | |
| 139 | - second_alternative = results[1] | ||
| 139 | + second_alternative = alternatives[1] | ||
| 140 | 140 | self.assertIsInstance(second_alternative, Alternative) | |
| 141 | 141 | self.assertEqual(second_alternative.transcript, self.ASSERT_TEXT) | |
| 142 | 142 | self.assertIsNone(second_alternative.confidence) | |
@@ -193,7 +193,7 @@ def test_stream_recognize(self): | |||
| 193 | 193 | ||
| 194 | 194 | with open(AUDIO_FILE, 'rb') as file_obj: | |
| 195 | 195 | for results in self._make_streaming_request(file_obj): | |
| 196 | - self._check_results(results) | ||
| 196 | + self._check_results(results.alternatives) | ||
| 197 | 197 | ||
| 198 | 198 | def test_stream_recognize_interim_results(self): | |
| 199 | 199 | if not Config.USE_GAX: | |
@@ -208,12 +208,12 @@ def test_stream_recognize_interim_results(self): | |||
| 208 | 208 | interim_results=True) | |
| 209 | 209 | responses = list(recognize) | |
| 210 | 210 | for response in responses: | |
| 211 | - if response[0].transcript: | ||
| 212 | - self.assertIn(response[0].transcript, | ||
| 211 | + if response.alternatives[0].transcript: | ||
| 212 | + self.assertIn(response.alternatives[0].transcript, | ||
| 213 | 213 | extras + self.ASSERT_TEXT) | |
| 214 | 214 | ||
| 215 | 215 | self.assertGreater(len(responses), 5) | |
| 216 | - self._check_results(responses[-1]) | ||
| 216 | + self._check_results(responses[-1].alternatives) | ||
| 217 | 217 | ||
| 218 | 218 | def test_stream_recognize_single_utterance(self): | |
| 219 | 219 | if not Config.USE_GAX: | |
@@ -222,4 +222,4 @@ def test_stream_recognize_single_utterance(self): | |||
| 222 | 222 | with open(AUDIO_FILE, 'rb') as file_obj: | |
| 223 | 223 | for results in self._make_streaming_request( | |
| 224 | 224 | file_obj, single_utterance=False): | |
| 225 | - self._check_results(results) | ||
| 225 | + self._check_results(results.alternatives) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments