knowledge.py
10.2 KB
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
# python standard library
import random
from datetime import datetime
import logging
# libraries
import networkx as nx
# this project
# import questions
# setup logger for this module
logger = logging.getLogger(__name__)
# ----------------------------------------------------------------------------
# kowledge state of each student....??
# Contains:
# state - dict of topics with state of unlocked topics
# deps - dependency graph
# topic_sequence - list with the order of recommended topics
# ----------------------------------------------------------------------------
class StudentKnowledge(object):
# =======================================================================
# methods that update state
# =======================================================================
def __init__(self, deps, state={}):
self.deps = deps # dependency graph shared among students
self.state = state # {'topic': {'level':0.5, 'date': datetime}, ...}
self.update_topic_levels() # forgetting factor
self.topic_sequence = self.recommend_topic_sequence() # ['a', 'b', ...]
self.unlock_topics() # whose dependencies have been done
self.current_topic = None
self.MAX_QUESTIONS = 6 # FIXME get from yaml configuration file??
# ------------------------------------------------------------------------
# Updates the proficiency levels of the topics, with forgetting factor
# FIXME no dependencies are considered yet...
# ------------------------------------------------------------------------
def update_topic_levels(self):
now = datetime.now()
for s in self.state.values():
dt = now - s['date']
s['level'] *= 0.95 ** dt.days # forgetting factor 0.95
# ------------------------------------------------------------------------
# Unlock topics whose dependencies are satisfied (> min_level)
# ------------------------------------------------------------------------
def unlock_topics(self):
# minimum level that the dependencies of a topic must have
# for the topic to be unlocked.
min_level = 0.01
for topic in self.topic_sequence:
if topic not in self.state: # if locked
pred = self.deps.predecessors(topic)
if all(d in self.state and self.state[d]['level'] > min_level for d in pred):
# all dependencies are done
self.state[topic] = {
'level': 0.0, # unlocked
'date': datetime.now()
}
logger.debug(f'Unlocked "{topic}".')
# ------------------------------------------------------------------------
# Start a new topic. If not provided, gets a recommendation.
# questions: list of generated questions to do in the topic
# current_question: the current question to be presented
# ------------------------------------------------------------------------
def init_topic(self, topic=''):
logger.debug(f'StudentKnowledge.init_topic({topic})')
# maybe get topic recommendation
if not topic:
topic = self.get_recommended_topic()
logger.debug(f'Recommended topic is {topic}')
# do not allow locked topics
if self.is_locked(topic):
logger.debug(f'Topic {topic} is locked')
return
# starting new topic
self.current_topic = topic
factory = self.deps.node[topic]['factory']
questionlist = self.deps.node[topic]['questions']
self.correct_answers = 0
self.wrong_answers = 0
# select a random set of questions for this topic
size = min(self.MAX_QUESTIONS, len(questionlist)) # number of questions
questionlist = random.sample(questionlist, k=size)
logger.debug(f'Questions: {", ".join(questionlist)}')
# generate instances of questions
self.questions = [factory[qref].generate() for qref in questionlist]
logger.debug(f'Total: {len(self.questions)} questions')
# get first question
self.next_question()
# def init_learning(self, topic=''):
# logger.debug(f'StudentKnowledge.init_learning({topic})')
# if self.is_locked(topic):
# logger.debug(f'Topic {topic} is locked')
# return False
# self.current_topic = topic
# factory = self.deps.node[topic]['factory']
# lesson = self.deps.node[topic]['lesson']
# self.questions = [factory[qref].generate() for qref in lesson]
# logger.debug(f'Total: {len(self.questions)} questions')
# self.next_question_in_lesson()
# ------------------------------------------------------------------------
# The topic has finished and there are no more questions.
# The topic level is updated in state and unlocks are performed.
# The current topic is unchanged.
# ------------------------------------------------------------------------
def finish_topic(self):
logger.debug(f'StudentKnowledge.finish_topic({self.current_topic})')
self.current_question = None
self.state[self.current_topic] = {
'date': datetime.now(),
'level': self.correct_answers / (self.correct_answers + self.wrong_answers)
}
self.unlock_topics()
# ------------------------------------------------------------------------
# corrects current question with provided answer.
# implements the logic:
# - if answer ok, goes to next question
# - if wrong, counts number of tries. If exceeded, moves on.
# ------------------------------------------------------------------------
def check_answer(self, answer):
logger.debug('StudentKnowledge.check_answer()')
q = self.current_question
q['answer'] = answer
q['finish_time'] = datetime.now()
grade = q.correct()
logger.debug(f'Grade {grade:.2} ({q["ref"]})')
# if answer is correct, get next question
if grade > 0.999:
self.correct_answers += 1
self.next_question()
# if wrong, keep same question and append a similar one at the end
else:
self.wrong_answers += 1
self.current_question['tries'] -= 1
logger.debug(f'Wrong answers = {self.wrong_answers}; Tries = {self.current_question["tries"]}')
# append a new instance of the current question to the end and
# move to the next question
if self.current_question['tries'] <= 0:
logger.debug("Appending new instance of this question to the end")
factory = self.deps.node[self.current_topic]['factory']
self.questions.append(factory[q['ref']].generate())
self.next_question()
# returns answered and corrected question (not new one)
return q
# ------------------------------------------------------------------------
# Move to next question
# ------------------------------------------------------------------------
def next_question(self):
try:
self.current_question = self.questions.pop(0)
except IndexError:
self.finish_topic()
else:
self.current_question['start_time'] = datetime.now()
self.current_question['tries'] = self.current_question.get('max_tries', 3) # FIXME hardcoded 3
logger.debug(f'Next question is "{self.current_question["ref"]}"')
# def next_question_in_lesson(self):
# try:
# self.current_question = self.questions.pop(0)
# except IndexError:
# self.current_question = None
# else:
# logger.debug(f'Next question is "{self.current_question["ref"]}"')
# ========================================================================
# pure functions of the state (no side effects)
# ========================================================================
# ------------------------------------------------------------------------
# compute recommended sequence of topics ['a', 'b', ...]
# ------------------------------------------------------------------------
def recommend_topic_sequence(self):
return list(nx.topological_sort(self.deps))
# ------------------------------------------------------------------------
def get_current_question(self):
return self.current_question
# ------------------------------------------------------------------------
def get_current_topic(self):
return self.current_topic
# ------------------------------------------------------------------------
def is_locked(self, topic):
return topic not in self.state
# ------------------------------------------------------------------------
# Return list of {ref: 'xpto', name: 'long name', leve: 0.5}
# Levels are in the interval [0, 1] if unlocked or None if locked.
# Topics unlocked but not yet done have level 0.0.
# ------------------------------------------------------------------------
def get_knowledge_state(self):
return [{
'ref': ref,
'type': self.deps.nodes[ref]['type'],
'name': self.deps.nodes[ref]['name'],
'level': self.state[ref]['level'] if ref in self.state else None
} for ref in self.topic_sequence ]
# ------------------------------------------------------------------------
def get_topic_progress(self):
return self.correct_answers / (1 + self.correct_answers + len(self.questions))
# ------------------------------------------------------------------------
def get_topic_level(self, topic):
return self.state[topic]['level']
# ------------------------------------------------------------------------
def get_topic_date(self, topic):
return self.state[topic]['date']
# ------------------------------------------------------------------------
# Recommends a topic to practice/learn from the state.
# ------------------------------------------------------------------------
def get_recommended_topic(self): # FIXME untested
return min(self.state.items(), key=lambda x: x[1]['level'])[0]