|
15 | 15 | """Long running operation representation for Google Speech API""" |
16 | 16 |
|
17 | 17 | from google.cloud._helpers import _rfc3339_to_datetime |
| 18 | +from google.cloud import operation |
18 | 19 |
|
19 | 20 |
|
20 | | -class Operation(object): |
| 21 | +class Operation(operation.Operation): |
21 | 22 | """Representation of a Google API Long-Running Operation. |
22 | 23 |
|
| 24 | + :type client: :class:`~google.cloud.speech.client.Client` |
| 25 | + :param client: Instance of speech client. |
| 26 | +
|
23 | 27 | :type name: int |
24 | 28 | :param name: ID assigned to an operation. |
25 | 29 |
|
@@ -52,31 +56,21 @@ def __init__(self, client, name, complete=False, last_updated=None, |
52 | 56 | def from_api_repr(cls, client, response): |
53 | 57 | """Factory: construct an instance from Google Speech API. |
54 | 58 |
|
| 59 | + :type client: :class:`~google.cloud.speech.client.Client` |
| 60 | + :param client: Instance of speech client. |
| 61 | +
|
55 | 62 | :type response: dict |
56 | 63 | :param response: Dictionary response from Google Speech Operations API. |
57 | 64 |
|
58 | 65 | :rtype: :class:`Operation` |
59 | 66 | :returns: Instance of `~google.cloud.speech.operations.Operation`. |
60 | 67 | """ |
61 | | - last_updated = None |
62 | | - progress_percent = 0 |
63 | | - results = None |
64 | | - start_time = None |
65 | | - |
66 | 68 | name = response['name'] |
67 | | - metadata = response.get('metadata', None) |
68 | | - |
69 | | - if metadata: |
70 | | - last_updated = _rfc3339_to_datetime(metadata.get('lastUpdateTime')) |
71 | | - start_time = _rfc3339_to_datetime(metadata.get('startTime')) |
72 | | - progress_percent = metadata.get('progressPercent') |
73 | | - |
74 | | - if response.get('response'): |
75 | | - results = response.get('response').get('results') |
76 | 69 | complete = response.get('done', False) |
77 | 70 |
|
78 | | - return cls(client, name, complete, last_updated, progress_percent, |
79 | | - results, start_time) |
| 71 | + operation_instance = cls(client, name, complete) |
| 72 | + operation_instance._update(response) |
| 73 | + return operation_instance |
80 | 74 |
|
81 | 75 | @property |
82 | 76 | def complete(self): |
@@ -150,10 +144,46 @@ def _update(self, response): |
150 | 144 | :param response: Response from Speech API Operations endpoint. |
151 | 145 | See: `speech_operations`_. |
152 | 146 | """ |
153 | | - metadata = response['metadata'] |
154 | | - results = response.get('response', {}).get('results') |
155 | | - self._last_updated = _rfc3339_to_datetime(metadata['lastUpdateTime']) |
| 147 | + metadata = response.get('metadata', None) |
| 148 | + raw_results = response.get('response', {}).get('results', None) |
| 149 | + results = [] |
| 150 | + if raw_results: |
| 151 | + for result in raw_results[0]['alternatives']: |
| 152 | + results.append(Transcript(result)) |
| 153 | + 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) |
| 158 | + |
156 | 159 | self._results = results |
157 | | - self._start_time = _rfc3339_to_datetime(metadata['startTime']) |
158 | 160 | self._complete = response.get('done', False) |
159 | | - self._progress_percent = metadata.get('progressPercent', 0) |
| 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 |
0 commit comments