'Tokenize text_document into a sequence of character n-grams'

def _char_ngrams(self, text_document):

    text_document = self._white_spaces.sub(u'    ', text_document)
    text_len = len(text_document)
    (min_n, max_n) = self.ngram_range
    if (min_n == 1):
        ngrams = list(text_document)
        min_n += 1
    else:
        ngrams = []
    ngrams_append = ngrams.append
    for n in xrange(min_n, min((max_n + 1), (text_len + 1))):
        for i in xrange(((text_len - n) + 1)):
            ngrams_append(text_document[i:(i + n)])
    return ngrams
