-
Notifications
You must be signed in to change notification settings - Fork 4
/
helga_jeopardy.py
363 lines (259 loc) · 9.38 KB
/
helga_jeopardy.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import datetime
import nltk
import random
import re
import requests
import smokesignal
import string
from helga import settings, log
from helga.db import db
from helga.plugins import command
from bson.son import SON
from difflib import SequenceMatcher
from nltk.corpus import stopwords
from nltk.stem.snowball import EnglishStemmer
from requests.exceptions import RequestException
from twisted.internet import reactor
logger = log.getLogger(__name__)
DEBUG = getattr(settings, 'HELGA_DEBUG', False)
ANSWER_DELAY = getattr(settings, 'JEOPARDY_ANSWER_DELAY', 30)
CHANNEL_ANNOUNCEMENT = getattr(settings, 'JEOPARDY_JOIN_MESSAGE', '')
URL_RE = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
api_endpoint = 'http://www.trivialbuzz.com/api/v1/'
correct_responses = [
'look at the big brains on {}',
'{}, you are correct.',
'{} takes it, and has control of the board.',
]
def reset_channel(channel, mongo_db=db.jeopardy):
"""
For channel name, make sure no question is active.
"""
logger.debug('resetting channel')
mongo_db.update_many({
'channel': channel,
'active': True
}, {'$set': {
'active': False
}})
remove_punctuation_map = dict((ord(char), None) for char in string.punctuation)
def process_token(token):
"""
stuff we do to every token, both answer and responses.
1. cast to unicode and lower case
2. remove punctuation
3. stem
"""
# cast to unicode and lower case
token = u'{}'.format(token).lower()
# remove punctuation
token = token.translate(remove_punctuation_map)
# stem
stemmer = EnglishStemmer()
token = stemmer.stem(token)
return token
def eval_potential_answer(input_line, answer):
"""
Checks if `input_line` is an match for `answer`
returns a 3 item tuple:
`bool`: True if correct
`partial`: number of tokens matched
`ratio`: ratio of matching characters
"""
pot_answers = re.findall(r'\([^()]*\)|[^()]+', answer)
if len(pot_answers) == 2:
for pot_answer in pot_answers:
pot_answer = pot_answer.replace('(','').replace(')','')
correct, _, _ = eval_potential_answer(input_line, pot_answer)
if correct:
return correct, None, None
correct = False
partial = 0
ratio = 0.0
input_string = u''.join(input_line)
sequence_matcher = SequenceMatcher(None, input_string, answer)
ratio = sequence_matcher.ratio()
if ratio >= 0.75:
correct = True
input_tokens = [process_token(token) for token in input_line]
processed_answer_tokens = [process_token(token) for token in answer.split()]
answer_tokens = []
for tok in processed_answer_tokens:
if tok not in stopwords.words('english'):
answer_tokens.append(tok)
# remove stopwords from answer_tokens
matched = set(input_tokens).intersection(set(answer_tokens))
partial = len(matched)
logger.debug(u'matched: {}'.format(matched))
logger.debug(u'ratio: {}'.format(ratio))
if len(matched) == len(answer_tokens):
correct = True
return correct, partial, ratio
def reveal_answer(client, channel, question_id, answer, mongo_db=db.jeopardy):
"""
This is the timer, essentially. When this point is reached, no more
answers will be accepted, and our gracious host will reveal the
answer in all of it's glory.
"""
logger.debug('time to reveal the answer, if no one has guess')
question = mongo_db.find_one({
'_id': question_id,
})
if not question:
logger.warning('no question found, not good')
return
if not question['active']:
logger.debug('not active question, someone must have answered it! Good Show!')
return
client.msg(channel, u'the correct answer is: {}'.format(answer))
mongo_db.update({
'_id': question_id,
}, {
'$set': {
'active': False,
}
})
def retrieve_question(client, channel):
"""
Return the question and correct answer.
Adds question to the database, which is how it is tracked until
active=False.
"""
logger.debug('initiating question retrieval')
try:
tb_resp = requests.get('{}questions/random.json'.format(api_endpoint))
except RequestException:
return "Could not retrieve a question from the TrivialBuzz API"
json_resp = tb_resp.json()['question']
question_text = json_resp['body'][1:-1]
answer = json_resp['response']
category = json_resp['category']['name']
value = json_resp['value']
if DEBUG:
logger.debug(u'psst! the answer is: {}'.format(answer))
question_id = db.jeopardy.insert({
'question': question_text,
'answer': answer,
'channel': channel,
'value': value,
'active': True,
})
question = u'[{}] For ${}: {}'.format(category, value, question_text)
logger.debug(u'will reveal answer in {} seconds'.format(ANSWER_DELAY))
reactor.callLater(ANSWER_DELAY, reveal_answer, client, channel, question_id, answer)
return question
def clean_question(question):
"""
Cleans question text.
:param question: The raw question text.
:return: A 2-tuple of the shape (<Resulting question>, <List of contextual messages to send before the question>)
"""
contexts = []
result = question
url_matches = re.findall(URL_RE, question)
if any(url_matches):
result = re.sub(URL_RE, "", question)
contexts += url_matches
return result.strip(), contexts
def scores(client, channel, nick, alltime=False):
"""
Returns top 3 scores in past week, plus the score of requesting
nick, if the requesting nick is not in the top 3.
"""
max_number = 3
if alltime:
max_number = 5
pipeline = [
{'$match': {
'channel': channel,
}},
{ '$group': {'_id': '$answered_by', 'money': {'$sum': '$value' }}},
{ '$sort': SON([('money', -1), ('_id', -1)])}
]
title = "Jeopardy Leaderboard"
if not alltime:
title += " (Past 7 Days)"
start_date = datetime.datetime.utcnow() - datetime.timedelta(days=7)
pipeline[0]['$match']['timestamp'] = {'$gte': start_date }
else:
title += " Hall of Game"
leaderboard = [leader_obj for leader_obj in db.jeopardy.aggregate(pipeline)]
rank = 1
if len(leaderboard):
client.msg(channel, title)
for leader in leaderboard:
if leader['_id'] is None:
continue
money = leader['money']
money = (u'${:%d,.0f}'%(len(str(money))+1)).format(abs(money)).lstrip()
if rank < max_number + 1:
client.msg(channel, u"{}. {} -- {}".format(rank, leader['_id'], money))
if leader['_id'] == nick:
if rank >= max_number + 1:
# i see you getting all judgey
client.msg(channel, u"{}. {} -- {}".format(rank, leader['_id'], money))
rank += 1
@command('j', help='usage: ,j [<response>|score]')
def jeopardy(client, channel, nick, message, cmd, args,
quest_func=retrieve_question, mongo_db=db.jeopardy):
"""
Asks a question if there is no active question in the channel.
If there are args and there is an active question, then evaluate
the string as a possible answer.
If there is an arg and there is no active question, ignore, was
probably a late response.
On the first correct response, deactivate the question and report
the correct response (w/ nick).
if the command 'score' is given, prints simple leaderboard
"""
if args and args[0] == 'score':
alltime = False
if len(args) > 1 and args[1] == 'all':
alltime = True
return scores(client, channel, nick, alltime=alltime)
if len(args) == 1 and args[0] == 'reset':
reset_channel(channel, mongo_db)
return 'done'
# if we have an active question, and args, evaluate the answer
question = mongo_db.find_one({
'channel': channel,
'active': True,
})
if question and args:
logger.debug('found active question')
correct, partial, ratio = eval_potential_answer(args, question['answer'])
if correct:
logger.debug('answer is correct!')
mongo_db.update({
'active': True,
'channel': channel,
}, {
'$set': {
'active': False,
'answered_by': nick,
'timestamp': datetime.datetime.utcnow(),
}
})
return random.choice(correct_responses).format(nick)
if partial > 0:
return u"{}, can you be more specific?".format(nick)
# wrong answer, ignore
return
if question and not args:
logger.debug('no answer provided :/')
return
if not question and args:
logger.debug('no active question :/')
return
question_text = quest_func(client, channel)
result, context_messages = clean_question(question_text)
for m in context_messages:
client.msg(channel, m)
return result
@smokesignal.on('join')
def back_from_commercial(client, channel):
logger.info('Joined %s, resetting jeopardy state', channel)
if CHANNEL_ANNOUNCEMENT:
client.msg(channel, CHANNEL_ANNOUNCEMENT)
reset_channel(channel)
nltk.download('stopwords')