import re
import string


def normalize_whitespace(text, replace_with=' '):
    """Normalize whitespace."""
    text = re.sub(r'(\s)+', replace_with, text, flags=re.UNICODE)
    text = re.sub(
        r'\s*([{}])\s*'.format(string.punctuation), '\\1 ',
        text, flags=re.UNICODE
    )
    return text.strip()