def insert_special_tokens(seq1: List[str], seq2: List[str],
                          special_tokens: Set, dur) -> List[str]:
    """Inserting special tokens into MFA aligned phoneme sequence.

    MFA aligned phoneme sequences contains no special token but contains silence
    phonemes such as 'sp' and 'sil'. However, FastSpeech2 expects phoneme
    sequences containing special tokens. This function will insert special
    tokens into MFA aligned phoneme sequence.

    Args:
        seq1 (List[str]): Phoneme sequence containing special tokens.
        seq2 (List[str]): MFA aligned phoneme sequence.
        special_tokens (Set): Special token set.

    Raises:
        ValueError: Indicating an insertion failure.

    Returns:
        List[str]: MFA aligned phoneme sequence with special tokens inserted.
    """
    new_seq = []
    new_dur = []
    i, j = 0, 0
    while i < len(seq1) and j < len(seq2):
        if seq1[i] == seq2[j]:
            new_seq.append(seq1[i])
            new_dur.append(dur[j])
            i += 1
            j += 1
        else:
            if seq1[i] in special_tokens:
                # we meet a special token in seq1
                # just insert it into new_seq
                # and move i to skip it
                new_seq.append(seq1[i])
                new_dur.append(0)
                i += 1
            elif seq2[j] in SILENCE_TOKEN:
                # we meet a sp or sil in seq2
                # insert it into new_seq and
                # skip it
                assert new_seq[-1] in special_tokens
                new_dur[-1] += dur[j]
                j += 1
            else:
                # we have found out an inconsistent sample
                # phoneme sequence containing special tokens should be the same
                # as MFA aligned phoneme sequence when removing special tokens
                # and silence phonemes ('sp' and 'sil') from both two types of
                # phoneme sequences.
                raise ValueError(
                    '{} and {} are inconsistent at pos {} and {}'.format(
                        seq1, seq2, i, j))
    while i < len(seq1):
        new_seq.append(seq1[i])
        new_dur.append(0)
        i += 1
    while j < len(seq2):
        assert new_seq[-1] in special_tokens
        new_dur[-1] += dur[j]
        j += 1

    assert len(new_dur) == len(new_dur)
    return new_seq, new_dur
Inserting special tokens into MFA aligned phoneme sequence.

    MFA aligned phoneme sequences contains no special token but contains silence
    phonemes such as 'sp' and 'sil'. However, FastSpeech2 expects phoneme
    sequences containing special tokens. This function will insert special
    tokens into MFA aligned phoneme sequence.

    Args:
        seq1 (List[str]): Phoneme sequence containing special tokens.
        seq2 (List[str]): MFA aligned phoneme sequence.
        special_tokens (Set): Special token set.

    Raises:
        ValueError: Indicating an insertion failure.

    Returns:
        List[str]: MFA aligned phoneme sequence with special tokens inserted.
    
mfa/step7_gen_alignment_from_textgrid.py