コード例 #1
0
def test_context():
    context = SpeechContext()

    # test is_speech
    assert not context.is_speech
    context.is_speech = True
    assert context.is_speech

    # test is_active
    assert not context.is_active
    context.is_active = True
    assert context.is_active

    # test transcript
    assert not context.transcript
    context.transcript = "this is a test"
    assert context.transcript

    # test confidence
    assert context.confidence == 0.0
    context.confidence = 1.0
    assert context.confidence == 1.0

    # test reset
    context.reset()
    assert not context.is_speech
    assert not context.is_active
    assert not context.transcript
    assert context.confidence == 0.0
コード例 #2
0
    def _receive(self, context: SpeechContext) -> None:
        for response in self._client.streaming_recognize(
                self._config, self._drain()):
            for result in response.results[:1]:
                for alternative in result.alternatives[:1]:
                    context.transcript = alternative.transcript
                    context.confidence = alternative.confidence
                    if context.transcript:
                        context.event("partial_recognize")

                if result.is_final:
                    if context.transcript:
                        context.event("recognize")
                        _LOG.debug("recognize event")
                    else:
                        context.event("timeout")
                        _LOG.debug("timeout event")
コード例 #3
0
    def _receive(self, context: SpeechContext) -> None:
        self._client.receive()
        hypotheses = self._client.response.get("hypotheses")
        if hypotheses:
            hypothesis = hypotheses[0]
            context.transcript = hypothesis["transcript"]
            context.confidence = hypothesis["confidence"]
            if context.transcript:
                context.event("partial_recognize")

        if self._client.is_final:
            if context.transcript:
                context.event("recognize")
                _LOG.debug("recognize event")
            else:
                context.event("timeout")
                _LOG.debug("timeout event")
コード例 #4
0
    def _detect(self, context: SpeechContext) -> None:
        # read the full contents of the encode window and add the batch dimension
        # calculate a scalar likelihood that the frame contains a keyword
        # with the detect model
        frame = self.encode_window.read_all()
        frame = np.expand_dims(frame, 0)
        posterior = self.detect_model(frame)[0][0]
        class_index = np.argmax(posterior)
        confidence = posterior[class_index]

        if confidence >= self._posterior_threshold:
            context.transcript = self.classes[class_index]
            context.confidence = confidence
            context.event("recognize")
        else:
            context.event("timeout")
        self.reset()