"""Document processing for the MESA task tree. Two jobs: 1. Tier/domain routing. Each tier lives in its own dataset config, so a leaf task loads only its tier and filters by domain here. The filters also drop the contamination canary, which is the first record of every data file and has no ``domain`` field. 2. Item rendering for continuation loglikelihood. MESA has two stem shapes and they must not be rendered the same way: * **Cloze** stems carry a ``___`` blank mid-sentence (567 of the 727 English items). The option belongs IN the blank, not after the stem. Appending instead of substituting leaves the blank unfilled and makes every option -- the key included -- ungrammatical, which is not a measurement of anything. The context is the text before the blank and the continuation is ``option + text after the blank``. * **Question** stems are complete questions. These get an ``Answer:`` cue and the option is appended. The four options of a single item always render through the same branch, so the ranking within an item is never comparing two different formats. """ import datasets BLANK = "___" # ---------------------------------------------------------------- rendering def _split_stem(stem): """Return (before_blank, after_blank), or (None, None) for a question.""" if BLANK not in stem: return None, None head, _, tail = stem.partition(BLANK) return head.rstrip(), tail def doc_to_text(doc): head, _ = _split_stem(doc["stem"]) if head is not None: return head return doc["stem"].rstrip() + "\nAnswer:" def doc_to_choice(doc): _, tail = _split_stem(doc["stem"]) if tail is not None: return [" " + c + tail for c in doc["choices"]] return [" " + c for c in doc["choices"]] # ---------------------------------------------------------------- routing def _by_domain(domain): def fn(dataset: datasets.Dataset) -> datasets.Dataset: return dataset.filter(lambda d: d.get("domain") == domain) return fn filter_math = _by_domain("Math") filter_english = _by_domain("English") filter_science = _by_domain("Science") filter_art = _by_domain("Art")