|
| 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) |
0 commit comments