import re


def preprocess(x):
    """Combine preprocessing done by CountVectorizer and the SimilarityEncoder.

    Different methods exist to compute the number of ngrams in a string:

    - Simply sum the values of a count vector, which is the ouput of a
      CountVectorizer with analyzer="char", and a specific ngram_range
    - Compute the number of ngrams using a formula (see ``get_ngram_count``)

    However, in the first case, some preprocessing is done by the
    CountVectorizer that may change the length of the string (in particular,
    stripping sequences of 2 or more whitespaces into 1). In order for the two
    methods to output similar results, this pre-processing is done upstream,
    prior to the CountVectorizer.
    """

    # preprocessing step done in ngram_similarity
    x = ' %s ' % x

    # preprocessing step done in the CountVectorizer
    _white_spaces = re.compile(r"\s\s+")

    return _white_spaces.sub(' ', x)