我已经使用 houndify STT 从 .wave 文件转换了 Speech-To-Text ,但是我在将文本保存到文件时遇到了问题。如何将最终流式传输的部分转录文本保存到 python 中的文件中!
/usr/bin/env python
import wave
import houndify
import sys
import time
if __name__ == '__main__':
BUFFER_SIZE = 512
if len(sys.argv) < 4:
print "Usage: %s <client ID> <client key> <wav file> [ <more wav files> ]" % sys.argv[0]
sys.exit(0)
CLIENT_ID = sys.argv[1]
CLIENT_KEY = sys.argv[2]
class MyListener(houndify.HoundListener):
def onPartialTranscript(self, transcript):
print "Partial transcript: " + transcript
def onFinalResponse(self, response):
print "Final response: " + str(response)
def onError(self, err):
print "Error: " + str(err)
client = houndify.StreamingHoundClient(CLIENT_ID, CLIENT_KEY, "test_user")
# clientMatches = [ {
# "Expression" : '([1/100 ("can"|"could"|"will"|"would")."you"].[1/10 "please"].("turn"|"switch"|(1/100 "flip"))."on".["the"].("light"|"lights").[1/20 "for"."me"].[1/20 "please"])|([1/100 ("can"|"could"|"will"|"would")."you"].[1/10 "please"].[100 ("turn"|"switch"|(1/100 "flip"))].["the"].("light"|"lights")."on".[1/20 "for"."me"].[1/20 "please"])|((("i".("want"|"like"))|((("i".["would"])|("i\'d")).("like"|"want"))).["the"].("light"|"lights").["turned"|"switched"|("to"."go")|(1/100"flipped")]."on".[1/20"please"])"',
# "Result" : { "Intent" : "TURN_LIGHT_ON" },
for fname in sys.argv[3:]:
print "============== %s ===================" % fname
audio = wave.open(fname)
if audio.getsampwidth() != 2:
print "%s: wrong sample width (must be 16-bit)" % fname
break
if audio.getframerate() != 8000 and audio.getframerate() != 16000:
print "%s: unsupported sampling frequency (must be either 8 or 16 khz)" % fname
break
if audio.getnchannels() != 1:
print "%s: must be single channel (mono)" % fname
break
client.setSampleRate(audio.getframerate())
samples = audio.readframes(BUFFER_SIZE)
finished = False
client.start(MyListener())
while not finished:
finished = client.fill(samples)
time.sleep(0.032)
samples = audio.readframes(BUFFER_SIZE)
if len(samples) == 0:
break
client.finish()