-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_handler.py
64 lines (48 loc) · 2.59 KB
/
question_handler.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
import json
import re
import random
import substitutions
# Load the JSON descriptions of the classes
with open('data/classes.json') as course_info:
classes = json.load(course_info)
# Gets the json info associated with a particular class name
def get_class_by_name(name):
match = re.match("([A-Z]+) ?(\d{3})", name)
return classes[ match.group(1) ][ match.group(2) ]
# Store each classes number in the class info object and inflate the prerequiste graph
for prefix, numbers in classes.items():
for number, clazz in numbers.items():
clazz["number"] = prefix + " " + number
if "prereqs" in clazz:
clazz["prereqs"] = [ get_class_by_name(prereq) for prereq in clazz["prereqs"] ]
# Load the JSON storing bot responses
with open('data/responses.json') as keywords_json:
keywords = json.load(keywords_json)
# Matches any valid course prefix followed by a three digit number
# First capture group is the prefix, second is the course number
course = "("+ "|".join( classes.keys() ) +")?\s*(\d{3}|BS)"
# A dict of keyword regexs to appropriate responses
keywords = {"|".join(key["keywords"]):key["response"] for key in keywords}
# Returns a response appropriate for the the string s
def handle(s):
try:
# Grab all of the classes mentioned in s (If no prefix, assume CS)
names_mentioned = [ " ".join((match.group(1) if match.group(1) else "CS", match.group(2))) for match in re.finditer(course, s.upper()) ]
# Grabs all of the responses that would be appropriate for the located keywords
responses = [response for regex, response in keywords.items() if re.search(regex, s.lower()) is not None ]
# Assume 1 keyword and 1 class mentioned
response = responses[ 0 ][ random.randint( 0, len(responses[0]) - 1 ) ]
# Substitute the requested info into the response
return re.sub("\$(\w+)\(([\w\[\]]*)\)", lambda m: substitute(m.group(1), names_mentioned, [arg.strip() for arg in m.group(2).split(",")]), response)
except Exception as e:
return "An exception occured: <" + s + "> `" + str(e) +"`"
def substitute(identifier, mentioned, args):
# If any of the arguments request mentioned classes, substitute now
args = [ re.sub("m\[(\d)\]", lambda m: mentioned[int(m.group(1))], arg) for arg in args ]
# Now substitute class numbers for class objects
args = [ get_class_by_name(arg) for arg in args if re.match("([A-Z]+) ?(\d{3})", arg) ]
# Call the subsitution function
substitution = getattr(substitutions, identifier)
if substitution:
return substitution(*args)
return "BAD SUBSTITUTION"