-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpam_ham_filter.py
More file actions
264 lines (233 loc) · 9.25 KB
/
Spam_ham_filter.py
File metadata and controls
264 lines (233 loc) · 9.25 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import pandas as pd
import numpy as np
import random
import nltk
from math import log
import argparse
from nltk.corpus import stopwords
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error, accuracy_score
def preprocess_text(df):
st1 =[]
st2_flag = []
st3 = []
for idx, row in df.iterrows():
#Splitting the mailid, spam/ham and contents
strg = row[0]
words = strg.split(None, 1)[0]
st1.append(words)
#filling df1['spam_or_ham'] column
words2 = strg.split(None, 1)[1]
st2 = words2.split(None, 1)[0]
if st2 =='ham':
st2_flag.append(0)
else:
st2_flag.append(1)
d = ' '
words3 = words2.split(None, 1)[1]
st3.append(words3)
df1 = pd.DataFrame()
df1['mailid']=st1
df1['spam_or_ham']=st2_flag
df1['content']=st3
#Initializing the spam and ham words count to zero
zeros = []
for i in range(0,len(df1['mailid'])):
zeros.append(0)
df1['spam_word_count'] = zeros
df1['ham_word_count'] = zeros
return df1
def spamham_word_counter(df1):
#Ignore words with the listed parts of speech
notpos = ['PRP','IN','DT','WDT','WP','WRB','TO','MD','EX']
#Ignore the stop words
stop_words = set(stopwords.words("english"))
#calculate the occurance of the word in all mails, spam mails and ham mails.
dict_spam = {}
dict_ham = {}
dict_words = {}
for idx, row in df1.iterrows():
wrds = row['content'].split(' ')
for w, c in zip(*[iter(wrds)]*2):
if not w.isdigit():
if w not in stop_words:
w = w.lower()
tagged = nltk.pos_tag([w])
if (filter(lambda word_tag: word_tag[1] in notpos, tagged)):
continue
if w in dict_words.keys():
dict_words[w]=dict_words[w]+int(c)
else:
dict_words[w]=int(c)
if row['spam_or_ham']==1:
wrds = row['content'].split(' ')
for w, c in zip(*[iter(wrds)]*2):
if not w.isdigit():
if w not in stop_words:
w = w.lower()
tagged = nltk.pos_tag([w])
if (filter(lambda word_tag: word_tag[1] in notpos, tagged)):
continue
if w in dict_spam.keys():
dict_spam[w]=dict_spam[w]+int(c)
else:
dict_spam[w]= int(c)
else:
wrds = row['content'].split(' ')
for w, c in zip(*[iter(wrds)]*2):
if not w.isdigit():
if w not in stop_words:
w = w.lower()
tagged = nltk.pos_tag([w])
if (filter(lambda word_tag: word_tag[1] in notpos, tagged)):
continue
if w in dict_ham.keys():
dict_ham[w]=dict_ham[w]+int(c)
else:
dict_ham[w]=int(c)
break
return dict_spam, dict_ham, dict_words
def check_for_spam_words(spam_list, df1):
#filling spam_word_count
spam_word_cnt_list = []
for idx, row in df1.iterrows():
spam_word_count = 0
wrds = row['content'].split(' ')
for w, c in zip(*[iter(wrds)]*2):
if not w.isdigit():
if w.lower() in spam_list:
spam_word_count=spam_word_count+int(c)
spam_word_cnt_list.append(spam_word_count)
df1['spam_word_count']=spam_word_cnt_list
return df1
def check_for_ham_words(ham_list, df1):
#filling ham_word_count
ham_word_cnt_list = []
for idx, row in df1.iterrows():
ham_word_count = 0
wrds = row['content'].split(' ')
for w, c in zip(*[iter(wrds)]*2):
if not w.isdigit():
if w.lower() in ham_list:
ham_word_count=ham_word_count+int(c)
ham_word_cnt_list.append(ham_word_count)
df1['ham_word_count']=ham_word_cnt_list
return df1
def classifier(dict_spam, dict_ham, dict_words, Ytest_predict_name):
#word count
sum_ham_words = sum(dict_spam.values())
sum_spam_words = sum(dict_ham.values())
total_words = sum(dict_words.values())
spam_or_ham = []
#Initializing number of mails
number_of_ham_mails = 0
number_of_spam_mails = 0
total_mails = 0
ham_sum = 0
spam_sum = 0
#Ignore words with the listed parts of speech
notpos = ['PRP','IN','DT','WDT','WP','WRB','TO','MD','EX']
#Check for stop words
stop_words = set(stopwords.words("english"))
df_train = pd.read_csv("processed_trainingdata2.csv")
for idx, row in df_train.iterrows():
total_mails = total_mails+1
if row['spam_or_ham'] == 1:
number_of_spam_mails = number_of_spam_mails+1
if row['spam_or_ham'] == 0:
number_of_ham_mails = number_of_ham_mails+1
ham_probability = float(number_of_ham_mails) / float(total_mails)
spam_probability = float(number_of_spam_mails) / float(total_mails)
df_test = pd.read_csv("processed_testingdata2.csv")
for idx, row in df_test.iterrows():
wrds = row['content'].split(' ')
for w, c in zip(*[iter(wrds)]*2):
if not w.isdigit():
if w not in stop_words:
w = w.lower()
tagged = nltk.pos_tag([w])
if (filter(lambda word_tag: word_tag[1] in notpos, tagged)):
continue
hamProbability = float(1/sum_ham_words) if w not in dict_ham.keys() else float(dict_ham[w]/sum_ham_words)
spamProbability = float(1/sum_spam_words) if w not in dict_spam.keys() else float(dict_spam[w]/sum_spam_words)
if spamProbability !=0:
spam_prob = log(spamProbability , 2)
else:
spam_prob = 0
if hamProbability !=0:
ham_prob = log(hamProbability, 2)
else:
ham_prob = 0
spam_sum += (int(c) * spam_prob)
ham_sum += (int(c) * ham_prob)
ham_sum += log(ham_probability, 2)
spam_sum += log(spam_probability, 2)
if ham_sum >= spam_sum:
spam_or_ham.append(0)
else:
spam_or_ham.append(1)
y_test = df_test['spam_or_ham']
prediction_list = []
for each in spam_or_ham:
if each==1:
prediction_list.append("spam")
else:
prediction_list.append("ham")
#Copying into output file
df_test = pd.read_csv("processed_testingdata2.csv")
df_op =pd.DataFrame()
df_op['ID']=df_test['mailid']
df_op['spam/ham']=prediction_list
filename = Ytest_predict_name
df_op.to_csv(filename, index=False)
#Evaluate Accuracy
print_results(y_test, spam_or_ham)
return
def print_results(y_test, prediction):
print("Accuracy score:", accuracy_score(y_test, prediction)*100)
print("R^2:", r2_score(y_test,prediction))
rmse = np.sqrt(mean_squared_error(y_test, prediction))
print("Mean squared error:", mean_squared_error(y_test, prediction))
print("Root mean squared error: {}".format(rmse))
print("Mean absolute error:", mean_absolute_error(y_test,prediction))
return
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-f1', help='training file in csv format', required=True)
parser.add_argument('-f2', help='test file in csv format', required=True)
parser.add_argument('-o', help='output labels for the test dataset', required=True)
args = vars(parser.parse_args())
Xtrain_name = args['f1']
Xtest_name = args['f2']
Ytest_predict_name = args['o']
Xtrain = pd.read_csv(Xtrain_name)
print("preprocess training data")
df1 = preprocess_text(Xtrain)
filename = 'preprocessing1.csv'
df1.to_csv(filename, index=False)
print("Spam words list creation")
dict_spam, dict_ham, dict_words = spamham_word_counter(df1)
print("Checking for Spam words")
df_train = pd.DataFrame()
df_train = check_for_spam_words(dict_spam, df1)
filename = 'processed_trainingdata2.csv'
df_train.to_csv(filename, index=False)
df_train1 = check_for_ham_words(dict_ham, df_train)
filename = 'processed_trainingdata2.csv'
df_train1.to_csv(filename, index=False)
print("preprocess testing data")
Xtest = pd.read_csv(Xtest_name)
df3=preprocess_text(Xtest)
filename = 'preprocessing_test1.csv'
df3.to_csv(filename, index=False)
#Checking for Spam words
df_test = pd.DataFrame()
df_test = check_for_spam_words(dict_spam, df3)
filename = 'processed_testingdata2.csv'
df_test.to_csv(filename, index=False)
df_test1 = check_for_ham_words(dict_ham, df_test)
filename = 'processed_testingdata2.csv'
df_test1.to_csv(filename, index=False)
print("Classifying emails")
classifier(dict_spam, dict_ham, dict_words, Ytest_predict_name)
if __name__ == '__main__':
main()