-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetector.py
More file actions
46 lines (36 loc) · 967 Bytes
/
detector.py
File metadata and controls
46 lines (36 loc) · 967 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
44
45
46
import _pickle as c
import os
from sklearn import *
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 = "msg/"
files = os.listdir(direc)
message = [direc + msg for msg in files]
words = []
c = len(message)
for msg in message:
f = open(msg,"r",encoding='utf-8', 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]])