knowledge.py 3.98 KB

# 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.topic_sequence = nx.topological_sort(self.depgraph) # FIXME

        # state = {'topic_id': {'level':0.5, 'date': datetime}, ...}
        self.state = state

        # select a topic to do
        self.new_topic()

    # ------------------------------------------------------------------------
    def new_topic(self, topic=None):
        logger.debug(f'-> Knowledge.new_topic({topic})')
        if topic is None:
            # select the first topic that has level < 0.9
            for topic in self.topic_sequence:
                if topic not in self.state or self.state[topic]['level'] < 0.9:
                    break

        # FIXME if all are > 0.9, will stay in the last one forever...
        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 = []

    # ------------------------------------------------------------------------
    def generate_questions_for_topic(self, topic):
        logger.debug(f'-> Knowledge.generate_questions_for_topic({topic})')
        factory_list = self.depgraph.node[topic]['factory']
        return [q.generate() for q in factory_list]

    # ------------------------------------------------------------------------
    def get_current_question(self):
        return self.current_question

    # ------------------------------------------------------------------------
    def get_current_topic(self):
        return self.current_topic

    # ------------------------------------------------------------------------
    def get_knowledge_state(self):
        logger.debug('-> Knowledge.get_knowledge_state()')
        ts = []
        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):
        logger.debug('-> Knowledge.get_topic_progress()')
        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):
        logger.debug('-> Knowledge.new_question()')

        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': 1.0,
                    '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)

            return self.current_question

    # --- checks answer ------------------------------------------------------
    #     returns current question with correction, time and comments updated
    def check_answer(self, answer):
        logger.debug(f'-> Knowledge.check_answer({answer})')
        question = self.current_question
        if question is not None:
            question['finish_time'] = datetime.now()
            question.correct(answer)

        return question