0

I have converted Speech-To-Text from .wave file using houndify STT , but i have problem in saving the text into a file . how can i save the final streamed partial transcript text into a file in 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()  
Gowdham GowD
  • 31
  • 1
  • 6
  • yeah but it is streaming the transcript . i need the final transcript to be stored into a file ..how can i do that?? – Gowdham GowD Feb 27 '17 at 13:31
  • class MyListener(houndify.HoundListener): def onPartialTranscript(self, transcript): print "Partial transcript: " + transcript with open("Output.txt", "w") as text_file: print("name:" %var1 "\n pwd:" %transcript, file=text_file) def onFinalResponse(self, response): print "Final response: " + str(response) def onError(self, err): print "Error: " + str(err) – Gowdham GowD Feb 27 '17 at 13:43
  • The final transcript can be found in the response object of the finished request:     def onFinalResponse(self, response):       if "Disambiguation" in response and response["Disambiguation"]["NumToShow"] > 0:         with open('output.txt', 'w') as text_file:           text_file.write("final_transcript: {0}\n".format(response["Disambiguation"]["ChoiceData"][0]["Transcription"])) You can find more information about the fields of response object here: https://docs.houndify.com/reference/HoundServer. – James Feb 27 '17 at 18:50
  • yeah thats great! but can i know! would it returns the last value of streamed STT from wave audio file??? – Gowdham GowD Feb 28 '17 at 07:09

0 Answers0