forked from youzan/YZSpamFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTrainAndTestData.py
More file actions
156 lines (128 loc) · 3.98 KB
/
createTrainAndTestData.py
File metadata and controls
156 lines (128 loc) · 3.98 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
import os
import sys
import random
import jieba
from utils import ClearAndSegment, u
from config import configs
trainPos = "trainPos.txt"
trainNeg = "trainNeg.txt"
testPos = "testPos.txt"
testNeg = "testNeg.txt"
def createTrainAndTestData(strpos, strneg, trainRate, stopWords):
fpos = open(strpos)
totalPos = fpos.readlines()
fneg = open(strneg)
totalNeg = fneg.readlines()
#trainRate = 0.9999
trainPosNum = int(trainRate * len(totalPos))
trainNegNum = int(trainRate * len(totalNeg))
trainPosIndex = random.sample(range(len(totalPos)),trainPosNum)
trainNegIndex = random.sample(range(len(totalNeg)),trainNegNum)
trainPosArray = []
testPosArray = []
trainNegArray = []
testNegArray = []
fstop = open(stopWords)
totalStop = fstop.readlines()
stops = []
for s in totalStop:
s = s.strip()
s = u(s)
stops.append(s)
index = 0
for i in range(len(totalPos)):
str = totalPos[i]
str = str.strip()
if str == '':
continue
#print str
str = u(str)
str = ClearAndSegment(str)
str = [word for word in str if word not in stops]
str = '/'.join(str)
str = str + '\n'
if not str.strip():
continue
index +=1
if index%1000 == 0:
print 'Adding positive data: %d' % (index)
if i in trainPosIndex:
trainPosArray.append(str)
else:
testPosArray.append(str)
print '\n'
index = 0
for i in range(len(totalNeg)):
str = totalNeg[i]
str = str.strip()
if str == '':
continue
str = u(str)
str = ClearAndSegment(str)
str = [word for word in str if word not in stops]
str = '/'.join(str)
str = str + '\n'
if not str.strip():
continue
index +=1
if index%1000 == 0:
print 'Adding negative data: %d' % (index)
if i in trainNegIndex:
trainNegArray.append(str)
else:
testNegArray.append(str)
print '\n'
print "len of trainPos is %d\n" % (len(trainPosArray))
print "len of trainNeg is %d\n" % (len(trainNegArray))
print "len of testPos is %d\n" % (len(testPosArray))
print "len of testNeg is %d\n" % (len(testNegArray))
ftrainPos = open(trainPos, "w")
ftrainNeg = open(trainNeg, "w")
ftestPos = open(testPos, "w")
ftestNeg = open(testNeg, "w")
#for ele in trainPosArray:
# ftrainPos.write(ele)
ftrainPos.writelines(trainPosArray)
ftrainNeg.writelines(trainNegArray)
ftestPos.writelines(testPosArray)
ftestNeg.writelines(testNegArray)
fpos.close()
fneg.close()
ftrainPos.close()
ftestPos.close()
ftrainNeg.close()
ftestNeg.close()
fstop.close()
if __name__ == "__main__":
"""
usage: python createTrainAndTestData.py
"""
reload(sys)
sys.setdefaultencoding('utf-8')
strpos = configs['ham_file']
strneg = configs['spam_file']
if configs.has_key('train_rate'):
trainRate = configs['train_rate']
if trainRate == 0:
trainRate = 0.5
print "Warning: train rate should large than 0, set default train rate 0.5"
else:
trainRate = 0.5
print "Warning: no train_rate in conifigs, set default train rate 0.5"
stopwords_file = configs['stopwords_file']
if not os.path.exists(strpos):
print "ERROR: need ham file"
exit(-1)
if not os.path.exists(strneg):
print "ERROR: need spam file"
exit(-1)
if not os.path.exists(stopwords_file):
print "ERROR: need stopwords file"
exit(-1)
print "ham_file is %s" % (strpos)
print "spam_file is %s" % (strneg)
print "train_rate is %f" % (trainRate)
print "stopwords_file is %s" % (stopwords_file)
createTrainAndTestData(strpos, strneg, trainRate, stopwords_file)
print 'Finish creating train and test Data'