import itertools


def pad_ends(
    sequence, pad_left=True, left_pad_symbol="<s>", right_pad_symbol="</s>"
):
    """
    Pad sentence ends with start- and end-of-sentence tokens

    In speech recognition, it is important to predict the end of sentence
    and use the start of sentence to condition predictions. Typically this
    is done by adding special tokens (usually <s> and </s>) at the ends of
    each sentence. The <s> token should not be predicted, so some special
    care needs to be taken for unigrams.

    Arguments
    ---------
    sequence : iterator
        The sequence (any iterable type) to pad.
    pad_left : bool
        Whether to pad on the left side as well. True by default.
    left_pad_symbol : any
        The token to use for left side padding. "<s>" by default.
    right_pad_symbol : any
        The token to use for right side padding. "</s>" by deault.

    Returns
    -------
    generator
        A generator that yields the padded sequence.

    Example
    -------
    >>> for token in pad_ends(["Speech", "Brain"]):
    ...     print(token)
    <s>
    Speech
    Brain
    </s>

    """
    if pad_left:
        return itertools.chain(
            (left_pad_symbol,), tuple(sequence), (right_pad_symbol,)
        )
    else:
        return itertools.chain(tuple(sequence), (right_pad_symbol,))