-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbayesian.py
67 lines (53 loc) · 2.39 KB
/
bayesian.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
import sqlite3
import api
import config
class Module(api.Module):
def __init__(self, server):
self.db = sqlite3.connect('wordstats.sqlite')
self.c = self.db.cursor()
self.c.execute('''create table if not exists stats (word text, spam, legit)''')
self.db.commit()
self.ratio = 0.01
self.damp = 0.3
self.kickat = 1.15
self.spamat = 1.5
self.legitat = 1
self.banat = 1.5
self.remove = ['-', '.', ', ', '_', '+', '\'', '"']
api.module.__init__(self, server)
def destroy(self):
self.db.commit()
del self.db, self.c
def privmsg(self, nick, data, channel):
pass
def spam(self, data):
for word in str(data).split():
tmp = self.c.execute('''select * from stats where word = ?''', (self.safety(word),)).fetchall()
if len(tmp) > 0:
self.c.execute('''delete from stats where word = ?''', (self.safety(word),))
self.c.execute('''insert into stats values (?, ?, ?)''', (self.safety(word), tmp[0][1] + 1, tmp[0][2]))
else:
self.c.execute('''insert into stats values (?, ?, ?)''', (self.safety(word), 2, 1))
def legit(self, data):
for word in data.split():
tmp = self.c.execute('''select * from stats where word = ?''', (self.safety(word),)).fetchall()
if len(tmp) > 0:
self.c.execute('''delete from stats where word = ?''', (self.safety(word),))
self.c.execute('''insert into stats values (?, ?, ?)''', (self.safety(word), tmp[0][1], tmp[0][2] + 1))
else:
self.c.execute('''insert into stats values (?, ?, ?)''', (self.safety(word), 1, 2))
def zzz(self, data):
for word in data.split():
tmp = self.c.execute('''select * from stats where word = ?''', (self.safety(word),)).fetchall()
self.msg('#bayesian', str(tmp))
def check(self, data):
self.msg('#bayesian', 'Level: %s' % self.get_stat(data))
def get_stat(self, data):
total = 0
for word in data.split():
tmp = self.c.execute('''select * from stats where word = ?''', (self.safety(word),)).fetchall()
if len(tmp) > 0:
total += ((float(tmp[0][1]) / float(tmp[0][2])) * self.ratio) / self.damp
return total
def safety(self, s):
return str(s)