File size: 1,056 Bytes
d954568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""MCQ answer grading."""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from data.question import TSQuestion

from .config import EnvConfig


def _normalize(s: str, *, lower: bool) -> str:
    s = str(s).strip()
    return s.lower() if lower else s


def grade_answer(
    submitted: str,
    question: TSQuestion,
    config: EnvConfig,
) -> tuple[bool, float]:
    """
    Returns (is_fully_correct, partial_score in [0,1]).

    Binary match to ``question.answer``, or equality to an option whose
    normalized text equals the normalized ground-truth answer.
    ``partial_credit_t1u`` is reserved for future multi-part T1U grading.
    """
    lower = config.case_insensitive_match
    truth = _normalize(question.answer, lower=lower)
    pred = _normalize(submitted, lower=lower)

    if pred == truth:
        return True, 1.0
    for opt in question.options:
        if pred == _normalize(opt, lower=lower) and _normalize(opt, lower=lower) == truth:
            return True, 1.0
    return False, 0.0