Skip to content

Commit ce01e9a

Browse files
committed
Add Transcript and Metadata classes.
1 parent 92b2b89 commit ce01e9a

File tree

11 files changed

+260
-92
lines changed

11 files changed

+260
-92
lines changed

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@
174174
speech-usage
175175
Client <speech-client>
176176
speech-encoding
177+
speech-metadata
177178
speech-operation
179+
speech-transcript
178180

179181
.. toctree::
180182
:maxdepth: 0

docs/speech-metadata.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Speech Metadata
2+
===============
3+
4+
.. automodule:: google.cloud.speech.metadata
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/speech-transcript.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Speech Transcript
2+
=================
3+
4+
.. automodule:: google.cloud.speech.transcript
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
"""Metadata representation from Google Speech API"""
16+
17+
from google.cloud._helpers import _rfc3339_to_datetime
18+
19+
20+
class Metadata(object):
21+
"""Representation of metadata from a Google Speech API Operation.
22+
23+
:type last_update: datetime
24+
:param last_update: When the Speech operation was last updated.
25+
26+
:type start_time: datetime
27+
:param start_time: When the Speech operation was started.
28+
29+
:type progress_percent: int
30+
:param progress_percent: Percentage of operation that has been completed.
31+
"""
32+
def __init__(self, last_update, start_time, progress_percent):
33+
self._last_update = last_update
34+
self._start_time = start_time
35+
self._progress_percent = progress_percent
36+
37+
@classmethod
38+
def from_api_repr(cls, response):
39+
"""Factory: construct representation of operation metadata.
40+
41+
:type response: dict
42+
:param response: Dictionary containing operation metadata.
43+
44+
:rtype: :class:`~google.cloud.speech.metadata.Metadata`
45+
:returns: Instance of operation Metadata.
46+
"""
47+
last_update = _rfc3339_to_datetime(response['lastUpdateTime'])
48+
start_time = _rfc3339_to_datetime(response['startTime'])
49+
progress_percent = response['progressPercent']
50+
51+
return cls(last_update, start_time, progress_percent)
52+
53+
@property
54+
def last_update(self):
55+
"""Last time operation was updated.
56+
57+
:rtype: datetime
58+
:returns: Datetime when operation was last updated.
59+
"""
60+
return self._last_update
61+
62+
@property
63+
def start_time(self):
64+
"""Start time of operation.
65+
66+
:rtype: datetime
67+
:returns: Datetime when operation was started.
68+
"""
69+
return self._start_time
70+
71+
@property
72+
def progress_percent(self):
73+
"""Progress percentage completed of operation.
74+
75+
:rtype: int
76+
:returns: Percentage of operation completed.
77+
"""
78+
return self._progress_percent
Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
"""Long running operation representation for Google Speech API"""
1616

17-
from google.cloud._helpers import _rfc3339_to_datetime
17+
from google.cloud.speech.metadata import Metadata
18+
from google.cloud.speech.transcript import Transcript
1819
from google.cloud import operation
1920

2021

@@ -30,27 +31,19 @@ class Operation(operation.Operation):
3031
:type complete: bool
3132
:param complete: True if operation is complete, else False.
3233
33-
:type last_updated: datetime
34-
:param last_updated: The last time the operation was updated.
35-
36-
:type progress_percent: int
37-
:param progress_percent: Percentage of operation that has been completed.
34+
:type metadata: :class:`~google.cloud.speech.metadata.Metadata`
35+
:param metadata: Instance of ``Metadata`` with operation information.
3836
3937
:type results: dict
4038
:param results: Dictionary with transcript and score of operation.
41-
42-
:type start_time: datetime
43-
:param start_time: Datetime when operation was started.
4439
"""
45-
def __init__(self, client, name, complete=False, last_updated=None,
46-
progress_percent=0, results=None, start_time=None):
40+
def __init__(self, client, name, complete=False, metadata=None,
41+
results=None):
4742
self.client = client
4843
self.name = name
4944
self._complete = complete
50-
self._last_updated = last_updated
51-
self._progress_percent = progress_percent
45+
self._metadata = metadata
5246
self._results = results
53-
self._start_time = start_time
5447

5548
@classmethod
5649
def from_api_repr(cls, client, response):
@@ -82,22 +75,13 @@ def complete(self):
8275
return self._complete
8376

8477
@property
85-
def last_updated(self):
86-
"""Operation last updated time.
87-
88-
:rtype: datetime
89-
:returns: RFC3339 last updated time of the operation.
90-
"""
91-
return self._last_updated
92-
93-
@property
94-
def progress_percent(self):
95-
"""Progress percentage of operation.
78+
def metadata(self):
79+
"""Metadata of operation.
9680
97-
:rtype: int
98-
:returns: Percentage of operation completed. [0-100]
81+
:rtype: :class:`~google.cloud.speech.metadata.Metadata`
82+
:returns: Instance of ``Metadata``.
9983
"""
100-
return self._progress_percent
84+
return self._metadata
10185

