def METHOD_NAME(segments, segments_dict):
    while True:
        num_merge = 0
        new_segments = deque([])
        utt_id_prev, start_prev, end_prev = segments.popleft()
        utt_ids_merged = utt_id_prev
        for utt_ids, start, end in segments:
            interval = start - end_prev
            duration = end - start_prev
            if interval < args.min_interval and duration < args.max_duration:
                # merge
                end_prev = end
                utt_ids_merged.extend(utt_ids)
                num_merge += 1
            else:
                new_segments.append((utt_ids_merged, start_prev, end_prev))
                start_prev = start
                end_prev = end
                utt_ids_merged = utt_ids
        # for last segments
        new_segments.append((utt_ids_merged, start_prev, end))
        segments = new_segments
        if num_merge == 0:
            break
    delimiter = args.delimiter
    for utt_ids, _, _ in segments:
        spk = delimiter.join(utt_ids[0].split(delimiter)[:-2])
        s = utt_ids[0].split(delimiter)[-2]
        e = utt_ids[-1].split(delimiter)[-1]
        new_utt_id = "%s" % (spk + delimiter + s + delimiter + e)
        segments_dict[new_utt_id] = (
            segments_dict[utt_ids[0]][0],
            segments_dict[utt_ids[-1]][1],
        )
        if len(utt_ids) > 1:
            for utt_id in utt_ids:
                del segments_dict[utt_id]
    return segments_dict