def ngrams(sequence, n=2):
    """Returns all ngram tokens in an input sequence, for a specified n.
    E.g. ngrams(['A', 'B', 'A', 'B', 'D'], n=2) yields
    ('A', 'B'), ('B', 'A'), ('A', 'B'), ('B', 'D')
    """
    window = []
    for item in sequence:
        window.append(item)
        if len(window) > n:
            window = window[-n:]
        if len(window) == n:
            yield tuple(window)
