knowledge.py
4.45 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
# 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....??
# ----------------------------------------------------------------------------
class Knowledge(object):
def __init__(self, depgraph, state={}, student=''):
self.depgraph = depgraph
self.state = state # {'topic_id': {'level':0.5, 'date': datetime}, ...}
self.student = student
# compute recommended sequence of topics (FIXME)
self.topic_sequence = nx.topological_sort(self.depgraph)
# select a topic to do
self.new_topic()
# ------------------------------------------------------------------------
# Start a new topic. If not provided, selects the first with level < 0.8
# If all levels > 0.8, will stay in the last one forever...
# ------------------------------------------------------------------------
def new_topic(self, topic=None):
if topic is None:
for topic in self.topic_sequence:
if topic not in self.state or self.state[topic]['level'] < 0.8:
break
logger.info(f'Student {self.student} new topic "{topic}"')
self.current_topic = topic
# self.current_topic_idx = self.topic_sequence.index(topic)
self.questions = self.generate_questions_for_topic(topic)
self.current_question = None
self.finished_questions = []
self.correct_answers = 1
self.wrong_answers = 0
# ------------------------------------------------------------------------
def generate_questions_for_topic(self, topic):
factory = self.depgraph.node[topic]['factory']
return [q.generate() for q in factory]
# ------------------------------------------------------------------------
def get_current_question(self):
return self.current_question
# ------------------------------------------------------------------------
def get_current_topic(self):
return self.current_topic
# ------------------------------------------------------------------------
def get_knowledge_state(self):
ts = [] # FIXME why list??
for t in self.topic_sequence:
if t in self.state:
ts.append((t, self.state[t]['level']))
else:
ts.append((t, 0.0))
return ts
# ------------------------------------------------------------------------
def get_topic_progress(self):
return len(self.finished_questions) / (len(self.finished_questions) + len(self.questions))
# ------------------------------------------------------------------------
# if answer to current question is correct generates a new question
# otherwise returns None
def new_question(self):
if self.current_question is None or \
self.current_question.get('grade', 0.0) > 0.9:
# if no more questions in this topic, go to the next one
# keep going if there are no questions in the next topics
while not self.questions:
self.state[self.current_topic] = {
'level': self.correct_answers / (self.correct_answers + self.wrong_answers),
'date': datetime.now()
}
self.new_topic()
self.current_question = self.questions.pop(0)
self.current_question['start_time'] = datetime.now()
self.finished_questions.append(self.current_question)
logger.debug(f'Student {self.student}: new_question({self.current_question["ref"]})')
return self.current_question
# ------------------------------------------------------------------------
# returns the current question with correction, time and comments updated
# ------------------------------------------------------------------------
def check_answer(self, answer):
question = self.current_question
if question is not None:
question['finish_time'] = datetime.now()
grade = question.correct(answer)
if grade > 0.9:
self.correct_answers += 1
else:
self.wrong_answers +=1
logger.debug(f'Student {self.student}: check_answer({answer}) = {grade}')
return question