Пример #1
0
    def long_running_recognize(self, sample, language_code,
                               max_alternatives=None, profanity_filter=None,
                               speech_contexts=()):
        """Long-running Recognize request to Google Speech API.

        .. _long_running_recognize: https://cloud.google.com/speech/reference/\
                                    rest/v1/speech/longrunningrecognize

        See `long_running_recognize`_.

        :type sample: :class:`~google.cloud.speech.sample.Sample`
        :param sample: Instance of ``Sample`` containing audio information.

        :type language_code: str
        :param language_code: The language of the supplied audio as
                              BCP-47 language tag. Example: ``'en-US'``.

        :type max_alternatives: int
        :param max_alternatives: (Optional) Maximum number of recognition
                                 hypotheses to be returned. The server may
                                 return fewer than maxAlternatives.
                                 Valid values are 0-30. A value of 0 or 1
                                 will return a maximum of 1. Defaults to 1

        :type profanity_filter: bool
        :param profanity_filter: If True, the server will attempt to filter
                                 out profanities, replacing all but the
                                 initial character in each filtered word with
                                 asterisks, e.g. ``'f***'``. If False or
                                 omitted, profanities won't be filtered out.

        :type speech_contexts: list
        :param speech_contexts: A list of strings (max 50) containing words and
                               phrases "hints" so that the speech recognition
                               is more likely to recognize them. This can be
                               used to improve the accuracy for specific words
                               and phrases. This can also be used to add new
                               words to the vocabulary of the recognizer.

        :rtype: :class:`~google.cloud.speech.operation.Operation`
        :returns: Instance of ``Operation`` to poll for results.
        """
        config = RecognitionConfig(
            encoding=sample.encoding,
            language_code=language_code,
            max_alternatives=max_alternatives,
            profanity_filter=profanity_filter,
            sample_rate_hertz=sample.sample_rate_hertz,
            speech_contexts=[SpeechContext(phrases=speech_contexts)],
        )

        audio = RecognitionAudio(content=sample.content,
                                 uri=sample.source_uri)
        api = self._gapic_api
        operation_future = api.long_running_recognize(
            audio=audio,
            config=config,
        )

        return Operation.from_pb(operation_future.last_operation_data(), self)
Пример #2
0
    def async_recognize(self, config, audio):
        from google.longrunning.operations_pb2 import Operation

        self.config = config
        self.audio = audio
        operation = Operation()
        return operation
Пример #3
0
    def async_recognize(self,
                        sample,
                        language_code=None,
                        max_alternatives=None,
                        profanity_filter=None,
                        speech_context=None):
        """Asychronous Recognize request to Google Speech API.

        .. _async_recognize: https://cloud.google.com/speech/reference/\
                             rest/v1beta1/speech/asyncrecognize

        See `async_recognize`_.

        :type sample: :class:`~google.cloud.speech.sample.Sample`
        :param sample: Instance of ``Sample`` containing audio information.

        :type language_code: str
        :param language_code: (Optional) The language of the supplied audio as
                              BCP-47 language tag. Example: ``'en-GB'``.
                              If omitted, defaults to ``'en-US'``.

        :type max_alternatives: int
        :param max_alternatives: (Optional) Maximum number of recognition
                                 hypotheses to be returned. The server may
                                 return fewer than maxAlternatives.
                                 Valid values are 0-30. A value of 0 or 1
                                 will return a maximum of 1. Defaults to 1

        :type profanity_filter: bool
        :param profanity_filter: If True, the server will attempt to filter
                                 out profanities, replacing all but the
                                 initial character in each filtered word with
                                 asterisks, e.g. ``'f***'``. If False or
                                 omitted, profanities won't be filtered out.

        :type speech_context: list
        :param speech_context: A list of strings (max 50) containing words and
                               phrases "hints" so that the speech recognition
                               is more likely to recognize them. This can be
                               used to improve the accuracy for specific words
                               and phrases. This can also be used to add new
                               words to the vocabulary of the recognizer.

        :rtype: `~google.cloud.speech.operation.Operation`
        :returns: ``Operation`` for asynchronous request to Google Speech API.
        """
        if sample.encoding is not Encoding.LINEAR16:
            raise ValueError('Only LINEAR16 encoding is supported by '
                             'asynchronous speech requests.')

        data = _build_request_data(sample, language_code, max_alternatives,
                                   profanity_filter, speech_context)

        api_response = self.connection.api_request(
            method='POST', path='speech:asyncrecognize', data=data)

        return Operation.from_api_repr(self, api_response)
Пример #4
0
    def async_recognize(self, sample, language_code=None,
                        max_alternatives=None, profanity_filter=None,
                        speech_context=None):
        """Asychronous Recognize request to Google Speech API.

        .. _async_recognize: https://cloud.google.com/speech/reference/\
                             rest/v1beta1/speech/asyncrecognize

        See `async_recognize`_.

        :type sample: :class:`~google.cloud.speech.sample.Sample`
        :param sample: Instance of ``Sample`` containing audio information.

        :type language_code: str
        :param language_code: (Optional) The language of the supplied audio as
                              BCP-47 language tag. Example: ``'en-GB'``.
                              If omitted, defaults to ``'en-US'``.

        :type max_alternatives: int
        :param max_alternatives: (Optional) Maximum number of recognition
                                 hypotheses to be returned. The server may
                                 return fewer than maxAlternatives.
                                 Valid values are 0-30. A value of 0 or 1
                                 will return a maximum of 1. Defaults to 1

        :type profanity_filter: bool
        :param profanity_filter: If True, the server will attempt to filter
                                 out profanities, replacing all but the
                                 initial character in each filtered word with
                                 asterisks, e.g. ``'f***'``. If False or
                                 omitted, profanities won't be filtered out.

        :type speech_context: list
        :param speech_context: A list of strings (max 50) containing words and
                               phrases "hints" so that the speech recognition
                               is more likely to recognize them. This can be
                               used to improve the accuracy for specific words
                               and phrases. This can also be used to add new
                               words to the vocabulary of the recognizer.

        :rtype: :class:`~google.cloud.speech.operation.Operation`
        :returns: Instance of ``Operation`` to poll for results.
        """
        config = RecognitionConfig(
            encoding=sample.encoding, sample_rate=sample.sample_rate,
            language_code=language_code, max_alternatives=max_alternatives,
            profanity_filter=profanity_filter,
            speech_context=SpeechContext(phrases=speech_context))

        audio = RecognitionAudio(content=sample.content,
                                 uri=sample.source_uri)
        api = self._gapic_api
        response = api.async_recognize(config=config, audio=audio)

        return Operation.from_pb(response, self)
