diff --git a/docs/speech-usage.rst b/docs/speech-usage.rst index 95e18f7a58f6..c8a01f085b4f 100644 --- a/docs/speech-usage.rst +++ b/docs/speech-usage.rst @@ -92,8 +92,8 @@ Great Britian. ... max_alternatives=2) >>> for alternative in alternatives: ... print('=' * 20) - ... print('transcript: ' + alternative['transcript']) - ... print('confidence: ' + alternative['confidence']) + ... print('transcript: ' + alternative.transcript) + ... print('confidence: ' + alternative.confidence) ==================== transcript: Hello, this is a test confidence: 0.81 @@ -114,8 +114,8 @@ Example of using the profanity filter. ... profanity_filter=True) >>> for alternative in alternatives: ... print('=' * 20) - ... print('transcript: ' + alternative['transcript']) - ... print('confidence: ' + alternative['confidence']) + ... print('transcript: ' + alternative.transcript) + ... print('confidence: ' + alternative.confidence) ==================== transcript: Hello, this is a f****** test confidence: 0.81 @@ -136,8 +136,8 @@ words to the vocabulary of the recognizer. ... speech_context=hints) >>> for alternative in alternatives: ... print('=' * 20) - ... print('transcript: ' + alternative['transcript']) - ... print('confidence: ' + alternative['confidence']) + ... print('transcript: ' + alternative.transcript) + ... print('confidence: ' + alternative.confidence) ==================== transcript: Hello, this is a test confidence: 0.81 diff --git a/speech/google/cloud/speech/__init__.py b/speech/google/cloud/speech/__init__.py index 4a9e4e4f6fc6..f1fc9250f3c9 100644 --- a/speech/google/cloud/speech/__init__.py +++ b/speech/google/cloud/speech/__init__.py @@ -17,3 +17,4 @@ from google.cloud.speech.client import Client from google.cloud.speech.connection import Connection from google.cloud.speech.encoding import Encoding +from google.cloud.speech.transcript import Transcript diff --git a/speech/google/cloud/speech/client.py b/speech/google/cloud/speech/client.py index 553927d237cd..3a708ce8391f 100644 --- a/speech/google/cloud/speech/client.py +++ b/speech/google/cloud/speech/client.py @@ -23,6 +23,7 @@ from google.cloud.speech.encoding import Encoding from google.cloud.speech.operation import Operation from google.cloud.speech.sample import Sample +from google.cloud.speech.transcript import Transcript class Client(client_module.Client): @@ -195,7 +196,9 @@ def sync_recognize(self, sample, language_code=None, method='POST', path='speech:syncrecognize', data=data) if len(api_response['results']) == 1: - return api_response['results'][0]['alternatives'] + result = api_response['results'][0] + return [Transcript.from_api_repr(alternative) + for alternative in result['alternatives']] else: raise ValueError('result in api should have length 1') diff --git a/speech/google/cloud/speech/operation.py b/speech/google/cloud/speech/operation.py index 69614b16cb7f..4ad09bd082ff 100644 --- a/speech/google/cloud/speech/operation.py +++ b/speech/google/cloud/speech/operation.py @@ -124,7 +124,7 @@ def _update(self, response): results = [] if raw_results: for result in raw_results[0]['alternatives']: - results.append(Transcript(result)) + results.append(Transcript.from_api_repr(result)) if metadata: self._metadata = Metadata.from_api_repr(metadata) diff --git a/speech/google/cloud/speech/transcript.py b/speech/google/cloud/speech/transcript.py index bbe915396c5c..2470871494e4 100644 --- a/speech/google/cloud/speech/transcript.py +++ b/speech/google/cloud/speech/transcript.py @@ -16,14 +16,29 @@ class Transcript(object): - """Representation of Speech Transcripts + """Representation of Speech Transcripts. - :type result: dict - :param result: Dictionary of transcript and confidence of recognition. + :type transcript: str + :param transcript: String of transcribed data. + + :type confidence: float + :param confidence: The confidence estimate between 0.0 and 1.0. """ - def __init__(self, result): - self._transcript = result.get('transcript') - self._confidence = result.get('confidence') + def __init__(self, transcript, confidence): + self._transcript = transcript + self._confidence = confidence + + @classmethod + def from_api_repr(cls, transcript): + """Factory: construct ``Transcript`` from JSON response. + + :type transcript: dict + :param transcript: Dictionary response from the REST API. + + :rtype: :class:`~Transcript` + :returns: Instance of ``Transcript``. + """ + return cls(transcript['transcript'], transcript['confidence']) @property def transcript(self): diff --git a/speech/unit_tests/test_client.py b/speech/unit_tests/test_client.py index e51805a81521..4d1b67e25106 100644 --- a/speech/unit_tests/test_client.py +++ b/speech/unit_tests/test_client.py @@ -67,7 +67,9 @@ def test_sync_recognize_content_with_optional_parameters(self): from google.cloud import speech from google.cloud.speech.sample import Sample + from google.cloud.speech.transcript import Transcript from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE + _AUDIO_CONTENT = _to_bytes(self.AUDIO_CONTENT) _B64_AUDIO_CONTENT = _bytes_to_unicode(b64encode(_AUDIO_CONTENT)) RETURNED = SYNC_RECOGNIZE_RESPONSE @@ -109,12 +111,17 @@ def test_sync_recognize_content_with_optional_parameters(self): self.assertEqual(req['method'], 'POST') self.assertEqual(req['path'], 'speech:syncrecognize') - expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'] - self.assertEqual(response, expected) + alternative = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0] + expected = Transcript.from_api_repr(alternative) + self.assertEqual(len(response), 1) + self.assertIsInstance(response[0], Transcript) + self.assertEqual(response[0].transcript, expected.transcript) + self.assertEqual(response[0].confidence, expected.confidence) def test_sync_recognize_source_uri_without_optional_parameters(self): from google.cloud import speech from google.cloud.speech.sample import Sample + from google.cloud.speech.transcript import Transcript from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE RETURNED = SYNC_RECOGNIZE_RESPONSE @@ -144,8 +151,12 @@ def test_sync_recognize_source_uri_without_optional_parameters(self): self.assertEqual(req['method'], 'POST') self.assertEqual(req['path'], 'speech:syncrecognize') - expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'] - self.assertEqual(response, expected) + expected = Transcript.from_api_repr( + SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0]) + self.assertEqual(len(response), 1) + self.assertIsInstance(response[0], Transcript) + self.assertEqual(response[0].transcript, expected.transcript) + self.assertEqual(response[0].confidence, expected.confidence) def test_sync_recognize_with_empty_results(self): from google.cloud import speech diff --git a/speech/unit_tests/test_transcript.py b/speech/unit_tests/test_transcript.py index b585d6e7429c..6cbf038546b4 100644 --- a/speech/unit_tests/test_transcript.py +++ b/speech/unit_tests/test_transcript.py @@ -26,7 +26,8 @@ def _makeOne(self, *args, **kwargs): def test_ctor(self): from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE as DATA TRANSCRIPT_DATA = DATA['response']['results'][0]['alternatives'][0] - transcript = self._makeOne(TRANSCRIPT_DATA) + transcript = self._makeOne(TRANSCRIPT_DATA['transcript'], + TRANSCRIPT_DATA['confidence']) self.assertEqual('how old is the Brooklyn Bridge', transcript.transcript) self.assertEqual(0.98267895, transcript.confidence)