|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from nokaman.data.loader import load_listening_pack |
| 8 | +from nokaman.models.cefr import compare_bands, score_to_cefr |
| 9 | +from nokaman.rubrics.registry import get_language_meta |
| 10 | + |
| 11 | +_DIFFICULTY_BASE = { |
| 12 | + "A1": 22.0, |
| 13 | + "A2": 42.0, |
| 14 | + "B1": 57.0, |
| 15 | + "B2": 72.0, |
| 16 | + "C1": 84.0, |
| 17 | + "C2": 94.0, |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def evaluate_listening_pack_file(path: Path) -> dict: |
| 22 | + pack = load_listening_pack(path) |
| 23 | + result = score_listening_pack(pack) |
| 24 | + expected = pack.get("expected_cefr") |
| 25 | + if expected: |
| 26 | + result["band_check"] = compare_bands(result["cefr"], str(expected)) |
| 27 | + result["source"] = str(path) |
| 28 | + return result |
| 29 | + |
| 30 | + |
| 31 | +def score_listening_pack(pack: dict[str, Any]) -> dict: |
| 32 | + language = str(pack.get("language") or "en").strip().lower() |
| 33 | + meta = get_language_meta(language) |
| 34 | + items = list(pack.get("items") or pack.get("questions") or []) |
| 35 | + learner_answers = pack.get("answers") if isinstance(pack.get("answers"), dict) else {} |
| 36 | + |
| 37 | + scored_items = [] |
| 38 | + earned = 0.0 |
| 39 | + possible = 0.0 |
| 40 | + for index, item in enumerate(items, start=1): |
| 41 | + item_id = str(item.get("id") or f"q{index}") |
| 42 | + response = item.get("response") |
| 43 | + if response is None: |
| 44 | + response = learner_answers.get(item_id) |
| 45 | + weight = _positive_weight(item.get("weight", 1.0)) |
| 46 | + credit = _score_item(item, response) |
| 47 | + earned += credit * weight |
| 48 | + possible += weight |
| 49 | + scored_items.append( |
| 50 | + { |
| 51 | + "id": item_id, |
| 52 | + "type": str(item.get("type") or "mcq"), |
| 53 | + "weight": weight, |
| 54 | + "response": response, |
| 55 | + "credit": round(credit, 4), |
| 56 | + "correct": credit >= 1.0, |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + accuracy = (earned / possible * 100.0) if possible else 0.0 |
| 61 | + difficulty = str(pack.get("difficulty_cefr") or pack.get("expected_cefr") or "").upper() |
| 62 | + score = _ability_score(accuracy, difficulty) |
| 63 | + cefr = score_to_cefr(score) |
| 64 | + return { |
| 65 | + "id": pack.get("id"), |
| 66 | + "language": language, |
| 67 | + "language_name": meta["name"], |
| 68 | + "skill": "listening", |
| 69 | + "score": round(score, 2), |
| 70 | + "accuracy": round(accuracy, 2), |
| 71 | + "cefr": cefr, |
| 72 | + "difficulty_cefr": difficulty or None, |
| 73 | + "n_items": len(items), |
| 74 | + "earned_weight": round(earned, 4), |
| 75 | + "possible_weight": round(possible, 4), |
| 76 | + "items": scored_items, |
| 77 | + "model": "ListeningProxyScorer", |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +def _score_item(item: dict[str, Any], response: object) -> float: |
| 82 | + kind = str(item.get("type") or "mcq").strip().lower() |
| 83 | + normalized_response = _normalize(response) |
| 84 | + if not normalized_response: |
| 85 | + return 0.0 |
| 86 | + |
| 87 | + accepted = [_normalize(value) for value in _accepted_answers(item)] |
| 88 | + accepted = [value for value in accepted if value] |
| 89 | + if normalized_response in accepted: |
| 90 | + return 1.0 |
| 91 | + |
| 92 | + if kind in {"short_answer", "short", "free_text"}: |
| 93 | + keywords = [_normalize(value) for value in item.get("keywords", [])] |
| 94 | + keywords = [value for value in keywords if value] |
| 95 | + if keywords: |
| 96 | + hits = sum(1 for keyword in keywords if keyword in normalized_response) |
| 97 | + return hits / len(keywords) |
| 98 | + |
| 99 | + return 0.0 |
| 100 | + |
| 101 | + |
| 102 | +def _accepted_answers(item: dict[str, Any]) -> list[object]: |
| 103 | + raw = item.get("accepted_answers") |
| 104 | + if raw is None: |
| 105 | + raw = item.get("answer", item.get("correct")) |
| 106 | + if isinstance(raw, list): |
| 107 | + return raw |
| 108 | + return [raw] |
| 109 | + |
| 110 | + |
| 111 | +def _ability_score(accuracy: float, difficulty: str) -> float: |
| 112 | + if difficulty in _DIFFICULTY_BASE: |
| 113 | + score = _DIFFICULTY_BASE[difficulty] + (accuracy - 70.0) * 0.5 |
| 114 | + else: |
| 115 | + score = accuracy |
| 116 | + return max(0.0, min(100.0, score)) |
| 117 | + |
| 118 | + |
| 119 | +def _positive_weight(value: object) -> float: |
| 120 | + try: |
| 121 | + weight = float(value) |
| 122 | + except (TypeError, ValueError): |
| 123 | + return 1.0 |
| 124 | + return weight if weight > 0 else 1.0 |
| 125 | + |
| 126 | + |
| 127 | +def _normalize(value: object) -> str: |
| 128 | + text = "" if value is None else str(value) |
| 129 | + text = text.strip().lower() |
| 130 | + text = re.sub(r"[^\w\s:.-]", " ", text, flags=re.UNICODE) |
| 131 | + return re.sub(r"\s+", " ", text).strip() |
0 commit comments