-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetector.py
45 lines (35 loc) · 930 Bytes
/
detector.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
import pickle as c
import os
from collections import Counter
def load(clf_file):
with open(clf_file, 'rb') as fp:
clf = c.load(fp)
return clf
def make_dict():
direc = "emails/"
files = os.listdir(direc)
emails = [direc + email for email in files]
words = []
c = len(emails)
for email in emails:
f = open(email, errors='ignore')
blob = f.read()
words += blob.split(" ")
c -= 1
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("text-classifier.mdl")
d = make_dict()
while True:
features = []
inp = input(">").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]])