-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.py
More file actions
44 lines (33 loc) · 973 Bytes
/
detector.py
File metadata and controls
44 lines (33 loc) · 973 Bytes
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
import os
import pickle
from collections import Counter
def load_dataset(clf_file):
return pickle.load(open(clf_file, "rb"))
def make_dict():
direc = "emails/"
files = os.listdir(direc)
emails = [direc + email for email in files]
words = []
for email in emails:
f = open(email)
if f is not None:
content = f.read()
words += content.split(' ')
for i in range(len(words)):
if not words[i].isalpha():
words[i] = ''
dictionary = Counter(words)
del dictionary['']
return dictionary.most_common(3000)
clf = load_dataset("spam_detection.classifier")
d = make_dict()
# Use input from command line
while True:
features = []
inp = input("Enter test email (subject + content): ").split()
if inp[0] == "exit":
break
for word in d:
features.append(inp.count(word[0]))
res = clf.predict([features])
print(["Not Spam", "Spam!"][res[0]])