-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpamFilter.py
177 lines (111 loc) · 5.33 KB
/
SpamFilter.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
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
import email
from itertools import *
from collections import *
import math
import unittest
import os
def load_tokens(email_path):
fileInstance = open(email_path, 'r')
messageObject = email.message_from_file(fileInstance)
EmailIterator = email.iterators.body_line_iterator(messageObject)
fileInstance.close()
for line in EmailIterator:
result = [words.lower()
for line in EmailIterator for words in line.split()]
return result
def log_probs(email_paths, smoothing):
# get the token list
tokens = [result for path in email_paths for result in load_tokens(path)]
# Total number of words
NumberOfWords = len(tokens)
# Determine the size of our vocab set
word_counter = Counter(tokens)
V = len(word_counter)
# Formula based on lecture slides
denominator = (NumberOfWords + (smoothing * (V + 1)))
# create the log dictionary based on the formula
log_dictionary = {word: math.log((word_counter[word] + smoothing) / denominator) for word in word_counter}
# special case for unknowns
log_dictionary["<UNK>"] = math.log(smoothing / denominator)
return log_dictionary
class SpamFilter(object):
def __init__(self, spam_dir, ham_dir, smoothing):
# get list of spam & ham files
spam_files = os.listdir(spam_dir)
ham_files = os.listdir(ham_dir)
# create the ham & spam paths
spam = [spam_dir + "/" + s for s in spam_files]
ham = [ham_dir + "/" + s for s in ham_files]
# size of each directory to compute class probabilities
spamSize = len(spam_files)
hamSize = len(ham_files)
# create probability dictionaries
self.spam_dictionary = log_probs(spam, smoothing)
self.ham_dictionary = log_probs(ham, smoothing)
# compute class probability of ham and spam
self.prob_spam = math.log(
spamSize / float(spamSize + hamSize))
self.prob_ham = math.log(1 - self.prob_spam)
def is_spam(self, email_path):
# Uses the approach offered in lecture slide #31 of Naive Bayes
word_counter = Counter(load_tokens(email_path))
# initial probabilities
prob_spam = self.prob_spam
prob_ham = self.prob_ham
# Reduce function calculates the final values. We sum log of probabilities to
# avoid overflow. log P(c) + summation of count(w) log P(W|c) for every word in email
# (iterate over each word in given email)
final_spam_probability = reduce(lambda x, word: x + self.spam_dictionary[word] * word_counter[word]
if self.spam_dictionary.has_key(word)
else x + self.spam_dictionary["<UNK>"] * word_counter[word],
word_counter, prob_spam)
final_ham_probability = reduce(lambda x, word: x + self.ham_dictionary[word] * word_counter[word]
if self.ham_dictionary.has_key(word)
else x + self.ham_dictionary["<UNK>"] * word_counter[word],
word_counter, prob_ham)
# return true if email is spam. Comparison in log space does the job (I
# got the hint from piazza)
return final_spam_probability > final_ham_probability
def most_indicative_helper(self, n, dict):
indicative_dictionary = {}
# We initially calculated log of probabilities.
prob_spam = math.exp(self.prob_spam)
prob_ham = math.exp(self.prob_ham)
for word in set(self.spam_dictionary).intersection(self.ham_dictionary):
# numerator was already calculated.
numerator = math.exp(dict[word])
# p1,p2 calculated using the already computed values in init function.
# Necessary to exponentiate to calculate denominator
p1 = float(math.exp(self.spam_dictionary[word]) * prob_spam)
p2 = float(math.exp(self.ham_dictionary[word]) * prob_ham)
indicative_dictionary[word] = math.log(numerator / (p1 + p2))
return (sorted(indicative_dictionary, key=indicative_dictionary.get, reverse=True)[:n])
def most_indicative_spam(self, n):
return self.most_indicative_helper(n, self.spam_dictionary)
def most_indicative_ham(self, n):
return self.most_indicative_helper(n, self.ham_dictionary)
class MyTest(unittest.TestCase):
def test_accuracy(self):
counter = 0
errors = []
sf = SpamFilter("data/train/spam", "data/train/ham", 1e-5)
spam_dir = "data/dev/spam"
ham_dir = "data/dev/ham"
spam_files = os.listdir(spam_dir)
ham_files = os.listdir(ham_dir)
spam = [spam_dir + "/" + s for s in spam_files]
ham = [ham_dir + "/" + s for s in ham_files]
for mail in spam:
if sf.is_spam(mail) == True:
counter = counter + 1
else:
errors.append(mail)
for mail in ham:
if sf.is_spam(mail) == False:
counter = counter + 1
else:
errors.append(mail)
accuracy = (counter / float(len(spam) + len(ham)))
self.assertGreaterEqual(accuracy, 0.96)
suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
unittest.TextTestRunner(verbosity=2).run(suite)