if pad_right:
    sequence = chain(sequence, (pad_symbol,) * (n - 1))
sequence = list(sequence)
count = max(0, len(sequence) - n + 1)
return [tuple(sequence[i:i + n]) for i in range(count)]
Simple implementation of N-Gram, tf-idf and Cosine similarity in Python