Skip to content

Commit e7032b3

Browse files
authored
Merge pull request #2613 from daspecster/add-speech-transcript
Use Transcript object in sync and async.
2 parents 9d9d73f + 310c2f8 commit e7032b3

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

google-cloud-speech/google/cloud/speech/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
from google.cloud.speech.client import Client
1818
from google.cloud.speech.connection import Connection
1919
from google.cloud.speech.encoding import Encoding
20+
from google.cloud.speech.transcript import Transcript

google-cloud-speech/google/cloud/speech/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from google.cloud.speech.encoding import Encoding
2424
from google.cloud.speech.operation import Operation
2525
from google.cloud.speech.sample import Sample
26+
from google.cloud.speech.transcript import Transcript
2627

2728

2829
class Client(client_module.Client):
@@ -195,7 +196,9 @@ def sync_recognize(self, sample, language_code=None,
195196
method='POST', path='speech:syncrecognize', data=data)
196197

197198
if len(api_response['results']) == 1:
198-
return api_response['results'][0]['alternatives']
199+
result = api_response['results'][0]
200+
return [Transcript.from_api_repr(alternative)
201+
for alternative in result['alternatives']]
199202
else:
200203
raise ValueError('result in api should have length 1')
201204

google-cloud-speech/google/cloud/speech/operation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _update(self, response):
124124
results = []
125125
if raw_results:
126126
for result in raw_results[0]['alternatives']:
127-
results.append(Transcript(result))
127+
results.append(Transcript.from_api_repr(result))
128128
if metadata:
129129
self._metadata = Metadata.from_api_repr(metadata)
130130

google-cloud-speech/google/cloud/speech/transcript.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,29 @@
1616

1717

1818
class Transcript(object):
19-
"""Representation of Speech Transcripts
19+
"""Representation of Speech Transcripts.
2020
21-
:type result: dict
22-
:param result: Dictionary of transcript and confidence of recognition.
21+
:type transcript: str
22+
:param transcript: String of transcribed data.
23+
24+
:type confidence: float
25+
:param confidence: The confidence estimate between 0.0 and 1.0.
2326
"""
24-
def __init__(self, result):
25-
self._transcript = result.get('transcript')
26-
self._confidence = result.get('confidence')
27+
def __init__(self, transcript, confidence):
28+
self._transcript = transcript
29+
self._confidence = confidence
30+
31+
@classmethod
32+
def from_api_repr(cls, transcript):
33+
"""Factory: construct ``Transcript`` from JSON response.
34+
35+
:type transcript: dict
36+
:param transcript: Dictionary response from the REST API.
37+
38+
:rtype: :class:`~Transcript`
39+
:returns: Instance of ``Transcript``.
40+
"""
41+
return cls(transcript['transcript'], transcript['confidence'])
2742

2843
@property
2944
def transcript(self):

google-cloud-speech/unit_tests/test_client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def test_sync_recognize_content_with_optional_parameters(self):
6767

6868
from google.cloud import speech
6969
from google.cloud.speech.sample import Sample
70+
from google.cloud.speech.transcript import Transcript
7071
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
72+
7173
_AUDIO_CONTENT = _to_bytes(self.AUDIO_CONTENT)
7274
_B64_AUDIO_CONTENT = _bytes_to_unicode(b64encode(_AUDIO_CONTENT))
7375
RETURNED = SYNC_RECOGNIZE_RESPONSE
@@ -109,12 +111,17 @@ def test_sync_recognize_content_with_optional_parameters(self):
109111
self.assertEqual(req['method'], 'POST')
110112
self.assertEqual(req['path'], 'speech:syncrecognize')
111113

112-
expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives']
113-
self.assertEqual(response, expected)
114+
alternative = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0]
115+
expected = Transcript.from_api_repr(alternative)
116+
self.assertEqual(len(response), 1)
117+
self.assertIsInstance(response[0], Transcript)
118+
self.assertEqual(response[0].transcript, expected.transcript)
119+
self.assertEqual(response[0].confidence, expected.confidence)
114120

115121
def test_sync_recognize_source_uri_without_optional_parameters(self):
116122
from google.cloud import speech
117123
from google.cloud.speech.sample import Sample
124+
from google.cloud.speech.transcript import Transcript
118125
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
119126

120127
RETURNED = SYNC_RECOGNIZE_RESPONSE
@@ -144,8 +151,12 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
144151
self.assertEqual(req['method'], 'POST')
145152
self.assertEqual(req['path'], 'speech:syncrecognize')
146153

147-
expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives']
148-
self.assertEqual(response, expected)
154+
expected = Transcript.from_api_repr(
155+
SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0])
156+
self.assertEqual(len(response), 1)
157+
self.assertIsInstance(response[0], Transcript)
158+
self.assertEqual(response[0].transcript, expected.transcript)
159+
self.assertEqual(response[0].confidence, expected.confidence)
149160

150161
def test_sync_recognize_with_empty_results(self):
151162
from google.cloud import speech

google-cloud-speech/unit_tests/test_transcript.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def _makeOne(self, *args, **kwargs):
2626
def test_ctor(self):
2727
from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE as DATA
2828
TRANSCRIPT_DATA = DATA['response']['results'][0]['alternatives'][0]
29-
transcript = self._makeOne(TRANSCRIPT_DATA)
29+
transcript = self._makeOne(TRANSCRIPT_DATA['transcript'],
30+
TRANSCRIPT_DATA['confidence'])
3031
self.assertEqual('how old is the Brooklyn Bridge',
3132
transcript.transcript)
3233
self.assertEqual(0.98267895, transcript.confidence)

0 commit comments

Comments
 (0)