def listen_print_loop(responses, print_locally=True, sock=None):
    num_chars_printed = 0
    for response in responses:
        if (not response.results):
            continue
        result = response.results[0]
        if (not result.alternatives):
            continue
        transcript = result.alternatives[0].transcript
        overwrite_chars = (' ' * (num_chars_printed - len(transcript)))
        if (sock is not None):
            sock.send(bytes(transcript, 'utf-8'))
        if print_locally:
            if (not result.is_final):
                stdout.write(((transcript + overwrite_chars) + '\r'))
                stdout.flush()
                num_chars_printed = len(transcript)
            else:
                print((transcript + overwrite_chars))
                num_chars_printed = 0
def listen_print_loop(responses, print_locally=True, sock=None):
    num_chars_printed = 0
    for response in responses:
        if (not response.results):
            continue
        result = response.results[0]
        if (not result.alternatives):
            continue
        transcript = result.alternatives[0].transcript
        overwrite_chars = (' ' * (num_chars_printed - len(transcript)))
        if (sock is not None):
            sock.send(bytes(transcript, 'utf-8'))
        if print_locally:
            if (not result.is_final):
                stdout.write(((transcript + overwrite_chars) + '\r'))
                stdout.flush()
                num_chars_printed = len(transcript)
            else:
                print((transcript + overwrite_chars))
                num_chars_printed = 0