import re


def clean_str(string):
    """
    Tokenization/string cleaning for all datasets except for SST.
    Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
    """
    string = re.sub(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', "_url_", string)
    string = re.sub(r'@([^\s:]+)',"_usernaem_", string)
    emot = dict.fromkeys([':-)',':)','(:','(-:',':-))'],'_smileyFace_')
    emot.update(dict.fromkeys([':-D',':D','=D','XD','xD'],'_laughing_'))
    emot.update(dict.fromkeys([':-(',':(','):',')-:'],'_sadFace_'))
    emot.update(dict.fromkeys([':-P',':P',':-p',':p'],'_tongueOut_'))
    emot.update(dict.fromkeys([':-O',':O',':-o',':o'],'_surprised_'))
    emot.update(dict.fromkeys([':-$',':$','$:','$-:'],'_embarrassedFace_'))
    emot.update(dict.fromkeys([':-/',':/',':\\',':-\\'],'_skeptical_'))
    emot.update(dict.fromkeys([':-|',':|'],'_straightFace_'))
    emot.update(dict.fromkeys([':\'-(',':\'('],'_crying_'))
    emot.update(dict.fromkeys([':\'-)',':\')'],'_happyTears_'))
    emot.update(dict.fromkeys([';-)',';)','*-)','*)',';D'],'_wink_'))
    for key in emot:
        string = string.replace(key,emot[key])
        
    string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " , ", string)
    string = re.sub(r"!", " ! ", string)
    string = re.sub(r"\(", " \( ", string)
    string = re.sub(r"\)", " \) ", string)
    string = re.sub(r"\?", " \? ", string)
    string = re.sub(r"\s{2,}", " ", string)
    return string.strip().lower()