-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_chatbot.py
executable file
·290 lines (225 loc) · 7.92 KB
/
base_chatbot.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
#!/usr/bin/env python
import logging
import pickle
import random
import re
import sys
import string
import signal
import time
import os
import shutil
import json
from threading import Timer, RLock
import logger
import markov
from colortext import *
# Required 'msg' dict keys
# text
# speaker
# p_reply
class Bot(object):
def __init__(self):
self.NICK = None
self.REALNAME = None
self.OWNERS = None
self.READONLY = False
# Caches of status
self.seen = {} # lists of who said what when
# Set up a lock for the seen db
self.seendb_lock = RLock()
self.SEENDB = None
# Markov chain settings
self.p_reply = 0.1
self.MARKOVDB = None
# Regular db saves
self.SAVE_PERIOD = 100
self.save_count = 0
# Commands
self.commands = {}
# signal handling
signal.signal(signal.SIGINT, self.signalHandler)
signal.signal(signal.SIGTERM, self.signalHandler)
signal.signal(signal.SIGQUIT, self.signalHandler)
def initialize(self, configFile):
with open(configFile) as json_data_file:
args = json.load(json_data_file)
self.NICK = args.get("nick", "chatbot")
self.REALNAME = args.get("realname", "Arthur J. Chatbot")
if "owners" in args:
self.OWNERS = args.get("owners", [])
else:
self.OWNERS = [None]
self.READONLY = args.get("readonly", False)
self.SEENDB = args.get("seendb", "seendb")
# Markov chain settings
pstr = args.get("p_reply", "0.1")
p = float(pstr)
if p < 0:
p = 0
elif p > 1:
p = 1
self.p_reply = p
self.MARKOVDB = args.get("markovdb", "chatbot_markovdb")
# Regular db saves
self.SAVE_PERIOD = int(args.get("save_period", 100))
self.save_count = 0
@staticmethod
def splitTextIntoSentences(text):
return text.split('.!?()')
@staticmethod
def containsURL(text):
m = re.search('(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?', text)
return m is not None
@staticmethod
def preprocessText(text):
text = str(text)
# remove all color codes
text = re.sub('\x03(?:\d{1,2}(?:,\d{1,2})?)?', '', text)
# remove non-alphanumeric characters
text = re.sub(r'[^a-zA-Z0-9 ]+', '', text)
return text
@staticmethod
def logMessage(speaker, msg):
logging.debug(CYAN + speaker + PLAIN + " : " + BLUE + msg)
def signalHandler(self, unused_signal, unused_frame):
self.quit()
def loadMarkovChain(self):
# Open our Markov chain database
self.mc = markov.MarkovChain(self.MARKOVDB)
def possiblyReply(self, msg):
words = self.preprocessText(msg["text"]).split()
leading_words = ""
seed = None
# If we have enough words, generate a reply based on the message
if len(words) >= 2:
print (GREEN + "Trying to reply to '" + str(words) + "'" + ENDC)
# Use a random bigram of the input message as a seed for the Markov chain
max_index = min(6, len(words)-1)
index = random.randint(1, max_index)
seed = (words[index-1], words[index])
leading_words = string.join(words[0:index+1])
else:
return None
# If we have a reply, randomly determine if we reply with it
if random.random() > msg["p_reply"]:
return None
# generate a response
response = string.strip(self.mc.respond(seed))
if len(leading_words) > 0:
leading_words = leading_words + " "
reply = leading_words + response
#print string.join(seed) + " :: " + reply
if len(response) == 0:
return None
else:
return reply
# Picks a random confused reply
def dunno(self, msg):
replies = ["I dunno, $who",
"I'm not following you."
"I'm not following you, $who."
"I don't understand.",
"You're confusing, $who."]
which = random.randint(0, len(replies)-1)
reply = re.sub("$who", msg["speaker"], replies[which])
return reply
@staticmethod
def createBackup(source):
if os.path.isfile(source):
dst = source + ".bak"
shutil.copyfile(source, dst)
def saveMarkovDatabase(self):
print "Saving Markov chain database"
self.createBackup(self.MARKOVDB)
if self.READONLY:
logging.info('Skipping markov db because we are read-only')
else:
self.mc.saveDatabase()
def addPhrase(self, text):
# add the phrase to the markov database if we're NOT in readonly mode
if not self.READONLY:
self.mc.addLine(text)
self.save_count = self.save_count + 1
if self.save_count == self.SAVE_PERIOD:
self.saveMarkovDatabase()
self.save_count = 0
def recordSeen(self, name, text):
with self.seendb_lock:
self.seen[name] = [time.time(), text]
@staticmethod
def elapsedTime(ss):
reply = ""
startss = ss
if ss > 31557600:
years = ss // 31557600
reply = reply + ("%g years " % years)
ss = ss - years*31557600
if ss > 2678400: # 31 days
months = ss // 2678400
reply = reply + ("%g months " % months)
ss = ss - months*2678400
if ss > 604800:
weeks = ss // 604800
reply = reply + ("%g weeks " % weeks)
ss = ss - weeks*604800
if ss > 86400:
days = ss // 86400
reply = reply + ("%g days " % days)
ss = ss - days*86400
if ss > 3600:
hours = ss // 3600
reply = reply + ("%g hours " % hours)
ss = ss - hours*3600
if ss > 60:
minutes = ss // 60
reply = reply + ("%g minutes " % minutes)
ss = ss - minutes*60
return reply
def hasBeenSeen(self, name):
with self.seendb_lock:
if name in self.seen:
last_seen = self.seen[name][0] # in seconds since epoch
since = self.elapsedTime(time.time() - last_seen)
return (True, self.seen[name][1], since)
else:
return (False, None, None)
def loadSeenDatabse(self):
with self.seendb_lock:
try:
with open(self.SEENDB, 'rb') as seendb:
self.seen = pickle.load(seendb)
except IOError:
logging.error(WARNING +
("Unable to open seen db '%s' for reading" %
self.SEENDB))
def saveSeenDatabase(self):
print "Saving 'seen' database"
with self.seendb_lock:
try:
with open(self.SEENDB, 'wb') as seendb:
pickle.dump(self.seen, seendb)
except IOError:
logging.error(ERROR +
("Unable to open seed db '%s' for writing" %
self.SEENDB))
def registerCommands(self):
return
def handleCommand(self, msg):
cmd = str(self.preprocessText(msg["text"]).split()[0])
if cmd in self.commands:
try:
print GREEN,"Running command '"+cmd+"'"+ENDC
return self.commands[cmd](msg)
except:
print "Error while calling function called '"+cmd+"'"
return False
return False
def run(self):
self.loadMarkovChain()
self.loadSeenDatabse()
# do nothing
def quit(self):
self.saveMarkovDatabase()
self.saveSeenDatabase()
sys.exit(0)