-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainingcorpus.py
49 lines (41 loc) · 1.73 KB
/
trainingcorpus.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
__author__ = 'eygene'
import utils, os, random, corpus
class TrainingCorpus(corpus.Corpus):
def __init__(self, path_to_train):
self.path_to_train = path_to_train
self.path_to_truth = os.path.join(path_to_train, '!truth.txt')
self.truth_dic = utils.read_classification_from_file(self.path_to_truth)
def get_class(self, path):
if '!' not in path:
if self.is_ham(path):
return 'OK'
if self.is_spam(path):
return 'SPAM'
def is_ham(self, path):
if self.truth_dic[path] == 'OK':
return True
else:
return False
def is_spam(self, path):
if self.truth_dic[path] == 'SPAM':
return True
else:
return False
def spams(self):
for file_names in os.listdir(self.path_to_train):
if self.get_class(file_names) == 'SPAM':
with open(os.path.join(self.path_to_train, file_names), encoding="ISO-8859-1") as a_file:
body = a_file.read()
yield (file_names, body)
def hams(self):
for file_names in os.listdir(self.path_to_train):
if self.get_class(file_names) == 'OK':
with open(os.path.join(self.path_to_train, file_names), encoding="ISO-8859-1") as a_file:
body = a_file.read()
yield (file_names, body)
def pre_spams(self):
for file_names in os.listdir("/Users/eygene/Desktop/spam-data-12-s75-h25/3"):
with open(os.path.join("/Users/eygene/Desktop/spam-data-12-s75-h25/3", file_names),
encoding="ISO-8859-1") as a_file:
body = a_file.read()
yield (file_names, body)