# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 31009 - Final Project - Data Clean
# ### Ada, Rohit, Dylan
#

import numpy as np  
import pandas as pd 
import re   
import nltk  
from nltk.corpus import stopwords           
from nltk.stem.porter import PorterStemmer
from collections import Counter  
import seaborn as sns 
import matplotlib.pyplot as plt     
from IPython.core.display import display, HTML  
import string
from tensorflow.keras.preprocessing.text import Tokenizer  
from tensorflow.keras.preprocessing.sequence import pad_sequences   
import tensorflow as tf 
from tqdm import tqdm  

dat = pd.read_csv("train.csv")

dat.head()


# ## 1. Data Processing: 

# +
## Helper functions
def Clean_URL(text):
    ## remove URL hyper links
    url = re.compile(r'https?://\S+|www\.\S+')
    return url.sub(r'',text)

def Clean_html(text):
    ## remove html tags
    html=re.compile(r'<.*?>')
    return html.sub(r'',text)

def Clean_emoji(text):
    ## remove emojis
    pattern = re.compile("["
                          
                           u"\U0001F600-\U0001F64F"  
                 
                           u"\U0001F300-\U0001F5FF"  
                          
                           u"\U0001F680-\U0001F6FF"  
                          
                           u"\U0001F1E0-\U0001F1FF"  
                          
                           u"\U00002702-\U000027B0"
                          
                           u"\U000024C2-\U0001F251"
                           "]+", flags=re.UNICODE)
    return pattern.sub(r'', text)

## Remove punctuations in the text
def Clean_punct(text):    
    table = str.maketrans('','',string.punctuation)
    return text.translate(table)

from spellchecker import SpellChecker
spell = SpellChecker()

## Spelling Checking
def Correct_spellings(text):
    corrected = []
    misspelled = spell.unknown(text.split())
    for word in text.split():
        if word in misspelled:
            corrected.append(spell.correction(word))
        else:
            corrected.append(word)
    return " ".join(corrected)


# -

# ###  Data Clean: 

#Apply all helper functions
dat['text'] = dat['text'].apply(lambda x : Clean_URL(x))
dat['text'] = dat['text'].apply(lambda x : Clean_html(x))
dat['text'] = dat['text'].apply(lambda x : Clean_emoji(x))
dat['text'] = dat['text'].apply(lambda x : Clean_punct(x))
dat['text'] = dat['text'].apply(lambda x : Correct_spellings(x))

dat.to_csv(r'Cleaned_Train.csv', index = False)



StarcoderdataJupyterScriptsDedupFiltered