if resp.error.code != code_pb2.OK:
raise RuntimeError("Server error: " + resp.error.message)
if not resp.results:
continue
// Display the top transcription
result = resp.results[0]
transcript = result.alternatives[0].transcript
// Display interim results, but with a carriage return at the end of the
// line, so subsequent lines will overwrite them.
if not result.is_final:
// If the previous result was longer than this one, we need to print
// some extra spaces to overwrite the previous result
overwrite_chars = " " * max(0, num_chars_printed - len(transcript))
sys.stdout.write(transcript + overwrite_chars + "\r")
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript)
// Exit recognition if any of the transcribed phrases could be
// one of our keywords.
if re.search(r"\b(exit|quit)\b", transcript, re.I):