|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Iterable |
| 4 | + |
| 5 | +from nokaman.models.cefr import score_to_cefr |
| 6 | +from nokaman.models.toy import ToyAbilityModel |
| 7 | +from nokaman.rubrics.registry import get_language_meta |
| 8 | + |
| 9 | +CEFR_TARGETS = { |
| 10 | + "A1": 22.0, |
| 11 | + "A2": 42.0, |
| 12 | + "B1": 57.0, |
| 13 | + "B2": 72.0, |
| 14 | + "C1": 84.0, |
| 15 | + "C2": 94.0, |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def adaptive_session( |
| 20 | + language: str, |
| 21 | + answers: list[str] | None = None, |
| 22 | + administered_ids: list[str] | None = None, |
| 23 | +) -> dict: |
| 24 | + code = language.strip().lower() |
| 25 | + meta = get_language_meta(code) |
| 26 | + answer_list = [answer for answer in (answers or []) if answer.strip()] |
| 27 | + answer_estimates = estimate_answer_scores(code, answer_list) |
| 28 | + ability_score = _running_ability(answer_estimates) |
| 29 | + administered = set(administered_ids or []) |
| 30 | + next_prompt = select_next_prompt( |
| 31 | + ability_score=ability_score, |
| 32 | + prompt_bank=build_prompt_bank(code), |
| 33 | + administered_ids=administered, |
| 34 | + ) |
| 35 | + return { |
| 36 | + "language": code, |
| 37 | + "language_name": meta["name"], |
| 38 | + "n_answers": len(answer_list), |
| 39 | + "ability_score": round(ability_score, 2), |
| 40 | + "cefr": score_to_cefr(ability_score), |
| 41 | + "answer_estimates": answer_estimates, |
| 42 | + "next_prompt": next_prompt, |
| 43 | + "complete": next_prompt is None, |
| 44 | + "model": "AdaptiveHeuristicSession", |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def estimate_answer_scores(language: str, answers: list[str]) -> list[dict]: |
| 49 | + model = ToyAbilityModel(language=language) |
| 50 | + estimates = [] |
| 51 | + for index, answer in enumerate(answers, start=1): |
| 52 | + scored = model.score_text(answer, skill="writing") |
| 53 | + estimates.append( |
| 54 | + { |
| 55 | + "answer_index": index, |
| 56 | + "score": scored["score"], |
| 57 | + "cefr": scored["cefr"], |
| 58 | + "tokens": scored["features"]["tokens"], |
| 59 | + } |
| 60 | + ) |
| 61 | + return estimates |
| 62 | + |
| 63 | + |
| 64 | +def select_next_prompt( |
| 65 | + ability_score: float, |
| 66 | + prompt_bank: Iterable[dict], |
| 67 | + administered_ids: set[str] | list[str] | None = None, |
| 68 | +) -> dict | None: |
| 69 | + administered = set(administered_ids or []) |
| 70 | + candidates = [prompt for prompt in prompt_bank if str(prompt["id"]) not in administered] |
| 71 | + if not candidates: |
| 72 | + return None |
| 73 | + target = max(0.0, min(100.0, float(ability_score))) |
| 74 | + return min( |
| 75 | + candidates, |
| 76 | + key=lambda prompt: ( |
| 77 | + abs(float(prompt["target_score"]) - target), |
| 78 | + float(prompt["target_score"]), |
| 79 | + str(prompt["id"]), |
| 80 | + ), |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def build_prompt_bank(language: str) -> list[dict]: |
| 85 | + code = language.strip().lower() |
| 86 | + meta = get_language_meta(code) |
| 87 | + language_name = meta["name"] |
| 88 | + templates = { |
| 89 | + "A1": f"Introduce yourself in simple {language_name} sentences.", |
| 90 | + "A2": f"Describe your daily routine in {language_name} with times and places.", |
| 91 | + "B1": f"Explain a recent problem you solved while learning {language_name}.", |
| 92 | + "B2": f"Compare two study strategies and defend your preference in {language_name}.", |
| 93 | + "C1": f"Analyze how culture affects communication style in {language_name}.", |
| 94 | + "C2": f"Write a nuanced argument about language policy and education in {language_name}.", |
| 95 | + } |
| 96 | + return [ |
| 97 | + { |
| 98 | + "id": f"{code}_{band.lower()}_adaptive", |
| 99 | + "language": code, |
| 100 | + "difficulty_cefr": band, |
| 101 | + "target_score": target, |
| 102 | + "prompt": templates[band], |
| 103 | + } |
| 104 | + for band, target in CEFR_TARGETS.items() |
| 105 | + ] |
| 106 | + |
| 107 | + |
| 108 | +def _running_ability(answer_estimates: list[dict]) -> float: |
| 109 | + if not answer_estimates: |
| 110 | + return CEFR_TARGETS["A2"] |
| 111 | + return sum(float(item["score"]) for item in answer_estimates) / len(answer_estimates) |
0 commit comments