-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBayesFilter.py
More file actions
135 lines (69 loc) · 2.36 KB
/
Copy pathBayesFilter.py
File metadata and controls
135 lines (69 loc) · 2.36 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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
#pip install nltk
# In[26]:
import nltk
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import os
import random
# In[27]:
class BayesFilter:
def __init__(self, ham_email_list, spam_email_list):
self.spam_email_list=spam_email_list
self.ham_email_list= ham_email_list
def run(self):
def create_word_features(words):
my_dict = dict( [ (word, True) for word in words] )
return my_dict
ham_list = [] # list of dictionary
spam_list = [] # list of dictionary
for email in self.ham_email_list:
#words = word_tokenize(email)
words = email.split()
ham_list.append((create_word_features(words), "Not Spam"))
for email in self.spam_email_list:
#words = word_tokenize(email)
words = email.split()
spam_list.append((create_word_features(words), "Spam"))
spam_list
ham_list
combined_list = ham_list + spam_list
# print(len(combined_list))
random.shuffle(combined_list)
combined_list
# Create a test and train section.
# 70% of the data is training. 30% is test
#training_part = int(len(combined_list) * .7)
# print(len(combined_list))
#training_set = combined_list[:training_part]
#test_set = combined_list[training_part:]
#print(test_set)
# print (len(training_set))
# print (len(test_set))
# Create the Naive Bayes filter
classifier = NaiveBayesClassifier.train(combined_list)
# Find the accuracy, using the test data
#accuracy = nltk.classify.util.accuracy(classifier, test_set)
#print("Accuracy is: ", accuracy * 100)
# #NEW TEST EMAILS
# email_test = "i am ready for this to be labeled as ham"
# #words = word_tokenize(msg1)
# features = create_word_features(email_test.split())
# print("Message 1 is :" ,classifier.classify(features)
#classifier.show_most_informative_features(5)
print("Your spam filter is all set. ")
bool = True
while(bool):
print("Please enter the subject line which you would like to test. To exit, type \"close\"")
title = str(input())
if title == "close":
bool = False
break
features = create_word_features(title.split())
print("Your Email is :" ,classifier.classify(features))
print("yay")