def get_response(self, json_res): res = json.loads(json_res) if 'partial' in res: alternatives = [ stt_service_pb2.SpeechRecognitionAlternative( text=res['partial']) ] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=False) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks) else: words = [self.get_word_info(x) for x in res.get('result', [])] confs = [w.confidence for w in words] if len(confs) > 0: alt_conf = sum(confs) / len(confs) else: alt_conf = 0 alternatives = [ stt_service_pb2.SpeechRecognitionAlternative( text=res['text'], words=words, confidence=alt_conf) ] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=True) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks)
def get_response(self, json_res): res = json.loads(json_res) if 'partial' in res: alternatives = [ stt_service_pb2.SpeechRecognitionAlternative( text=res['partial']) ] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=False) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks) elif 'alternatives' in res: alternatives = [ self.get_alternative(x) for x in res['alternatives'] ] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=True) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks) else: alternatives = [self.get_alternative(res)] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=True) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks)
def get_alternative(self, x): words = [self.get_word_info(y) for y in x.get('result', [])] if 'confidence' in x: conf = x['confidence'] elif len(words) > 0: confs = [w.confidence for w in words] conf = sum(confs) / len(confs) else: conf = 1.0 return stt_service_pb2.SpeechRecognitionAlternative(text=x['text'], words=words, confidence=conf)
def get_response(self, json_res): res = json.loads(json_res) print(res) if 'partial' in res: alternatives = [ stt_service_pb2.SpeechRecognitionAlternative( text=res['partial']) ] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=False) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks) else: words = [self.get_word_info(x) for x in res['result']] alternatives = [ stt_service_pb2.SpeechRecognitionAlternative(text=res['text'], words=words) ] chunks = [ stt_service_pb2.SpeechRecognitionChunk( alternatives=alternatives, final=True) ] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks)