forked from dasu/syrup-sopel-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wafflebot.py
153 lines (135 loc) · 4.39 KB
/
wafflebot.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
import datetime
import redis
import markovify
import sopel
# Change this if you want, it will increase the DB size eventually.
timeout = 60 * 60 * 24 * 30 # 60s * 60m * 24h * 30d = 1 month
ignore = ["http"]
MAX_OVERLAP_RATIO = 0.7
MAX_OVERLAP_TOTAL = 15
global llines
llines = 0
db = redis.Redis(db=0)
class WaffleBotText(markovify.Text):
def test_sentence_input(self, sentence):
return True
def _prepare_text(self, text):
text = text.strip()
#if not text.endswith((".", "?", "!")):
# text += "."
return text
def sentence_split(self, text):
lines = text.splitlines()
text = " ".join([self._prepare_text(line) for line in lines if line.strip()])
return markovify.split_into_sentences(text)
def setup(bot):
global fullresults
global fullmodel
#global llines
#llines = 0
fullresults = []
for k in db.keys('*'):
fullresults.extend(db.smembers(k))
fullmodel = WaffleBotText("\n".join([r.decode('utf8') for r in fullresults]), state_size=3)
@sopel.module.interval(86400)
def refresh_results(bot):
fullresults = []
for k in db.keys('*'):
fullresults.extend(db.smembers(k))
fullmodel = WaffleBotText("\n".join([r.decode('utf8') for r in fullresults]), state_size=3)
@sopel.module.rule(r'.*$')
def wafflebot(bot, trigger):
for ignored_item in ignore:
if ignored_item.lower() in trigger.lower():
continue
# have at least 5 words
if len(str(trigger).split(" ")) < 5:
return
today = datetime.datetime.now()
key = "%s%s:%s" % (today.month, today.day, trigger.nick.lower())
db.sadd(key, str(trigger))
#db.expire(key, timeout)
global llines
llines+=1
if llines > 1000:
llines = 0
nicklist = []
fullresp = []
nicks = bot.channels[trigger.sender.lower()].users
for nick in nicks:
nicklist.append(nick)
while not fullresp:
fullresp = fullmodel.make_short_sentence(140,
max_overlap_total=MAX_OVERLAP_TOTAL,
max_overlap_ratio=MAX_OVERLAP_RATIO)
while any(word in fullresp.lower().split() for word in nicklist):
fullresp = fullmodel.make_short_sentence(140,
max_overlap_total=MAX_OVERLAP_TOTAL,
max_overlap_ratio=MAX_OVERLAP_RATIO)
bot.say(fullresp)
@sopel.module.commands('talk', 'wb')
def wafflebot_talk(bot, trigger):
nick = trigger.group(2)
if nick:
pattern = "*:%s" % nick.lower()
min_lines = 200
results = []
for k in db.keys(pattern):
results.extend(db.smembers(k))
else:
#pattern = "*"
min_lines = 100
results = fullresults
#results = []
#for k in db.keys(pattern):
# results.extend(db.smembers(k))
if len(results) < min_lines:
bot.say("Sorry %s, there is not enough data." % trigger.nick)
else:
if nick:
model = WaffleBotText("\n".join([r.decode('utf8') for r in results]), state_size=3)
nicklist = []
resp = []
nicks = bot.channels[trigger.sender.lower()].users
for nick in nicks:
nicklist.append(nick)
while not resp:
resp = model.make_short_sentence(140,
max_overlap_total=MAX_OVERLAP_TOTAL,
max_overlap_ratio=MAX_OVERLAP_RATIO)
while any(word in resp.lower().split() for word in nicklist):
resp = model.make_short_sentence(140,
max_overlap_total=MAX_OVERLAP_TOTAL,
max_overlap_ratio=MAX_OVERLAP_RATIO)
bot.say(resp)
else:
nicklist = []
fullresp = []
nicks = bot.channels[trigger.sender.lower()].users
for nick in nicks:
nicklist.append(nick)
while not fullresp:
fullresp = fullmodel.make_short_sentence(140,
max_overlap_total=MAX_OVERLAP_TOTAL,
max_overlap_ratio=MAX_OVERLAP_RATIO)
if fullresp:
while any(word in fullresp.lower().split() for word in nicklist):
fullresp = fullmodel.make_short_sentence(140,
max_overlap_total=MAX_OVERLAP_TOTAL,
max_overlap_ratio=MAX_OVERLAP_RATIO)
bot.say(fullresp)
@sopel.module.commands('wbknows')
def wafflebotknows(bot,trigger):
#knows = db.dbsize()
knows = len(fullresults)
bot.say("I know about %s things" % (knows))
@sopel.module.commands('lines')
def lines(bot,trigger):
if not trigger.group(2):
return bot.say("Enter a nick")
db = redis.Redis(db=0)
linessaid = 0
for x in db.keys("*{0}".format(trigger.group(2))):
print(x)
linessaid+=len(db.smembers(x))
bot.say("{} has said {} lines".format(trigger.group(2),linessaid))