在python的继承类中包含类?

2024-04-25 18:07:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将Jasper(https://github.com/jasperproject/jasper-client)与houndifypythonsdk集成。我试图在Jasper的Speech-to-Text引擎(https://github.com/jasperproject/jasper-client/blob/master/client/stt.py)中集成Houndify。你知道吗

问题是所有的stt引擎都被定义为抽象类的继承类,我需要为Houdify定义一个继承类。 transcribe()方法返回已转录的文本,但Houndify使用类返回文本。你知道吗

那么,如何在继承类(Houdify stt class)的方法中包含一个类(Houndify transcribe class),以便该类(Houndify transcribe class)返回的值应该由继承类的方法返回呢?你知道吗

要集成的代码:

import wave
import houndify
import sys
import time

# The code below will demonstrate how to use streaming audio through Hound
if __name__ == '__main__':
  # We'll accept WAV files but it should be straightforward to 
  # use samples from a microphone or other source

  BUFFER_SIZE = 512


  CLIENT_ID = 
  CLIENT_KEY = 

  #
  # Simplest HoundListener; just print out what we receive.
  #
  # You can use these callbacks to interact with your UI.
  #
  class MyListener(houndify.HoundListener):
    def onFinalResponse(self, response):
      transcribed = response['AllResults']
      print "\n"
      print transcribed[0]['FormattedTranscription']
    def onError(self, err):
      print "Error: " + str(err)

  client = houndify.StreamingHoundClient(CLIENT_ID, CLIENT_KEY, "test_user")

  fname = 'whatistheweatherthere.wav'
  audio = wave.open(fname)
  client.setSampleRate(audio.getframerate())
  samples = audio.readframes(BUFFER_SIZE)
  finished = False

  client.start(MyListener())
  while not finished:
    finished = client.fill(samples)
    samples = audio.readframes(BUFFER_SIZE)
    if len(samples) == 0:
      break
  client.finish()

要集成的代码: 以上短期你知道吗


Tags: to方法importclientusebufferaudioclass