|
| 1 | +# clean data for tfidf, doc2vec and countvectorizer |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import csv |
| 6 | +import spacy |
| 7 | +import string |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | + |
| 11 | +en = spacy.load('en_core_web_sm') |
| 12 | +sw_spacy = en.Defaults.stop_words |
| 13 | +load_model = spacy.load('en', disable=["tok2vec", "tagger", "parser", "attribute_ruler", "ner"]) |
| 14 | + |
| 15 | +emoji_pattern = re.compile("[" |
| 16 | + u"\U0001F600-\U0001F64F" # emoticons |
| 17 | + u"\U0001F300-\U0001F5FF" # symbols & pictographs |
| 18 | + u"\U0001F680-\U0001F6FF" # transport & map symbols |
| 19 | + u"\U0001F1E0-\U0001F1FF" # flags (iOS) |
| 20 | + u"\U00002702-\U000027B0" |
| 21 | + u"\U000024C2-\U0001F251" |
| 22 | + "]+", flags=re.UNICODE) |
| 23 | + |
| 24 | +def text_preprocess(x): |
| 25 | + x = x.lower() # lowercase |
| 26 | + x = ' '.join([word for word in x.split(' ') if word not in sw_spacy]) # stopwords |
| 27 | + x = x.encode('ascii', 'ignore').decode() # unicode |
| 28 | + x = re.sub(r'https*\S+', ' ', x) # url |
| 29 | + x = re.sub(r'@\S+', ' ', x) # mentions |
| 30 | + x = re.sub(r'#\S+', ' ', x) # hastags |
| 31 | + x = x.replace("'", "") # remove ticks |
| 32 | + x = re.sub('[%s]' % re.escape(string.punctuation), ' ', x) # punctuation |
| 33 | + x = re.sub(r'\w*\d+\w*', '', x) # numbers |
| 34 | + x = re.sub(r'\s{2,}', ' ', x) # over spaces |
| 35 | + x = emoji_pattern.sub(r'', x) # emojis |
| 36 | + x = re.sub('[^A-Za-z0-9]+', ' ', x) # special charachters |
| 37 | + x = load_model(x) |
| 38 | + x = " ".join([token.lemma_ for token in x]) |
| 39 | + |
| 40 | + return x |
| 41 | + |
| 42 | +def clean_data(df, columns_to_clean): |
| 43 | + for col in columns_to_clean: |
| 44 | + df[f"cleaned_{col}"] = df[col].progress_apply(text_preprocess) |
| 45 | + |
| 46 | + df = df.drop(['TITLE', 'DESCRIPTION', 'BULLET_POINTS', 'BRAND'], axis=1) |
| 47 | + return df |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + |
| 53 | + BASE_DIR = "/content/input" |
| 54 | + |
| 55 | + train_path = os.path.join(BASE_DIR, "train.csv") |
| 56 | + test_path = os.path.join(BASE_DIR, "train.csv") |
| 57 | + sample_submission_path = os.path.join(BASE_DIR, "sample_submission.csv") |
| 58 | + |
| 59 | + train = pd.read_csv(train_path, escapechar="\\", quoting=csv.QUOTE_NONE) |
| 60 | + train_na_free = train.fillna(value="NaN") |
| 61 | + train_cleaned = clean_data(train_na_free, ['TITLE', 'DESCRIPTION', 'BULLET_POINTS', 'BRAND']) |
| 62 | + |
| 63 | + # save cleaned train file |
| 64 | + train_cleaned.to_csv("cleaned_train.csv", index=False) |
| 65 | + |
| 66 | + test = pd.read_csv(test_path, escapechar="\\", quoting=csv.QUOTE_NONE) |
| 67 | + test_na_free = test.fillna(value="NaN") |
| 68 | + test_cleaned = clean_data(test_na_free, ['TITLE', 'DESCRIPTION', 'BULLET_POINTS', 'BRAND']) |
| 69 | + |
| 70 | + # save cleaned test file |
| 71 | + train_cleaned.to_csv("cleaned_test.csv", index=False) |
0 commit comments