import re


def preprocess_message(w):
    """
    Performs preprocessing on single message in string datatype
    """
    w = w.lower().strip()

    # Remove links and email addresses
    w = [
        word for word in w.split(' ')
        if ('@' not in word and 'html' not in word and 'http' not in word)
    ]
    w = ' '.join(w)

    # creating a space between a word and the punctuation following it
    # eg: "he is a boy." => "he is a boy ."
    # Reference:- https://stackoverflow.com/questions/3645931/python-padding-punctuation-with-white-spaces-keeping-punctuation
    w = re.sub(r"([?.!,¿])", r" \1 ", w)
    w = re.sub(r'[" "]+', " ", w)

    # replacing everything with space except (a-z, A-Z, ".", "?", "!", ",", "'")
    w = re.sub(r"[^a-zA-Z?.!,¿]+", " ", w)

    w = w.rstrip().strip()

    return w