-
Notifications
You must be signed in to change notification settings - Fork 9
/
antispam.py
318 lines (239 loc) · 8.81 KB
/
antispam.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import monkeypatch
import re,math
import shelve
if __name__ == '__main__':
from aql import AQLController
aqlc = AQLController('http://127.0.0.1:8529', 'db2047')
aql = aqlc.aql
def allcn(text):
text = text.lower()
return re.findall(r'[\u4e00-\u9fbba-z\?\!?!。,]{1}',text).join('')
def preproc(text):
allcnt = f'^{allcn(text)}$'
grams = {}
for gl in [2,3]: # bigram is enough
for i in range(len(allcnt)-gl+1):
gram2 = allcnt[i:i+gl]
if gram2 not in grams:
grams[gram2] = 0
grams[gram2]+=1
return grams,len(allcnt)
class SpamTrainer:
def __init__(self):
self.known_goods = {}
self.known_spams = {}
self.known_ignore = {5983, 5976}
self.tc = {}
def get_one_thread(self, tid):
if tid not in self.tc:
print('getting', tid ,'from db')
self.tc[tid] = aql('''
return (for i in threads filter i.tid==@tid return i)[0]
''', silent=True, tid=tid)[0]
return self.tc[tid]
def get_threads(self, tids):
print('loading...', len(tids))
tss=[]
for tid in tids:
ts = self.get_one_thread(tid)
tss.append(ts)
print('loaded...', len(tids))
return tss
def make_dict(self):
self.spam_ts = spam_ts = self.get_threads(list(self.known_spams.keys()))
self.good_ts = good_ts = self.get_threads(list(self.known_goods.keys()))
spam_grams = {}
spam_length = 0
for idx, t in enumerate(spam_ts):
for j in (t['title'] , t['content']):
grams,length = preproc(j)
for k in grams:
if k not in spam_grams:
spam_grams[k] = 0
spam_grams[k]+=grams[k]
spam_length+=length
if idx%20==0:
print('parsing spam', idx, len(t['content']))
good_grams = {}
good_length = 0
for idx, t in enumerate(good_ts):
for j in (t['title'] , t['content']):
grams,length = preproc(j)
for k in grams:
if k not in good_grams:
good_grams[k]= 0
good_grams[k]+=grams[k]
good_length+=length
if idx%20==0:
print('parsing good', idx, len(t['content']))
final_grams = {}
for k in spam_grams:
if k not in final_grams:
final_grams[k] = [1e-6, 1e-6, 0]
freq_in_spam = spam_grams[k]/spam_length
final_grams[k][0] += freq_in_spam
final_grams[k][2] += spam_grams[k]
for k in good_grams:
if k not in final_grams:
final_grams[k] = [1e-6, 1e-6, 0]
freq_in_good = good_grams[k]/good_length
final_grams[k][1] += freq_in_good
final_grams[k][2] += good_grams[k]
print('spamlen,goodlen',spam_length, good_length)
print(len(final_grams), 'in final_grams')
spamgoods = {}
for k,v in final_grams.items():
j = math.log(v[0]) - math.log(v[1])
if (j>5 or j<-2):
spamgoods[k] = j
self.spamgoods = spamgoods
print(len(spamgoods), 'in spamgoods')
def score_text(self, *texts):
sg = self.spamgoods
spamlog = 0
goodlog = 0
for text in texts:
grams,length = preproc(text)
for gram in grams:
if gram in sg:
spamlog += sg[gram] * grams[gram]
# spamscore *= sg[gram]**grams[gram]
minlog = max(spamlog, goodlog)
spamlog -= minlog
goodlog -= minlog
# spamlog,goodlog = spamlog*.5, goodlog*.5
spamscore = math.exp(spamlog)
goodscore = math.exp(goodlog)
sumscore = spamscore+goodscore
return spamscore/sumscore, goodscore/sumscore
# return spamscore,goodscore
def save(self):
with shelve.open('spamdb',flag='c') as d:
d['good'] = self.known_goods
d['spam'] = self.known_spams
d['tc'] = self.tc
with shelve.open('spamdata/spamgoods',flag='c') as d:
d['spamgoods'] = self.spamgoods
def load(self):
with shelve.open('spamdb',flag='c') as d:
if 'good' in d and 'spam' in d:
self.known_goods = d['good']
self.known_spams = d['spam']
if 'tc' in d:
self.tc = d['tc']
with shelve.open('spamdata/spamgoods',flag='c') as d:
if 'spamgoods' in d :
self.spamgoods = d['spamgoods']
def load_dict_only(self):
with shelve.open('spamdata/spamgoods',flag='c') as d:
if 'spamgoods' in d :
self.spamgoods = d['spamgoods']
def addgoods(self, goods):
for j in goods:
self.known_goods[j] = True
if j in self.known_spams:
del self.known_spams[j]
def addspams(self, goods):
for j in goods:
self.known_spams[j] = True
if j in self.known_goods:
del self.known_goods[j]
st = SpamTrainer()
def is_spam(*texts):
threshold = told = 0.99999
spam, good = st.score_text(*texts)
if spam > told:
return True
else:
return False
if __name__!='__main__':
st.load_dict_only()
if __name__ == '__main__':
st.load()
known_spams = [14223,14127,14075,14074,13290,13289,
3573,3572,3575,4180,4288,4378,4437,4436,4488,4489,4538,4535,4526,4537
]
st.addspams(known_spams)
print('make_dict...')
st.make_dict()
print('save...')
st.save()
print(f'got {len(st.known_goods)}good {len(st.known_spams)}spam')
lenallt = aql('return count(for i in threads return 1)')[0]
# lenallt = 100
overkill = 0
underkill = 0
g,s = [],[]
# scan thru all threads
for i in range(lenallt):
t = aql(f'for i in threads sort i.t_c desc limit {i},1 return i', silent=True)[0]
tid = t['tid']
# add thread to [goods] if good enough via some metric
if 1:
if 'votes' in t and t['votes']>=8:
if len(t['content'])>200 and (('delete' not in t)
or (not t['delete'])):
print(tid,'added b/c very good')
st.addgoods([tid])
continue
else:
if tid in st.known_goods:
print(tid,'deleted b/c problematic')
del st.known_goods[tid]
spam,good = st.score_text(t['title'], t['content'])
tid = t['tid']
if i % 50==0:
print(f"{i} {t['tid']} {spam:.7f}/{good:.7f}")
if spam >0.99:
print(f"spam{spam:.7f} #{t['tid']}# ###CONSIDERED SPAM### {t['title']}")
# print(t['content'])
threshold = told = 0.99999
# elif 0.05<good<0.95:
if spam < told and tid in st.known_spams:
print('UNDERKILL!!!',f'{spam:.7f}', tid, t['title'])
underkill+=1
# if CONSIDERED as spam but not included in [spams]
if spam>told and (tid not in st.known_spams) and \
(tid not in st.known_ignore):
# if overkill
if tid in st.known_goods:
print('OVERKILL!!!',f'{spam:.7f}', tid, t['title'])
overkill+=1
print(f"{good:.7f} {t['tid']}, {t['title']}")
print(t['content'])
print('tid in spam, good:', tid in st.known_spams, tid in st.known_goods)
# ask whether this thread should be considered as good instead
end = 0
while 1:
print(tid, 'is this good?')
ss = input().lower()
if 'y' in ss:
# g.append(t['tid'])
st.addgoods([tid])
break
elif 'n' in ss:
# s.append(t['tid'])
st.addspams([tid])
break
elif 'i' in ss:
print('ignore', tid)
if tid in st.known_goods:
del st.known_goods[tid]
if tid in st.known_spams:
del st.known_spams[tid]
break
elif 'l' in ss:
# learn
st.make_dict()
st.save()
continue
elif 'q' in ss:
end = 1
break
else:
continue
if end:
break
print(f'got {len(st.known_goods)}good {len(st.known_spams)}spam')
print(f'overkills {overkill} underkills {underkill}')
st.save()