Commit db2aceedc099a63be6b38691f9adb90efd9073ff
1 parent
1cdce3ee
Exists in
master
and in
1 other branch
- generates questions in sequence corresponding to topological sort of the topics
Showing
2 changed files
with
34 additions
and
18 deletions
Show diff stats
app.py
| @@ -126,7 +126,6 @@ class LearnApp(object): | @@ -126,7 +126,6 @@ class LearnApp(object): | ||
| 126 | s.commit() | 126 | s.commit() |
| 127 | logger.debug('check_answer: saving done') | 127 | logger.debug('check_answer: saving done') |
| 128 | 128 | ||
| 129 | - logger.debug('check_answer: will return knowledge.new_question') | ||
| 130 | return knowledge.new_question() | 129 | return knowledge.new_question() |
| 131 | 130 | ||
| 132 | # ------------------------------------------------------------------------ | 131 | # ------------------------------------------------------------------------ |
knowledge.py
| @@ -19,39 +19,56 @@ class Knowledge(object): | @@ -19,39 +19,56 @@ class Knowledge(object): | ||
| 19 | def __init__(self, depgraph, state={}): | 19 | def __init__(self, depgraph, state={}): |
| 20 | self.depgraph = depgraph | 20 | self.depgraph = depgraph |
| 21 | self.state = state # {node: level, node: level, ...} | 21 | self.state = state # {node: level, node: level, ...} |
| 22 | + | ||
| 23 | + self.topic = self.topic_generator() | ||
| 24 | + self.current_topic = next(self.topic) | ||
| 25 | + | ||
| 26 | + self.questions = self.generate_questions_for_topic(self.current_topic) | ||
| 22 | self.current_question = None | 27 | self.current_question = None |
| 23 | 28 | ||
| 24 | - self.seq = nx.topological_sort(self.depgraph) | ||
| 25 | - self.topic = None | 29 | + # ------------------------------------------------------------------------ |
| 30 | + def topic_generator(self): | ||
| 31 | + topics = nx.topological_sort(self.depgraph) # FIXME for now... | ||
| 32 | + for t in topics: | ||
| 33 | + self.questions = self.generate_questions_for_topic(t) | ||
| 34 | + yield t | ||
| 26 | 35 | ||
| 36 | + # ------------------------------------------------------------------------ | ||
| 37 | + def generate_questions_for_topic(self, topic): | ||
| 38 | + factory = self.depgraph.node[topic]['factory'] | ||
| 39 | + return [q.generate() for q in factory] | ||
| 27 | 40 | ||
| 41 | + # ------------------------------------------------------------------------ | ||
| 28 | def get_current_question(self): | 42 | def get_current_question(self): |
| 29 | return self.current_question | 43 | return self.current_question |
| 30 | 44 | ||
| 45 | + # ------------------------------------------------------------------------ | ||
| 31 | def get_knowledge_state(self): | 46 | def get_knowledge_state(self): |
| 32 | return self.state | 47 | return self.state |
| 33 | 48 | ||
| 34 | - # --- generates a new question given the current state | 49 | + # --- generates a new question given the current state ------------------- |
| 35 | def new_question(self): | 50 | def new_question(self): |
| 36 | logger.debug('new_question()') | 51 | logger.debug('new_question()') |
| 52 | + | ||
| 37 | if self.current_question is None or self.current_question.get('grade', 0.0) > 0.9999: | 53 | if self.current_question is None or self.current_question.get('grade', 0.0) > 0.9999: |
| 38 | - g = self.depgraph | ||
| 39 | - # choose topic, ie, node of the graph | ||
| 40 | - # print(g.nodes()) | ||
| 41 | - self.topic = random.choice(g.nodes()) # FIXME | ||
| 42 | - # print(topic) | ||
| 43 | - # choose question from that topic | ||
| 44 | - question = random.choice(g.node[self.topic]['factory']) | ||
| 45 | - # print(question) | ||
| 46 | - | ||
| 47 | - self.current_question = question.generate() | ||
| 48 | - # print(self.current_question) | ||
| 49 | - | ||
| 50 | - # self.current_question = g.node[topic]['factory'].generate(ref) | 54 | + |
| 55 | + while not self.questions: | ||
| 56 | + self.state[self.current_topic] = 1.0 # finished topic! | ||
| 57 | + try: | ||
| 58 | + self.current_topic = next(self.topic) | ||
| 59 | + except StopIteration: | ||
| 60 | + self.topic = self.topic_generator() | ||
| 61 | + self.current_topic = next(self.topic) | ||
| 62 | + self.questions = self.generate_questions_for_topic(self.current_topic) | ||
| 63 | + | ||
| 64 | + print(self.current_topic) | ||
| 65 | + print(self.current_question) | ||
| 66 | + self.current_question = self.questions.pop(0) | ||
| 51 | self.current_question['start_time'] = datetime.now() | 67 | self.current_question['start_time'] = datetime.now() |
| 68 | + | ||
| 52 | return self.current_question | 69 | return self.current_question |
| 53 | 70 | ||
| 54 | - # --- checks answer and updates knowledge state | 71 | + # --- checks answer ------------------------------------------------------ |
| 55 | # returns current question with correction, time and comments updated | 72 | # returns current question with correction, time and comments updated |
| 56 | def check_answer(self, answer): | 73 | def check_answer(self, answer): |
| 57 | logger.debug(f'check_answer("{answer}")') | 74 | logger.debug(f'check_answer("{answer}")') |