-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleaning_Script_V1.py
148 lines (112 loc) · 6.37 KB
/
Cleaning_Script_V1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pandas as pd
import nltk
import re
import numpy as np
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
import os
def pre_processing():
"""
pre_processing function
:return:
"""
try:
wordnet_lemmatizer = WordNetLemmatizer()
stopwords = set(w.rstrip() for w in open('stopwords.txt'))
df_train = pd.read_csv('training.1600000.processed.noemoticon.csv',encoding='latin-1', names=["sentiment","tweet_id","date","query","user_id","text_body"])
df_test = pd.read_csv('testdata.manual.2009.06.14.csv',encoding='latin-1', names=["sentiment","tweet_id","date","query","user_id","text_body"])
#Function to remove emoticons from text
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]+", flags=re.UNICODE)
def emoji_remover(text):
text = emoji_pattern.sub(r'', text) # no emoji
return text
#Function to remove special charactars from text
APPOSTOPHES = { "'s" : " is", "'re" : " are", "'t" : " not", "'m" : " am", "'ll" : " will", "'d" : " would", "'ve" : " have"}
contractions_re = re.compile("|".join(r'(\b%s\b)' % c for c in APPOSTOPHES.keys()))
def expand_contractions(s, APPOSTOPHES=APPOSTOPHES):
def replace(match):
return APPOSTOPHES[match.group(0)]
return contractions_re.sub(replace, s)
#Function to clean text body
def cleaning(text):
import string
exclude = set(string.punctuation)
import re
# remove new line and digits with regular expression
text = re.sub(r'\n', '', text)
text = re.sub(r'\d', '', text)
# remove patterns matching url format
url_pattern = r'((http|ftp|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?'
text = re.sub(url_pattern, ' ', text)
#word seperater
text = " ".join(re.findall('[A-Z][^A-Z]*', text))
# remove non-ascii characters
text = ''.join(character for character in text if ord(character) < 128)
# remove punctuations
text = ''.join(character for character in text if character not in exclude)
# standardize white space
text = re.sub(r'\s+', ' ', text)
# drop capitalization
text = text.lower()
#remove white space
text = text.strip()
#from textblob import TextBlob
#spelling correction
#text = str(TextBlob(text).correct())
return text
#Fuction to tekenize, lemmatize and remove stop words from text
def my_tokenizer(s):
s = s.lower() # downcase
tokens = nltk.tokenize.word_tokenize(s) # split string into words (tokens)
tokens = [t for t in tokens if len(t) > 2] # remove short words, they're probably not useful
tokens = [wordnet_lemmatizer.lemmatize(t) for t in tokens] # put words into base form
tokens = [t for t in tokens if t not in stopwords] # remove stopwords
return ' '.join(tokens)
#Deriving clean text column from twitter data
df_train['Clean_Text_Body'] = df_train['text_body'].apply(lambda x: emoji_remover(str(x)))
df_test['Clean_Text_Body'] = df_test['text_body'].apply(lambda x: emoji_remover(str(x)))
print('---- Stage 1 cleaning complete ----')
df_train['Clean_Text_Body'] = df_train['Clean_Text_Body'].apply(lambda x: expand_contractions(str(x)))
df_test['Clean_Text_Body'] = df_test['Clean_Text_Body'].apply(lambda x: expand_contractions(str(x)))
print('---- Stage 2 cleaning complete ----')
df_train['Clean_Text_Body'] = df_train['Clean_Text_Body'].apply(lambda x: cleaning(str(x)))
df_test['Clean_Text_Body'] = df_test['Clean_Text_Body'].apply(lambda x: cleaning(str(x)))
print('---- Stage 3 cleaning complete ----')
df_train['Clean_Text_Body'] = df_train['Clean_Text_Body'].apply(lambda x: my_tokenizer(str(x)))
df_test['Clean_Text_Body'] = df_test['Clean_Text_Body'].apply(lambda x: my_tokenizer(str(x)))
print('---- Stage 4 cleaning complete ----')
# Replace and remove empty rows
df_train['Clean_Text_Body'] = df_train['Clean_Text_Body'].replace('', np.nan)
df_train = df_train.dropna(how='any')
df_test['Clean_Text_Body'] = df_test['Clean_Text_Body'].replace('', np.nan)
df_test = df_test.dropna(how='any')
print('---- Stage 5 cleaning complete ----')
df_train_clean = df_train[['tweet_id','text_body','Clean_Text_Body','sentiment']]
df_test_clean = df_test[['tweet_id','text_body','Clean_Text_Body','sentiment']]
print('---- Cleaning Complete ----')
df_train_clean.to_csv('Train_Data_cleaned.csv', index=False)
print('Train Data Saved, Printing First 5 Rows ==', df_train_clean.head())
print('Length of Train Data ===', len(df_train_clean))
df_test_clean.to_csv('Test_Data_cleaned.csv', index=False)
print('Test Data Saved, Printing First 5 Rows ==', df_test_clean.head())
print('Length of Test Data ===', len(df_test_clean))
except Exception as e:
print('Exception in Running pre_processing, as:: ', e)
if __name__ == "__main__":
os.chdir(r'C:\Users\Ishpal\Desktop\Sentiment_Analysis')
pre_processing()