knowledge.py
2.02 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
# 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__)
# ----------------------------------------------------------------------------
# contains the kowledge state of each student.
class Knowledge(object):
def __init__(self, depgraph, state={}):
self.depgraph = depgraph
self.state = state # {node: level, node: level, ...}
self.current_question = None
self.seq = nx.topological_sort(self.depgraph)
self.topic = None
def get_current_question(self):
return self.current_question
def get_knowledge_state(self):
return self.state
# --- generates a new question given the current state
def new_question(self):
logger.debug('new_question()')
if self.current_question is None or self.current_question.get('grade', 0.0) > 0.9999:
g = self.depgraph
# choose topic, ie, node of the graph
# print(g.nodes())
self.topic = random.choice(g.nodes()) # FIXME
# print(topic)
# choose question from that topic
question = random.choice(g.node[self.topic]['factory'])
# print(question)
self.current_question = question.generate()
# print(self.current_question)
# self.current_question = g.node[topic]['factory'].generate(ref)
self.current_question['start_time'] = datetime.now()
return self.current_question
# --- checks answer and updates knowledge state
# returns current question with correction, time and comments updated
def check_answer(self, answer):
logger.debug(f'check_answer("{answer}")')
question = self.current_question
if question is not None:
question['finish_time'] = datetime.now()
question.correct(answer)
# logger.debug(f'check_answer returning')
return question