-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
73 lines (54 loc) · 1.26 KB
/
preprocessing.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
import os
import codecs
from collections import Counter
def make_dict():
direct = "emails/"
files = os.listdir(direct)
emails = [direct + email for email in files]
#print(emails)
words = []
c = len(emails)
for email in emails:
#f = open(email)
with codecs.open(email, "r",encoding='utf-8', errors='ignore') as fdata:
blob = fdata.read()
words += blob.split(' ')
print(c)
c -= 1
#print(words)
for i in range(len(words)):
if not words[i].isalpha():
words[i] = ""
dictionary = Counter(words)
del dictionary[""]
return dictionary.most_common(3000)
def make_dataset(dictionary):
direct = "emails/"
files = os.listdir(direct)
emails = [direct + email for email in files]
#print(emails)
words = []
labels = []
feautre_set = []
c = len(emails)
for email in emails:
#f = open(email)
data = []
with codecs.open(email, "r",encoding='utf-8', errors='ignore') as fdata:
blob = fdata.read()
words += blob.split(' ')
for entry in dictionary:
data.append(words.count(entry[0]))
feautre_set.append(data)
if "ham" in email:
labels.append(0)
if "spam" in email:
labels.append(1)
print(c)
c -= 1
return feautre_set, labels
'''
d = make_dict()
feautre_set, labels = make_dataset(d)
print(feautre_set, labels)
'''