Пример #1
0
def api_answer_question(user_id, question_id, answer):
    """
    提交对应问题的回答。
        参数:
            user_id      提交回答的用户的 ID;
            question_id  要回答的问题的 ID;
            answer       用户的回答。
    """
    p_validate_argument('user_id', user_id, int, 'question_id', question_id, int, 'answer', answer, str)

    questions = Question.objects.filter(id = question_id)
    if len(questions) != 1:
        raise APIException('不存在的问题。')
    question = questions[0]

    if question.user_id != user_id:
        raise APIException('该问题不属于此用户。')

    if question.answered:
        raise APIException('该问题已经被回答。')

    with transaction.atomic():
        question.answered = True
        question.answer_date = timezone.now()
        question.answer_is_correct = True if question.correct_answer == answer else False
        question.save()

        learned_words = LearnedWord.objects.filter(word_id = question.related_word_id, user_id = user_id)
        if len(learned_words) == 0:
            learned_word = LearnedWord(word_id = question.related_word_id, user_id = user_id)
            learned_word.save()
        else:
            learned_word = learned_words[0]

        learned_word.last_review_date = timezone.now().date()
        learned_word.unfamiliarity = p_calculate_unfamiliarity(learned_word)
        learned_word.total_answered_count += 1
        if question.answer_is_correct:
            learned_word.correct_answered_count += 1
            learned_word.unfamiliarity -= 1 / UNFAMILIARITY_COEFFICIENT
        else:
            learned_word.unfamiliarity += 1 / UNFAMILIARITY_COEFFICIENT
        learned_word.unfamiliarity = p_bound_value(learned_word.unfamiliarity, (0, 1))
        learned_word.save()

        return question.correct_answer
Пример #2
0
def p_recheck_leanred_words(user):
    """
    重新检查并计算给定用户的已学习生词的信息。
        参数:
            待重新检查的用户的 ID。
    """
    with transaction.atomic():
        LearnedWord.objects.all().delete()

        question_dict = {}
        for question in Question.objects.filter(user_id = user.id, answered = True):
            if question.related_word_id not in question_dict:
                question_dict[question.related_word_id] = []
            question_dict[question.related_word_id].append(question)

        for key, value in question_dict.items():
            learned_word = LearnedWord(word_id = key, user_id = user.id)
            learned_word.save()
            value.sort(key = lambda q: q.answer_date)

            unfamiliarity = 1
            learned_word.learned_date = current_date = value[0].answer_date.date()
            total_answered_count = 0
            correct_answered_count = 0

            for question in value:
                time_delta = question.answer_date.date() - current_date
                unfamiliarity = p_calculate_unfamiliarity_with_params(time_delta, unfamiliarity, correct_answered_count)
                
                total_answered_count += 1
                if question.answer_is_correct:
                    correct_answered_count += 1
                    unfamiliarity -= 1 / UNFAMILIARITY_COEFFICIENT
                else:
                    unfamiliarity += 1 / UNFAMILIARITY_COEFFICIENT

                unfamiliarity = p_bound_value(unfamiliarity, (0, 1))
                current_date = question.answer_date.date()

            learned_word.last_review_date = current_date
            learned_word.total_answered_count = total_answered_count
            learned_word.correct_answered_count = correct_answered_count
            learned_word.unfamiliarity = unfamiliarity
            learned_word.save()