File size: 4,194 Bytes
fdfe81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""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