Datasets:

Languages:
English
ArXiv:
License:
File size: 10,493 Bytes
7527970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from __future__ import annotations

# ruff: noqa: E402

import argparse
import csv
import re
import sys
from pathlib import Path
from typing import Dict, Tuple

try:
    from scripts.hydrate_defextra import (
        _ensure_trailing_punct,
        _postprocess_text,
    )
except ModuleNotFoundError as exc:
    if exc.name != "scripts":
        raise
    PROJECT_ROOT = Path(__file__).resolve().parent.parent
    if str(PROJECT_ROOT) not in sys.path:
        sys.path.insert(0, str(PROJECT_ROOT))
    from scripts.hydrate_defextra import (
        _ensure_trailing_punct,
        _postprocess_text,
    )


def _normalize_text(text: str) -> str:
    if text is None:
        return ""
    value = text.replace("\u00ad", "")
    value = re.sub(r"([A-Za-z])-\s+([A-Za-z])", r"\1\2", value)
    value = re.sub(r"\s+", " ", value).strip()
    return value


def _normalize_punct(text: str) -> str:
    if text is None:
        return ""
    value = text.replace("\u00ad", "")
    value = re.sub(r"[^\w\s]", "", value)
    value = re.sub(r"\\s+", " ", value).strip()
    return value


def _load(path: Path) -> Dict[Tuple[str, str], Dict[str, str]]:
    with path.open(encoding="utf-8", newline="") as handle:
        reader = csv.DictReader(handle)
        return {(row["paper_id"], row["concept"]): row for row in reader}


def _row_flag(
    row: Dict[str, str],
    key: str,
    default: bool = False,
) -> bool:
    value = (row.get(key) or "").strip().lower()
    if not value:
        return default
    return value == "true"


