-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathINCOMPLETE_action-mental-calculation.py
executable file
·165 lines (123 loc) · 4.94 KB
/
INCOMPLETE_action-mental-calculation.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
#!/usr/bin/env python2
from hermes_python.hermes import Hermes
import random
MQTT_IP_ADDR = "localhost"
MQTT_PORT = 1883
MQTT_ADDR = "{}:{}".format(MQTT_IP_ADDR, str(MQTT_PORT))
# replace `bezzam` with your username if using your own intents!!
INTENT_START = "bezzam:start_mental_calculations"
INTENT_ANSWER = "bezzam:give_mental_calculation"
INTENT_STOP = "bezzam:stop_lesson"
INTENT_DOES_NOT_KNOW = "bezzam:does_not_know_calculation"
INTENT_FILTER_GET_ANSWER = [
INTENT_ANSWER,
INTENT_STOP,
INTENT_DOES_NOT_KNOW
]
operations = ["add", "sub", "mul", "div"]
SessionsStates = {}
def main():
# subscribe to the intents of desire and point to the corresponding callback function
with Hermes(MQTT_ADDR) as h:
h.subscribe_intent(INTENT_START, user_request_quiz) \
.subscribe_intent(INTENT_STOP, user_quits) \
.subscribe_intent(INTENT_DOES_NOT_KNOW, user_does_not_know) \
.subscribe_intent(INTENT_ANSWER, user_gives_answer) \
.start()
def create_question(oper=None):
"""
Randomly create a question with two terms.
:param oper: Which operation ("add", "sub", "mul", "div") you would like to focus on (TODO). Default will randomly
pick one of four.
:return:
"""
if oper is None or oper not in operations:
oper = random.choice(operations)
x = random.randint(2, 12)
y = random.randint(2, 12)
# make sure the answer is a positive integer
question = None
answer = None
if oper == "add":
answer = x + y
question = "What is {} plus {}?".format(x, y)
elif oper == "sub":
answer = x
x = x + y
question = "What is {} minus {}?".format(x, y)
elif oper == "mul":
answer = x * y
question = "What is {} times {}?".format(x, y)
elif oper == "div":
answer = x
x = x * y
question = "What is {} divided by {}?".format(x, y)
return question, answer
def continue_lesson(response, session_id):
"""
Update state, and continue/end lesson depending on number of questions left.
:param response: Beginning of response to the user (whether the answer was correct/wrong).
:param session_id: Session ID to keep track on remaining questions.
:return:
"""
SessionsStates[session_id]["step"] += 1
if SessionsStates[session_id]["step"] == SessionsStates[session_id]["n_questions"]:
response += "You had {} out of {} correct. ".format(SessionsStates[session_id]["good"],
SessionsStates[session_id]["n_questions"])
percent_correct = float(SessionsStates[session_id]["good"]) / SessionsStates[session_id]["n_questions"]
if percent_correct == 1.:
response += "You are so smart!"
elif percent_correct >= 0.75:
response += "Well done! With a bit more practice you'll be a master."
elif percent_correct >= 0.5:
response += "Not bad. With more practice, you'll get better."
else:
response += "You should really practice more."
del SessionsStates[session_id]
cont = False
else:
question, answer = create_question()
response += question
SessionsStates[session_id]["ans"] = answer
cont = True
return response, cont
def user_request_quiz(hermes, intent_message):
session_id = intent_message.session_id
# parse intent_message and generate response (don't forget extra space for question!)
n_questions = int(intent_message.slots.n_questions.first().value)
response
# create first question
question, answer = create_question()
# initialize session state
session_state = {
"ans": answer,
"good": 0,
"bad": 0,
"step": 0,
"n_questions": n_questions
}
SessionsStates[session_id] = session_state
# continue session since we need to answer the questions!
hermes.publish_continue_session(session_id, response + question, INTENT_FILTER_GET_ANSWER)
def user_gives_answer(hermes, intent_message):
# parse intent_message
answer
# check user answer and give feedback (don't forget extra space!)
response
# create new question or terminate if reached desired number of questions (`cont=False`)
response, cont = continue_lesson(response, session_id)
# different hermes publish based on value of `cont`
def user_does_not_know(hermes, intent_message):
# some reassuring message
response
# create new question or terminate if reached desired number of questions (`cont=False`)
response, cont = continue_lesson(response, session_id)
# different hermes publish based on value of `cont`
def user_quits(hermes, intent_message):
session_id = intent_message.session_id
# clean up
del SessionsStates[session_id]
response = "Alright. Let's play again soon!"
hermes.publish_end_session(session_id, response)
if __name__ == "__main__":
main()