diff --git a/aprendizations/learnapp.py b/aprendizations/learnapp.py index 03490ee..ed57c84 100644 --- a/aprendizations/learnapp.py +++ b/aprendizations/learnapp.py @@ -370,7 +370,6 @@ class LearnApp(object): t['path'] = path.join(g.graph['prefix'], tref) # prefix/topic - # ======================================================================== # methods that do not change state (pure functions) # ======================================================================== @@ -379,6 +378,7 @@ class LearnApp(object): # Buils dictionary of question factories # ------------------------------------------------------------------------ def make_factory(self) -> Dict[str, QFactory]: + logger.info('Building questions factory:') factory: Dict[str, QFactory] = {} g = self.deps diff --git a/aprendizations/questions.py b/aprendizations/questions.py index cb15512..d66c2c6 100644 --- a/aprendizations/questions.py +++ b/aprendizations/questions.py @@ -234,7 +234,8 @@ class QuestionText(Question): self.set_defaults(QDict({ 'text': '', - 'correct': [], + 'correct': [], # no correct answers, always wrong + 'transform': [], # transformations applied to the answer, in order })) # make sure its always a list of possible correct answers @@ -244,12 +245,36 @@ class QuestionText(Question): # make sure all elements of the list are strings self['correct'] = [str(a) for a in self['correct']] + # make sure that the answers are invariant with respect to the filters + if any(c != self.transform(c) for c in self['correct']): + logger.warning(f'in "{self["ref"]}", correct answers are not ' + 'invariant wrt transformations') + + # ------------------------------------------------------------------------ + # apply optional filters to the answer + def transform(self, ans): + for f in self['transform']: + if f == 'remove_space': + ans = ans.replace(' ', '') + elif f == 'trim': + ans = ans.strip() + elif f == 'normalize_space': + ans = re.sub(r'\s+', ' ', ans.strip()) + elif f == 'lower': + ans = ans.lower() + elif f == 'upper': + ans = ans.upper() + else: + logger.warning(f'in "{self["ref"]}", unknown transform "{f}"') + return ans + # ------------------------------------------------------------------------ def correct(self) -> None: super().correct() if self['answer'] is not None: - self['grade'] = 1.0 if self['answer'] in self['correct'] else 0.0 + answer = self.transform(self['answer']) # apply transformations + self['grade'] = 1.0 if answer in self['correct'] else 0.0 # ============================================================================ diff --git a/aprendizations/student.py b/aprendizations/student.py index 10fad38..6319bb2 100644 --- a/aprendizations/student.py +++ b/aprendizations/student.py @@ -3,7 +3,7 @@ from datetime import datetime import logging import random -from typing import Dict, List, Optional, Tuple +from typing import List, Optional, Tuple # third party libraries import networkx as nx @@ -176,7 +176,7 @@ class StudentState(object): dt = now - s['date'] try: forgetting_factor = self.deps.nodes[tref]['forgetting_factor'] - s['level'] *= forgetting_factor ** dt.days # forgetting factor + s['level'] *= forgetting_factor ** dt.days # forgetting factor except KeyError: logger.warning(f'Topic {tref} is not on the graph!') -- libgit2 0.21.2