import re

##############################
# CLEAN TEXT
##############################

mediaPattern = r"(\<Media omitted\>)" # Because it serves no purpose
regexMedia = re.compile(mediaPattern, flags=re.M)

dateAndTimepattern = r"(\d+\/\d+\/\d+)(,)(\s)(\d+:\d+)(\s)(\w+)(\s)(-)(\s\w+)*(:)"
regexDate = re.compile(dateAndTimepattern, flags=re.M)

def cleanText(filename):
    ''' A function that cleans the chat data and saves it to a file.
        Parameters
        ----------
        filname : str
        It is the name of the chat file.
    '''
      
    chat = open(filename, 'r+',encoding='utf-8', errors='ignore')
    chatText = chat.read()
    chat.close()

    # 01/09/17, 11:34 PM - Amfa:

    
    # Removes the matches and 
    # replace them with an empty string
    
    chatText = regexMedia.sub("", chatText)
    chatText = regexDate.sub("", chatText)

    lines = []

    for line in chatText.splitlines():
        if line.strip() is not "": # If it's empty, we don't need it
            lines.append(line.strip())
    return lines

# lines = cleanText('chat.txt')
# print(lines)

