-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.py
69 lines (61 loc) · 2.17 KB
/
read.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
class DataClassifier:
def __init__(self, name):
self.countHam = 0
self.countSpam = 0
self.data = []
for line in open(name, "r"):
index = line.index('\t')
typeL = line[:index].strip()
if (typeL == "ham"):
self.countHam += 1
elif typeL == "spam":
self.countSpam += 1
self.data.append(line)
self.rel1 = float(self.countSpam)/float(self.countSpam + self.countHam)
self.rel2 = float(self.countHam)/float(self.countSpam + self.countHam)
def separateData(self, process_data, percentage):
remaining_data = []
preClassifiedData = []
inserterted = False
cantHam = 0
cantSpam = 0
for line in process_data:
index = line.index('\t')
typeL = line[:index]
line = self.parseFile(line)
if typeL == "ham":
if (cantHam < (int)(round(self.countHam*percentage))):
preClassifiedData.append(line)
cantHam += 1
inserterted = True
elif typeL == "spam":
if (cantSpam < (int)(round(self.countSpam*percentage))):
preClassifiedData.append(line)
cantSpam += 1
inserterted = True
if (inserterted == False):
remaining_data.append(line)
inserterted = False
return preClassifiedData, remaining_data, cantHam, cantSpam
def getData(self, classifyData, p1, p2, p3):
training_data, rem_data, countHam1, countSpam2 = classifyData(self.data, p1)
cross_validation, rem_data, countHam3, countSpam4 = classifyData(rem_data, p2)
test_data, rem_data, countHam5, countSpam6 = classifyData(rem_data, p3)
return [training_data, countHam1, countSpam2] , [cross_validation, countHam3, countSpam4], [test_data, countHam5, countSpam6]
def parseFile(self, line):
line = line.replace(".", "")
line = line.replace("!", "")
line = line.replace("?", "")
line = line.replace(";", "")
line = line.replace(":", "")
line = line.replace(">", "")
line = line.replace(",", "")
line = line.replace("-", "")
return line
def parseArray(self, array):
newData = []
for data in array:
line = self.parseFile(data)
newData.append(line)
return newData
#4449, 557, 555