File size: 2,379 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""MMK12 importer — math-only multiple-choice training rows.

All 15,616 public MMK12 ``train`` rows are ``subject=math``; the importer asserts
that invariant row-by-row. A non-math row is treated as source revision drift
(a hard :class:`IngestError`), never silently kept or relabeled. Only ``train``
is ingested; the 2,000-row source ``test`` split never enters training
(``docs/01_RESOURCE_CATALOG.md`` §3.1).
"""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from .base import (
    Choice,
    ImageResolver,
    ImageStore,
    IngestError,
    NormalizedItem,
    Policy,
    canonicalize_mc_answer,
    make_item,
    mc_choices,
)

NAME = "mmk12"
POLICY: Policy = "c2_train_candidate"
LICENSE_GATE = "mmk12_license"
ALLOWED_SPLIT = "train"
REQUIRED_SUBJECT = "math"


def normalize(
    row: Mapping[str, Any],
    images: ImageStore,
    resolve: ImageResolver,
    *,
    revision: str,
    split: str,
    config: str = "default",
) -> NormalizedItem:
    """Normalize one MMK12 native row to a :class:`NormalizedItem`."""
    if split != ALLOWED_SPLIT:
        raise IngestError(f"mmk12: only split {ALLOWED_SPLIT!r} may be ingested, got {split!r}")
    subject = str(row.get("subject", "")).strip()
    if subject != REQUIRED_SUBJECT:
        raise IngestError(
            f"mmk12: row {row.get('index', '?')!r} subject is {subject!r}, expected "
            f"{REQUIRED_SUBJECT!r} — source revision drift"
        )
    question = str(row["question"])
    option_texts = [str(o) for o in row["options"]]
    choices: list[Choice] = mc_choices(option_texts)
    answer_raw = str(row["answer"])
    answer_canonical = canonicalize_mc_answer(answer_raw, choices)
    image_ref = str(row["image"])
    rel, digest = images.store(resolve(image_ref))
    native_id = str(row.get("index", row.get("id")))
    return make_item(
        source=NAME,
        source_revision=revision,
        source_config=config,
        source_split=split,
        source_native_id=native_id,
        question=question,
        choices=choices,
        answer_raw=answer_raw,
        answer_canonical=answer_canonical,
        answer_type="multiple_choice",
        image_paths=(rel,),
        image_sha256=(digest,),
        policy=POLICY,
        native_row=row,
        subject=subject,
        license_gate=LICENSE_GATE,
    )