Esempio n. 1
0
    def check_answer(self,
                     value,
                     alternatives_are_correct=False,
                     allow_typos=False):
        value = value.lower()
        correct = [self['correct'].lower()]
        if alternatives_are_correct:
            correct.extend([x.lower() for x in self['alternatives']])
        if value in correct:
            return True

        # if there is a chance of spelling it wrong, proceed
        alphas = re.findall('[^\d]+', correct[0])
        nums = re.findall('\d+', correct[0])
        if len(nums) > len(alphas):
            return False

        if len(value) < 3:
            # short answers aren't spell corrected
            return False

        if not allow_typos:
            return False

        for each in [x for x in correct if len(x) >= 4]:
            ed = EditDistance(each)
            if ed.match(value):
                return True
            # perhaps there are multiple words, like "Tupac Shakur"
            if (len(each.split()) > 1 and len(value.split()) > 1
                    and len(each.split()) == len(value.split())):
                all = True
                for i, part in enumerate(each.split()):
                    ed = EditDistance(part)
                    if not ed.match(value.split()[i]):
                        all = False
                        break
                if all:
                    return True
        return False
Esempio n. 2
0
    def check_answer(self, value, alternatives_are_correct=False,
                     allow_typos=False):
        value = value.lower()
        correct = [self['correct'].lower()]
        if alternatives_are_correct:
            correct.extend([x.lower() for x in self['alternatives']])
        if value in correct:
            return True

        # if there is a chance of spelling it wrong, proceed
        alphas = re.findall('[^\d]+', correct[0])
        nums = re.findall('\d+', correct[0])
        if len(nums) > len(alphas):
            return False

        if len(value) < 3:
            # short answers aren't spell corrected
            return False

        if not allow_typos:
            return False

        for each in [x for x in correct if len(x) >= 4]:
            ed = EditDistance(each)
            if ed.match(value):
                return True
            # perhaps there are multiple words, like "Tupac Shakur"
            if (len(each.split()) > 1 and len(value.split()) > 1 and
                len(each.split()) == len(value.split())):
                all = True
                for i, part in enumerate(each.split()):
                    ed = EditDistance(part)
                    if not ed.match(value.split()[i]):
                        all = False
                        break
                if all:
                    return True
        return False
Esempio n. 3
0
    def check_answer(self, answer):
        answer = answer.lower().strip()
        if answer == self.answer.lower():
            return True
        elif self.accept:
            if answer in [x.lower() for x in self.accept]:
                return True
        if self.spell_correct:
            ed = EditDistance([self.answer.lower()] +
                              [x.lower() for x in self.accept])
            if ed.match(answer):
                return True

        return False
Esempio n. 4
0
    def check_answer(self, answer, _spell_correct=False):
        if self.stopped:
            return
        _answer = answer.lower().strip()
        if _spell_correct:
            ed = EditDistance([self.current_question.answer.lower()] +
                              [x.lower() for x in self.current_question.accept])
            if ed.match(_answer):
                return True

        if _answer == self.current_question.answer.lower():
            return True
        if self.current_question.accept:
            if _answer in [x.lower() for x in self.current_question.accept]:
                return True

        if not _spell_correct and self.current_question.spell_correct:
            return self.check_answer(answer, _spell_correct=True)

        return False
Esempio n. 5
0
    def check_answer(self, answer, _spell_correct=False):
        if self.stopped:
            return
        _answer = answer.lower().strip()
        if _spell_correct:
            ed = EditDistance(
                [self.current_question.answer.lower()] +
                [x.lower() for x in self.current_question.accept])
            if ed.match(_answer):
                return True

        if _answer == self.current_question.answer.lower():
            return True
        if self.current_question.accept:
            if _answer in [x.lower() for x in self.current_question.accept]:
                return True

        if not _spell_correct and self.current_question.spell_correct:
            return self.check_answer(answer, _spell_correct=True)

        return False