-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaction-mental-calculation.py
executable file
·160 lines (123 loc) · 5.13 KB
/
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
#!/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))
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 create_question(oper=None):
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):
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 input message, NOTE extra space to append question
n_questions = int(intent_message.slots.n_questions.first().value)
if n_questions > 1:
response = "Starting a lesson with {} questions. ".format(n_questions)
elif n_questions == 1:
response = "Starting a lesson with one question. "
else:
response = "I can only give a positive number of questions."
hermes.publish_end_session(session_id, 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
hermes.publish_continue_session(session_id, response + question, INTENT_FILTER_GET_ANSWER)
def user_gives_answer(hermes, intent_message):
session_id = intent_message.session_id
# parse input message
answer = intent_message.slots.answer.first().value
# check user answer, NOTE the extra space at the end since we will add more to the response!
if answer == SessionsStates[session_id]["ans"]:
response = "Correct! "
SessionsStates[session_id]["good"] += 1
else:
response = "Incorrect. The answer is {}. ".format(SessionsStates[session_id]["ans"])
SessionsStates[session_id]["bad"] += 1
# create new question or terminate if reached desired number of questions
response, cont = continue_lesson(response, session_id)
if cont:
hermes.publish_continue_session(intent_message.session_id, response, INTENT_FILTER_GET_ANSWER)
else:
hermes.publish_end_session(session_id, response)
def user_does_not_know(hermes, intent_message):
session_id = intent_message.session_id
response = "That's quite alright! The answer is {}. ".format(SessionsStates[session_id]["ans"])
# create new question or terminate if reached desired number of questions
response, cont = continue_lesson(response, session_id)
if cont:
hermes.publish_continue_session(intent_message.session_id, response, INTENT_FILTER_GET_ANSWER)
else:
hermes.publish_end_session(session_id, response)
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)
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()