forked from youzan/YZSpamFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
198 lines (157 loc) · 5.05 KB
/
filter.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
import os
import sys
import pickle
from classifier import Algorithm
from utils import ClearAndSegment, u
from config import configs
trainPos = "trainPos.txt"
trainNeg = "trainNeg.txt"
testPos = "testPos.txt"
testNeg = "testNeg.txt"
posScore = "posScore.txt"
negScore = "negScore.txt"
class filter:
def __init__(self, Algorithm):
self.Algorithm = Algorithm
self.tar = 0.0
self.trr = 0.0
self.accuracy = 0.0
self.hashset = set()
def train(self):
fTrainPos = open(trainPos)
fTrainNeg = open(trainNeg)
trainPosArray = fTrainPos.readlines()
trainNegArray = fTrainNeg.readlines()
totalPos = len(trainPosArray)
totalNeg = len(trainNegArray)
realPos = 0
realNeg = 0
for pos in trainPosArray:
hashvalue = hash(pos)
if hash(pos) not in self.hashset:
self.hashset.add(hashvalue)
else:
continue
realPos += 1
pos = pos.strip('')
pos = pos.split('/')
is_spam = False
self.Algorithm.cover(pos, is_spam)
for neg in trainNegArray:
hashvalue = hash(neg)
if hashvalue not in self.hashset:
self.hashset.add(hashvalue)
else:
continue
realNeg += 1
neg = neg.strip('')
neg = neg.split('/')
#neg = neg.strip('/')
#neg = [word for word in neg]
is_spam = True
self.Algorithm.cover(neg, is_spam)
print "Real Train Pos: %d, Total Train Pos:%d " % (realPos, totalPos)
print "Real Train Neg: %d, Total Train Neg:%d " % (realNeg, totalNeg)
def test(self, threshold):
fPosScore = open(posScore, 'w')
fNegScore = open(negScore, 'w')
fTestPos = open(testPos)
fTestNeg = open(testNeg)
testPosArray = fTestPos.readlines()
testNegArray = fTestNeg.readlines()
totalPos = len(testPosArray)
totalNeg = len(testNegArray)
assert totalPos > 0
assert totalNeg > 0
print "Test Pos: %d" % (totalPos)
print "Test Neg: %d" % (totalNeg)
rightPos = 0
rightNeg = 0
for pos in testPosArray:
pos = pos.strip('')
pos = pos.split('/')
#pos = pos.strip('/')
#pos = [word for word in pos]
score = self.Algorithm.predict(pos)
#print score
fPosScore.write("%d\n" % (score))
if score > threshold:
is_spam = True
else:
is_spam = False
if is_spam==False:
rightPos+=1
fPosScore.close()
for neg in testNegArray:
neg = neg.strip('')
neg = neg.split('/')
#neg = neg.strip('/')
#neg = [word for word in neg]
score = self.Algorithm.predict(neg)
fNegScore.write("%d\n" % (score))
if score > threshold:
is_spam = True
else:
is_spam = False
if is_spam==True:
rightNeg+=1
fNegScore.close()
self.tar = float(rightPos)/totalPos
self.trr = float(rightNeg)/totalNeg
self.accuracy = float(rightPos+rightNeg)/(totalPos+totalNeg)
def singlejudge(self, wordlist):
score = self.Algorithm.predict(wordlist)
return score
if __name__ == '__main__':
"""
usage: python filter.py
"""
reload(sys)
sys.setdefaultencoding('utf-8')
classify_model = configs['classify_model']
threshold = configs['threshold']
##############################
# 1.train or load model
##############################
if os.path.exists(classify_model):
with open(classify_model, 'rb') as file:
f = filter(Algorithm)
t = pickle.load(file)
f.Algorithm.loadmodel(t)
file.close()
else:
f = filter(Algorithm)
f.train()
with open(classify_model, 'wb') as file:
t = f.Algorithm.getmodel()
pickle.dump(t, file)
file.close()
##############################
# 2.test
##############################
f.test(threshold)
print "tar = %f" % (f.tar)
print "trr = %f" % (f.trr)
print "accuracy = %f" % (f.accuracy)
##############################
# 3.single judge
##############################
stopwords_file = configs['stopwords_file']
fstop = open(stopwords_file)
totalStop = fstop.readlines()
fstop.close()
stops = []
for s in totalStop:
s = s.strip()
stops.append(s)
query = '赚钱test宝妈tes日赚学生兼职*.@打字员'
query = u(query)
print query
listquery = ClearAndSegment(query)
#print listquery
listquery = [word.encode('UTF-8') for word in listquery if word not in stops]
if f.singlejudge(listquery) > threshold:
print 'WARNING: this is a spam message'
else:
print 'this message is harmless'