-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtradibot.py
331 lines (283 loc) · 12 KB
/
tradibot.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
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 12 11:50:59 2016
Author: Arttu Huttunen, 2016
Oulu, Finland.
Version 0.84
"""
from errbot import BotPlugin, botcmd
import random, json
import tradibot_conf
#vocabulary words are of class vocWord
class vocWord:
"""Stores a word and relations it has to other words."""
def __init__(self, newWord):
self.word = newWord
self.occurrence = 1
self.links = {'':0}
#increase occurrence
def incOcc(self):
if self.occurrence < 255:
self.occurrence = self.occurrence + 1
#decrease occurrence
def decOcc(self):
if self.occurrence > 0:
self.occurrence = self.occurrence - 1
#links this word to another word
def linkWords(self, word, amount=1):
if word != self.word:
#if word is in links dictionary increase link strength
if word in self.links: #self.links.get(word) != None:
self.links[word] = self.links[word] + amount
if self.links[word] > 255:
self.links[word] = 255
#if not in links replace lowest strength link with word
else:
if len(self.links) > 15: #at max 16 links
del self.links[min(self.links, key=self.links.get)]
self.links[word] = 1
#reduce random link strength
def decLinks(self):
key = random.choice(list(self.links.keys()))
if self.links[key] > 0:
self.links[key] = self.links[key] - 1
#gives randomly a link, weighted by the link strengths
def giveLink(self):
keys = []
values = []
a=0
for item in self.links.items():
keys.append(item[0])
a= a + item[1]
values.append(a)
rnd = random.randint(0,values[-1])
b = 0
while b < len(self.links):
if rnd < values[b]:
return keys[b]
b += 1
else:
return keys[-1]
#makes a list of the data for -save to file- purpose
def dataToList(self):
output = [self.word, self.occurrence, self.links]
return output
class Tradibot(BotPlugin):
"""Tragic discussion bot -plugin bor Errbot. Tragic refers to the development process.
The bot takes all messages posted to a channel and makes a vocabulary of
the words, then semi-randomly talks back."""
#Tragic discussion bot
#Safer to make these non-persistent!
#
# def activate(self):
# """Initializes bot variables"""
# #if the __init__/activate does not work, comment out and initialize manually.
# self.vocabularyfile = 'vocabulary.txt'
#
# self.activity = 1 #0-255
# self.muted = True
# self.enabled = False
# self.recent = ['']*8 #set by using: self['recent'][0]
#
# self.chatroom = self.build_identifier('#general')
# #self.chatroom = self.query_room("#general") #*******SET #channel
# self.urge = 0 # the urge to talk 0-255
# self['vocabulary'] = [vocWord('init')]
# super().activate()
#Here are commands for adminstering the bot
@botcmd
def tradibot(self, msg, args):
if args == 'mute':
"""No speaking."""
self.muted = True
return 'Tradibot muted'
elif args == 'unmute':
self.muted = False
return 'Tradibot unmuted'
elif args == 'enable':
self.enabled = True
return 'Tradibot enabled'
elif args == 'disable':
"""No word collecting or speaking."""
self.enabled = False
return 'Tradibot disabled'
elif args == 'status':
statusList = ['activity: ', str(self.activity),
' silence: ', str(self.silence),
' muted: ', str(self.muted),
' enabled: ', str(self.enabled),
' urge: ', str(self.urge),
' dictionary size: ', str(len(self['vocabulary']))]
statusList.extend(self.recent)
state = ' '.join(statusList)
return state
elif args == 'talknow':
"""Bot talks now."""
self.urge = 65535
self.speak()
return 'I have spoken.'
elif args == 'save':
"""Saves the vocabulary to a file."""
data = []
for item in self['vocabulary']:
data.append(item.dataToList())
with open(tradibot_conf.vocabularyfile, 'w') as outfile:
json.dump(data, outfile)
return 'Vocabulary saved'
elif args.split()[0] =='activity':
"""Sets the value how often the bot speaks, max 65535. !tradibot activity 4"""
self.activity = int(args.split()[1])
if self.activity >65535:
self.activity = 65535
return 'Activity set to ' + str(self.activity)
elif args.split()[0] =='silence':
"""Sets the value how fast bot becomes silent. !tradibot silence 40"""
self.silence = int(args.split()[1])
if self.silence >65535:
self.silence = 65535
elif self.silence == 0:
self.silence = 1 #will not stop talking with 0
return 'Silence set to ' + str(self.silence)
elif args == 'initialize':
"""Initializes bot variables."""
#if the __init__/activate does not work, comment out and use this manually.
self.vocabularyfile = tradibot_conf.vocabularyfile
self.activity = 200 #0-65535
self.silence = 2000
self.muted = True
self.enabled = False
self.recent = ['']*16 #set by using: self['recent'][0]
self.chatroom = self.build_identifier(tradibot_conf.chatroom)
#self.chatroom = self.query_room(tradibot_conf.chatroom)
self.urge = 0 # the urge to talk 0-65535
return 'Initialized'
elif args == 'new_vocabulary':
"""Resets the vocabulary."""
self['vocabulary'] = [vocWord('init')]
return 'New vocabulary initialized'
elif args == 'load_vocabulary':
"""Loads words from file to vocabulary."""
with open(tradibot_conf.vocabularyfile, 'r') as file:
data=json.load(file)
vocab = self['vocabulary']
#add new words from file to vocabulary
for word in data:
for curvoc in vocab:
if word[0] == curvoc.word:
break
else:
if len(vocab) >= 8191:
vocab.pop()
vocab.insert(8000,vocWord(word))
vocab[8000].occurrence = word[1]
vocab[8000].links = word[2]
else:
vocab.append(vocWord(word))
vocab[-1].occurrence = word[1]
vocab[-1].links = word[2]
#sort by occurrence
pos = len(vocab) - 1
while pos > 0:
if vocab[pos].occurrence > vocab[pos-1].occurrence:
vocab[pos],vocab[pos-1] = vocab[pos-1], vocab[pos]
pos = pos - 1
self['vocabulary'] = vocab
return 'Loaded vocabulary file'
else:
return '''Valid parameters are: initialize, status, mute, unmute,
enable, disable, activity 250, silence 2000, save, new_vocabulary,
load_vocabulary'''
#Actual logic starts here
def speak(self, sentence = ''):
#if wants to speak
vocab = self.vocab
topic = random.choice(self.recent)
rnd = random.randint(0,65535)
while self.urge > rnd:
self.urge = self.urge - self.silence
if self.urge < 0:
self.urge = 0
for item in vocab:
if item.word == topic:
relation = item.giveLink()
sentence += relation +' '
topic = relation
break
#talk to chat
if sentence != '':
self.send(self.chatroom, sentence,)
def vocUpdate(self, word):
vocab = self.vocab
#vocabulary is vocab[vocWord(word1),vocWord(word2)]
#if old word
for item in vocab:
if word == item.word:
item.incOcc()
#link current to recent, i.e. word in voc gets a link to recent
for rec in self.recent:
item.linkWords(rec)
#link recent to current, i.e. recent linked to word at hand
#find recent and link
for rcnt in vocab:
if rcnt.word == rec:
rcnt.linkWords(word)
break
break
#if new word, insert near end
else:
if len(vocab) >= 8192:
point = 8000
vocab.pop()
vocab.insert(point,vocWord(word))
else:
vocab.append(vocWord(word))
point = len(vocab) -1
#links ...
for rec in self.recent:
vocab[point].linkWords(rec)
for rcnt in vocab:
if rcnt.word == rec:
rcnt.linkWords(word)
break
#if vocabulary size is over something, keep balance
if vocab[0].occurrence > 200:
#Randomly reduce some occurrence and links to keep balance
random.choice(vocab).decOcc()
for i in range(0,32):
random.choice(vocab).decLinks()
self.vocab = vocab
def callback_message(self, mess):
if self.enabled:
#skip commands completely and too long messages
if not mess.body.startswith(tradibot_conf.ignore_commands):
self.vocab = self['vocabulary'] #RAM copy of vocab
if len(mess.body) < 1000:
# sanitize word by word, add to vocabulary, add to recent, speak
incoming_message = mess.body.lower()
incoming_words = incoming_message.split()
for item in incoming_words:
if len(item) < 32:
if not item.startswith(tradibot_conf.forbidden_words):
if item.endswith(tradibot_conf.remove_chars):
item = item[:-1]
self.vocUpdate(item)
del self.recent[0]
self.recent.append(item)
self.urge += self.activity
if self.urge > 65535:
self.urge = 65535
#sort by order of occurrence, at least partially
vocab = self.vocab
pos = len(vocab) - 1
while pos > 0:
if vocab[pos].occurrence > vocab[pos-1].occurrence:
vocab[pos],vocab[pos-1] = vocab[pos-1], vocab[pos]
pos = pos - 1
self.vocab = vocab
if not self.muted:
self.speak()
#too long message is skipped
else:
if not self.muted:
self.speak('tl;dr ')
self['vocabulary'] = self.vocab