Пример #5
0
    def async_recognize(self, sample, language_code=None,
                        max_alternatives=None, profanity_filter=None,
                        speech_context=None):
        """Asychronous Recognize request to Google Speech API.

        .. _async_recognize: https://cloud.google.com/speech/reference/\
                             rest/v1beta1/speech/asyncrecognize

        See `async_recognize`_.

        :type sample: :class:`~google.cloud.speech.sample.Sample`
        :param sample: Instance of ``Sample`` containing audio information.

        :type language_code: str
        :param language_code: (Optional) The language of the supplied audio as
                              BCP-47 language tag. Example: ``'en-GB'``.
                              If omitted, defaults to ``'en-US'``.

        :type max_alternatives: int
        :param max_alternatives: (Optional) Maximum number of recognition
                                 hypotheses to be returned. The server may
                                 return fewer than maxAlternatives.
                                 Valid values are 0-30. A value of 0 or 1
                                 will return a maximum of 1. Defaults to 1

        :type profanity_filter: bool
        :param profanity_filter: If True, the server will attempt to filter
                                 out profanities, replacing all but the
                                 initial character in each filtered word with
                                 asterisks, e.g. ``'f***'``. If False or
                                 omitted, profanities won't be filtered out.

        :type speech_context: list
        :param speech_context: A list of strings (max 50) containing words and
                               phrases "hints" so that the speech recognition
                               is more likely to recognize them. This can be
                               used to improve the accuracy for specific words
                               and phrases. This can also be used to add new
                               words to the vocabulary of the recognizer.

        :rtype: `~google.cloud.speech.operation.Operation`
        :returns: ``Operation`` for asynchronous request to Google Speech API.
        """
        if sample.encoding is not Encoding.LINEAR16:
            raise ValueError('Only LINEAR16 encoding is supported by '
                             'asynchronous speech requests.')

        data = _build_request_data(sample, language_code, max_alternatives,
                                   profanity_filter, speech_context)

        api_response = self.connection.api_request(
            method='POST', path='speech:asyncrecognize', data=data)

        return Operation.from_api_repr(self, api_response)
    def long_running_recognize(self,
                               sample,
                               language_code,
                               max_alternatives=None,
                               profanity_filter=None,
                               speech_contexts=()):
        """Long-running Recognize request to Google Speech API.

        .. _long_running_recognize: https://cloud.google.com/speech/reference/\
                                    rest/v1/speech/longrunningrecognize

        See `long_running_recognize`_.

        :type sample: :class:`~google.cloud.speech.sample.Sample`
        :param sample: Instance of ``Sample`` containing audio information.

        :type language_code: str
        :param language_code: The language of the supplied audio as
                              BCP-47 language tag. Example: ``'en-US'``.

        :type max_alternatives: int
        :param max_alternatives: (Optional) Maximum number of recognition
                                 hypotheses to be returned. The server may
                                 return fewer than maxAlternatives.
                                 Valid values are 0-30. A value of 0 or 1
                                 will return a maximum of 1. Defaults to 1

        :type profanity_filter: bool
        :param profanity_filter: If True, the server will attempt to filter
                                 out profanities, replacing all but the
                                 initial character in each filtered word with
                                 asterisks, e.g. ``'f***'``. If False or
                                 omitted, profanities won't be filtered out.

        :type speech_contexts: list
        :param speech_contexts: A list of strings (max 50) containing words and
                               phrases "hints" so that the speech recognition
                               is more likely to recognize them. This can be
                               used to improve the accuracy for specific words
                               and phrases. This can also be used to add new
                               words to the vocabulary of the recognizer.

        :rtype: :class:`~google.cloud.speech.operation.Operation`
        :returns: Operation for asynchronous request to Google Speech API.
        """
        data = _build_request_data(sample, language_code, max_alternatives,
                                   profanity_filter, speech_contexts)
        api_response = self._connection.api_request(
            method='POST', path='speech:longrunningrecognize', data=data)

        operation = Operation.from_dict(api_response, self._client)
        operation.caller_metadata['request_type'] = 'LongRunningRecognize'
        return operation
Пример #7
0
    def long_running_recognize(self, config, audio):
        from google.gapic.longrunning.operations_client import OperationsClient
        from google.gax import _OperationFuture
        from google.longrunning.operations_pb2 import Operation
        from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
            LongRunningRecognizeResponse)

        self.config = config
        self.audio = audio
        operations_client = mock.Mock(spec=OperationsClient)
        operation_future = _OperationFuture(Operation(), operations_client,
                                            LongRunningRecognizeResponse, {})
        return operation_future