def intervals_to_seq(intervals, gt_intervals, gt_labels,
        n_min_frames_overlap=3):
    """
    Convert the given intervals to ground truth sequences.

    The convention of Versteegh et al. [Interspeech, 2015] is followed where
    the transcription of an interval is taken as the forced aligned phones or
    words overlapping with that interval. In the case of phones, if the left or
    right edge of the interval contains part of a phone, that phone is included
    in the transcription if the overlap corresponds to at least 30 ms or at
    least 50% of the phone's duration. The `n_min_frames_overlap` controls the
    number of frames overlap. For words, a value of 20 can be used.
    """
    seq = [[] for i in range(len(intervals))]
    for i_interval, (interval_left, interval_right) in enumerate(intervals):
        for i_unit, (gt_left, gt_right) in enumerate(gt_intervals):
            label = gt_labels[i_unit]
            if gt_right <= interval_left or gt_left >= interval_right:
                continue
            else:
                overlap = interval_right - interval_left
                if gt_left > interval_left:
                    overlap -= gt_left - interval_left
                if gt_right < interval_right:
                    overlap -= interval_right - gt_right
                # print(label, (gt_left, gt_right), i_interval, (interval_left, interval_right), interval_right - gt_right, overlap)
            duration = gt_right - gt_left
            # print(duration, float(overlap)/duration)
            if ((overlap == (interval_right - interval_left))   # entire interval is covered
                    or (overlap >= n_min_frames_overlap)        # overlap is at least 30 ms (or 200 ms)
                    or (duration != 0 and
                        float(overlap)/duration >= 0.5)   # at least 50% of unit
                    ):
                seq[i_interval].append(label)
    return seq