def ProcessData():
    try:
        f_in = open(args.ctm_edits_in)
    except:
        sys.exit('modify_ctm_edits.py: error opening ctm-edits input file {0}'.format(args.ctm_edits_in))
    try:
        text_output_handle = open(args.text_out, 'w')
    except:
        sys.exit('modify_ctm_edits.py: error opening text output file {0}'.format(args.text_out))
    try:
        segments_output_handle = open(args.segments_out, 'w')
    except:
        sys.exit('modify_ctm_edits.py: error opening segments output file {0}'.format(args.text_out))
    if (args.ctm_edits_out != None):
        try:
            ctm_edits_output_handle = open(args.ctm_edits_out, 'w')
        except:
            sys.exit('modify_ctm_edits.py: error opening ctm-edits output file {0}'.format(args.ctm_edits_out))
    first_line = f_in.readline()
    if (first_line == ''):
        sys.exit('modify_ctm_edits.py: empty input')
    split_pending_line = first_line.split()
    if (len(split_pending_line) == 0):
        sys.exit(('modify_ctm_edits.py: bad input line ' + first_line))
    cur_utterance = split_pending_line[0]
    split_lines_of_cur_utterance = []
    while True:
        if ((len(split_pending_line) == 0) or (split_pending_line[0] != cur_utterance)):
            (segments_for_utterance, deleted_segments_for_utterance) = GetSegmentsForUtterance(split_lines_of_cur_utterance)
            AccWordStatsForUtterance(split_lines_of_cur_utterance, segments_for_utterance)
            WriteSegmentsForUtterance(text_output_handle, segments_output_handle, cur_utterance, segments_for_utterance)
            if (args.ctm_edits_out != None):
                PrintDebugInfoForUtterance(ctm_edits_output_handle, split_lines_of_cur_utterance, segments_for_utterance, deleted_segments_for_utterance)
            split_lines_of_cur_utterance = []
            if (len(split_pending_line) == 0):
                break
            else:
                cur_utterance = split_pending_line[0]
        split_lines_of_cur_utterance.append(split_pending_line)
        next_line = f_in.readline()
        split_pending_line = next_line.split()
        if (len(split_pending_line) == 0):
            if (next_line != ''):
                sys.exit('modify_ctm_edits.py: got an empty or whitespace input line')
    try:
        text_output_handle.close()
        segments_output_handle.close()
        if (args.ctm_edits_out != None):
            ctm_edits_output_handle.close()
    except:
        sys.exit('modify_ctm_edits.py: error closing one or more outputs (broken pipe or full disk?)')
def ProcessData():
    try:
        f_in = open(args.ctm_edits_in)
    except:
        sys.exit('modify_ctm_edits.py: error opening ctm-edits input file {0}'.format(args.ctm_edits_in))
    try:
        text_output_handle = open(args.text_out, 'w')
    except:
        sys.exit('modify_ctm_edits.py: error opening text output file {0}'.format(args.text_out))
    try:
        segments_output_handle = open(args.segments_out, 'w')
    except:
        sys.exit('modify_ctm_edits.py: error opening segments output file {0}'.format(args.text_out))
    if (args.ctm_edits_out != None):
        try:
            ctm_edits_output_handle = open(args.ctm_edits_out, 'w')
        except:
            sys.exit('modify_ctm_edits.py: error opening ctm-edits output file {0}'.format(args.ctm_edits_out))
    first_line = f_in.readline()
    if (first_line == ):
        sys.exit('modify_ctm_edits.py: empty input')
    split_pending_line = first_line.split()
    if (len(split_pending_line) == 0):
        sys.exit(('modify_ctm_edits.py: bad input line ' + first_line))
    cur_utterance = split_pending_line[0]
    split_lines_of_cur_utterance = []
    while True:
        if ((len(split_pending_line) == 0) or (split_pending_line[0] != cur_utterance)):
            (segments_for_utterance, deleted_segments_for_utterance) = GetSegmentsForUtterance(split_lines_of_cur_utterance)
            AccWordStatsForUtterance(split_lines_of_cur_utterance, segments_for_utterance)
            WriteSegmentsForUtterance(text_output_handle, segments_output_handle, cur_utterance, segments_for_utterance)
            if (args.ctm_edits_out != None):
                PrintDebugInfoForUtterance(ctm_edits_output_handle, split_lines_of_cur_utterance, segments_for_utterance, deleted_segments_for_utterance)
            split_lines_of_cur_utterance = []
            if (len(split_pending_line) == 0):
                break
            else:
                cur_utterance = split_pending_line[0]
        split_lines_of_cur_utterance.append(split_pending_line)
        next_line = f_in.readline()
        split_pending_line = next_line.split()
        if (len(split_pending_line) == 0):
            if (next_line != ):
                sys.exit('modify_ctm_edits.py: got an empty or whitespace input line')
    try:
        text_output_handle.close()
        segments_output_handle.close()
        if (args.ctm_edits_out != None):
            ctm_edits_output_handle.close()
    except:
        sys.exit('modify_ctm_edits.py: error closing one or more outputs (broken pipe or full disk?)')