10286
@property
10387
def results(self):
@@ -108,15 +92,6 @@ def results(self):
10892
"""
10993
return self._results
11094

111-
@property
112-
def start_time(self):
113-
"""Operation start time.
114-
115-
:rtype: datetime
116-
:returns: RFC3339 start time of the operation.
117-
"""
118-
return self._start_time
119-
12095
def poll(self):
12196
"""Check if the operation has finished.
12297
@@ -151,39 +126,7 @@ def _update(self, response):
151126
for result in raw_results[0]['alternatives']:
152127
results.append(Transcript(result))
153128
if metadata:
154-
self._last_updated = _rfc3339_to_datetime(
155-
metadata['lastUpdateTime'])
156-
self._start_time = _rfc3339_to_datetime(metadata['startTime'])
157-
self._progress_percent = metadata.get('progressPercent', 0)
129+
self._metadata = Metadata.from_api_repr(metadata)
158130

159131
self._results = results
160132
self._complete = response.get('done', False)
161-
162-
163-
class Transcript(object):
164-
"""Representation of Speech Transcripts
165-
166-
:type result: dict
167-
:param result: Dictionary of transcript and confidence of recognition.
168-
"""
169-
def __init__(self, result):
170-
self._transcript = result.get('transcript')
171-
self._confidence = result.get('confidence')
172-
173-
@property
174-
def transcript(self):
175-
"""Transcript text from audio.
176-
177-
:rtype: str
178-
:returns: Text detected in audio.
179-
"""
180-
return self._transcript
181-
182-
@property
183-
def confidence(self):
184-
"""Confidence score for recognized speech.
185-
186-
:rtype: float
187-
:returns: Confidence score of recognized speech [0-1].
188-
"""
189-
return self._confidence
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
"""Transcript representation for Google Speech API"""
16+
17+
18+
class Transcript(object):
19+
"""Representation of Speech Transcripts
20+
21+
:type result: dict
22+
:param result: Dictionary of transcript and confidence of recognition.
23+
"""
24+
def __init__(self, result):
25+
self._transcript = result.get('transcript')
26+
self._confidence = result.get('confidence')
27+
28+
@property
29+
def transcript(self):
30+
"""Transcript text from audio.
31+
32+
:rtype: str
33+
:returns: Text detected in audio.
34+
"""
35+
return self._transcript
36+
37+
@property
38+
def confidence(self):
39+
"""Confidence score for recognized speech.
40+
41+
:rtype: float
42+
:returns: Confidence score of recognized speech [0-1].
43+
"""
44+
return self._confidence

speech/unit_tests/test_client.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_sync_recognize_content_with_optional_parameters(self):
4141
import base64
4242
from google.cloud._helpers import _to_bytes
4343
from google.cloud.speech.encoding import Encoding
44-
from unit_tests.speech._fixtures import SYNC_RECOGNIZE_RESPONSE
44+
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
4545

4646
_AUDIO_CONTENT = _to_bytes('/9j/4QNURXhpZgAASUkq')
4747
_B64_AUDIO_CONTENT = base64.b64encode(_AUDIO_CONTENT)
@@ -88,13 +88,8 @@ def test_sync_recognize_content_with_optional_parameters(self):
8888
self.assertEqual(response, expected)
8989