def main() -> None:
    parser = argparse.ArgumentParser(
        description="Compare DefExtra hydrated CSV against reference.",
    )
    parser.add_argument(
        "--ref",
        type=Path,
        default=Path("results/paper_results/defextra_hf_tablefix.csv"),
        help="Reference CSV path.",
    )
    parser.add_argument(
        "--hyd",
        type=Path,
        default=Path(
            "results/paper_results/defextra_hydrated_tablefix_test.csv",
        ),
        help="Hydrated CSV path.",
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=5,
        help="Number of mismatches to print.",
    )
    parser.add_argument(
        "--report",
        type=Path,
        default=None,
        help="Optional path to write a detailed mismatch report.",
    )
    parser.add_argument(
        "--report-limit",
        type=int,
        default=0,
        help="Limit mismatches written to report (0 = all).",
    )
    parser.add_argument(
        "--legal",
        type=Path,
        default=Path("results/paper_results/defextra_legal_tablefix.csv"),
        help="Legal CSV with token counts and linebreak flags.",
    )
    parser.add_argument(
        "--postprocess",
        action="store_true",
        help="Apply hydration postprocessing to hydrated text before compare.",
    )
    args = parser.parse_args()

    ref = _load(args.ref)
    hyd = _load(args.hyd)
    legal = _load(args.legal) if args.postprocess else {}

    missing = [k for k in ref if k not in hyd]
    extra = [k for k in hyd if k not in ref]

    def_mismatch = []
    ctx_mismatch = []
    def_mismatch_norm = []
    ctx_mismatch_norm = []
    def_mismatch_punct = []
    ctx_mismatch_punct = []

    for key, ref_row in ref.items():
        hyd_row = hyd.get(key)
        if not hyd_row:
            continue
        if args.postprocess:
            legal_row = legal.get(key, {})
            def_expected = int(legal_row.get("definition_token_count") or 0)
            ctx_expected = int(legal_row.get("context_token_count") or 0)
            def_preserve = _row_flag(
                legal_row,
                "definition_preserve_linebreaks",
            )
            ctx_preserve = _row_flag(
                legal_row,
                "context_preserve_linebreaks",
            )
            def_preserve_hyphen = _row_flag(
                legal_row,
                "definition_preserve_hyphenation",
            )
            ctx_preserve_hyphen = _row_flag(
                legal_row,
                "context_preserve_hyphenation",
            )
            def_keep_bracket = _row_flag(
                legal_row,
                "definition_has_bracket_citation",
                True,
            )
            def_keep_paren = _row_flag(
                legal_row,
                "definition_has_paren_citation",
                True,
            )
            def_split_letter_digit = not _row_flag(
                legal_row,
                "definition_has_letter_digit",
            )
            ctx_keep_bracket = _row_flag(
                legal_row,
                "context_has_bracket_citation",
                True,
            )
            ctx_keep_paren = _row_flag(
                legal_row,
                "context_has_paren_citation",
                True,
            )
            ctx_split_letter_digit = not _row_flag(
                legal_row,
                "context_has_letter_digit",
            )
            hyd_row = dict(hyd_row)
            hyd_row["definition"] = _ensure_trailing_punct(
                _postprocess_text(
                    hyd_row.get("definition", ""),
                    def_expected,
                    def_preserve,
                    def_preserve_hyphen,
                    def_keep_bracket,
                    def_keep_paren,
                    def_split_letter_digit,
                ),
                legal_row.get("definition_end_punct", ""),
            )
            hyd_row["context"] = _ensure_trailing_punct(
                _postprocess_text(
                    hyd_row.get("context", ""),
                    ctx_expected,
                    ctx_preserve,
                    ctx_preserve_hyphen,
                    ctx_keep_bracket,
                    ctx_keep_paren,
                    ctx_split_letter_digit,
                ),
                legal_row.get("context_end_punct", ""),
            )
        if ref_row.get("definition", "") != hyd_row.get("definition", ""):
            def_mismatch.append(key)
            if _normalize_text(
                ref_row.get("definition", ""),
            ) != _normalize_text(
                hyd_row.get("definition", ""),
            ):
                def_mismatch_norm.append(key)
            if _normalize_punct(ref_row.get("definition", "")) == _normalize_punct(
                hyd_row.get("definition", ""),
            ):
                def_mismatch_punct.append(key)
        if ref_row.get("context", "") != hyd_row.get("context", ""):
            ctx_mismatch.append(key)
            if _normalize_text(ref_row.get("context", "")) != _normalize_text(
                hyd_row.get("context", ""),
            ):
                ctx_mismatch_norm.append(key)
            if _normalize_punct(ref_row.get("context", "")) == _normalize_punct(
                hyd_row.get("context", ""),
            ):
                ctx_mismatch_punct.append(key)

    total_ref = len(ref)
    total_hyd = len(hyd)
    print(f"Reference rows: {total_ref}")
    print(f"Hydrated rows: {total_hyd}")
    print(f"Missing keys: {len(missing)}")
    print(f"Extra keys: {len(extra)}")
    print(f"Definition mismatches (exact): {len(def_mismatch)}")
    print(f"Definition mismatches (normalized): {len(def_mismatch_norm)}")
    print(f"Context mismatches (exact): {len(ctx_mismatch)}")
    print(f"Context mismatches (normalized): {len(ctx_mismatch_norm)}")
    if def_mismatch_punct:
        print(
            "Definition mismatches (punctuation-only): "
            f"{len(def_mismatch_punct)}",
        )
    if ctx_mismatch_punct:
        print(
            "Context mismatches (punctuation-only): "
            f"{len(ctx_mismatch_punct)}",
        )

    if args.limit <= 0:
        return

    shown = 0
    for key in def_mismatch:
        if shown >= args.limit:
            break
        ref_row = ref[key]
        hyd_row = hyd[key]
        print("\nDefinition mismatch:", key)
        print("ref:", ref_row.get("definition", ""))
        print("hyd:", hyd_row.get("definition", ""))
        shown += 1

    shown = 0
    for key in ctx_mismatch:
        if shown >= args.limit:
            break
        ref_row = ref[key]
        hyd_row = hyd[key]
        print("\nContext mismatch:", key)
        print("ref:", ref_row.get("context", ""))
        print("hyd:", hyd_row.get("context", ""))
        shown += 1

    if args.report is not None:
        report_lines = []
        report_lines.append(f"Missing keys: {len(missing)}")
        report_lines.extend([f"- {k}" for k in missing])
        report_lines.append("")
        report_lines.append(
            f"Definition mismatches (exact): {len(def_mismatch)}"
        )
        report_lines.append(
            f"Definition mismatches (normalized): {len(def_mismatch_norm)}"
        )
        report_lines.append(
            f"Definition mismatches (punctuation-only): {len(def_mismatch_punct)}"
        )
        report_lines.append(
            f"Context mismatches (exact): {len(ctx_mismatch)}"
        )
        report_lines.append(
            f"Context mismatches (normalized): {len(ctx_mismatch_norm)}"
        )
        report_lines.append(
            f"Context mismatches (punctuation-only): {len(ctx_mismatch_punct)}"
        )
        report_lines.append("")

        def_limit = args.report_limit or len(def_mismatch)
        ctx_limit = args.report_limit or len(ctx_mismatch)

        report_lines.append("Definition mismatches:")
        for key in def_mismatch[:def_limit]:
            ref_row = ref[key]
            hyd_row = hyd[key]
            report_lines.append(f"- {key[0]} | {key[1]}")
            report_lines.append(f"  ref: {ref_row.get('definition','')}")
            report_lines.append(f"  hyd: {hyd_row.get('definition','')}")
        report_lines.append("")

        report_lines.append("Context mismatches:")
        for key in ctx_mismatch[:ctx_limit]:
            ref_row = ref[key]
            hyd_row = hyd[key]
            report_lines.append(f"- {key[0]} | {key[1]}")
            report_lines.append(f"  ref: {ref_row.get('context','')}")
            report_lines.append(f"  hyd: {hyd_row.get('context','')}")

        args.report.parent.mkdir(parents=True, exist_ok=True)
        args.report.write_text(
            "\n".join(report_lines) + "\n",
            encoding="utf-8",
        )
        print(f"Wrote report to {args.report}")


if __name__ == "__main__":
    main()