"""Ground-truth extractor for the IDEXX calibration deck. Reads speaker notes from a .pptx via services.notes_xml (raw zip, no python-pptx side effects) and classifies each slide's notes into the 6 fixed eval categories using simple regex rules. Public API ---------- classify_notes(notes: dict[int, str]) -> dict[int, set[str]] Map ``{slide_n: notes_text}`` → ``{slide_n: set[eval_category]}``. extract_ground_truth(deck_path: Path) -> tuple[dict[int, str], dict[int, set[str]]] Convenience wrapper: reads the notes then classifies them. Returns ``(raw_notes, classified)``. Eval categories (the 6 fixed pass-bar units): confidentiality_statement slide_title_position table_labeling key_vs_legend spacing terminology """ from __future__ import annotations import re from pathlib import Path from services.notes_xml import read_notes_xml # --------------------------------------------------------------------------- # The 6 fixed eval categories # --------------------------------------------------------------------------- EVAL_CATEGORIES: list[str] = [ "confidentiality_statement", "slide_title_position", "table_labeling", "key_vs_legend", "spacing", "terminology", ] # --------------------------------------------------------------------------- # Regex rules — (pattern, eval_category) pairs, evaluated in order. # A slide note can match multiple categories. # --------------------------------------------------------------------------- # Each entry: (compiled_regex, eval_category) _RULES: list[tuple[re.Pattern[str], str]] = [ # key / legend → key_vs_legend (re.compile(r"\bkey\b|\blegend\b", re.IGNORECASE), "key_vs_legend"), # confidentiality / privacy → confidentiality_statement (re.compile(r"\bconfidentiality\b|\bprivacy\b", re.IGNORECASE), "confidentiality_statement"), # title moved / title length / title position → slide_title_position (re.compile(r"title\s+moved|title\s+length|title\s+position", re.IGNORECASE), "slide_title_position"), # %ile / Percentile / Company / Incumbent / Executive / header → table_labeling ( re.compile( r"%ile|Percentile|\bCompany\b|\bIncumbent\b|\bExecutive\b|\bheader\b", re.IGNORECASE, ), "table_labeling", ), # spacing / margin / .03 / indent → spacing (re.compile(r"\bspacing\b|\bmargin\b|\.03\b|\bindent\b", re.IGNORECASE), "spacing"), # TGT / Target / stats row / Count row / C.R Bard / punctuation → terminology # Also covers footnote-related terminology cues. ( re.compile( r"\bTGT\b|\bTarget\b|stats\s+row|count\s+row|C\.R\s+Bard|" r"\bpunctuation\b|\bfootnote\b|\bacro\b|\babbrev", re.IGNORECASE, ), "terminology", ), ] def classify_notes(notes: dict[int, str]) -> dict[int, set[str]]: """Classify each slide's note text into a set of eval categories. Parameters ---------- notes: ``{slide_n: raw_notes_text}`` — typically from :func:`read_notes_xml`. Returns ------- dict[int, set[str]] ``{slide_n: {eval_category, ...}}``. Only slides with at least one matching eval category are included. """ result: dict[int, set[str]] = {} for slide_n, text in notes.items(): cats: set[str] = set() for pattern, category in _RULES: if pattern.search(text): cats.add(category) if cats: result[slide_n] = cats return result def extract_ground_truth( deck_path: Path, ) -> tuple[dict[int, str], dict[int, set[str]]]: """Read notes from *deck_path* and classify into eval categories. Parameters ---------- deck_path: Path to the annotated ``.pptx`` file. Returns ------- (raw_notes, classified) ``raw_notes``: ``{slide_n: notes_text}`` (all slides with notes). ``classified``: ``{slide_n: set[eval_category]}`` (slides that matched at least one rule). """ raw_notes = read_notes_xml(Path(deck_path)) classified = classify_notes(raw_notes) return raw_notes, classified