9090
def test_sync_recognize_source_uri_without_optional_parameters(self):
91-
<<<<<<< b7f004e00f1ee021b0cb7ac9261fcc6c021dfacb:speech/unit_tests/test_client.py
9291
from google.cloud.speech.client import Encoding
9392
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
94-
=======
95-
from google.cloud.speech.encoding import Encoding
96-
from unit_tests.speech._fixtures import SYNC_RECOGNIZE_RESPONSE
97-
>>>>>>> Add speech asynchronous recognize support.:unit_tests/speech/test_client.py
9893

9994
RETURNED = SYNC_RECOGNIZE_RESPONSE
10095
REQUEST = {
@@ -166,13 +161,8 @@ def test_sync_recognize_without_samplerate(self):
166161
None)
167162

168163
def test_sync_recognize_with_empty_results(self):
169-
<<<<<<< b7f004e00f1ee021b0cb7ac9261fcc6c021dfacb:speech/unit_tests/test_client.py
170164
from google.cloud.speech.client import Encoding
171165
from unit_tests._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE
172-
=======
173-
from google.cloud.speech.encoding import Encoding
174-
from unit_tests.speech._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE
175-
>>>>>>> Add speech asynchronous recognize support.:unit_tests/speech/test_client.py
176166

177167
credentials = _Credentials()
178168
client = self._makeOne(credentials=credentials)
@@ -183,7 +173,7 @@ def test_sync_recognize_with_empty_results(self):
183173
self.SAMPLE_RATE)
184174

185175
def test_async_recognize(self):
186-
from unit_tests.speech._fixtures import ASYNC_RECOGNIZE_RESPONSE
176+
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
187177
from google.cloud.speech.encoding import Encoding
188178
from google.cloud.speech.operation import Operation
189179
RETURNED = ASYNC_RECOGNIZE_RESPONSE
@@ -199,6 +189,7 @@ def test_async_recognize(self):
199189
self.SAMPLE_RATE)
200190
self.assertIsInstance(operation, Operation)
201191
self.assertFalse(operation.complete)
192+
self.assertIsNone(operation.metadata)
202193

203194

204195
class _Credentials(object):

speech/unit_tests/test_metadata.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import unittest
16+
17+
18+
class TestMetadata(unittest.TestCase):
19+
OPERATION_ID = 123456789
20+
21+
def _getTargetClass(self):
22+
from google.cloud.speech.metadata import Metadata
23+
return Metadata
24+
25+
def _makeOne(self, *args, **kwargs):
26+
return self._getTargetClass()(*args, **kwargs)
27+
28+
def test_ctor(self):
29+
last_update = 'last_update'
30+
start_time = 'start_time'
31+
progress_percent = 23
32+
metadata = self._makeOne(last_update, start_time, progress_percent)
33+
self.assertEqual('last_update', metadata.last_update)
34+
self.assertEqual('start_time', metadata.start_time)
35+
self.assertEqual(23, metadata.progress_percent)
36+
37+
def test_from_api_repr(self):
38+
import datetime
39+
from google.cloud._helpers import _rfc3339_to_datetime
40+
from unit_tests._fixtures import OPERATION_INCOMPLETE_RESPONSE as DATA
41+
METADATA = DATA['metadata']
42+
43+
start_time = _rfc3339_to_datetime(METADATA['startTime'])
44+
last_update = _rfc3339_to_datetime(METADATA['lastUpdateTime'])
45+
metadata = self._getTargetClass().from_api_repr(METADATA)
46+
self.assertIsInstance(metadata.last_update, datetime.datetime)
47+
self.assertEqual(last_update, metadata.last_update)
48+
self.assertIsInstance(metadata.start_time, datetime.datetime)
49+
self.assertEqual(start_time, metadata.start_time)
50+
self.assertEqual(27, metadata.progress_percent)

0 commit comments

Comments
 (0)