File size: 33,846 Bytes
1b62586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#!/usr/bin/env python3
"""Score SEGALE-aligned windows with COMET and aggregate document diagnostics.

This adapter follows SEGALE's document aggregation rule: every null alignment
receives a COMET score of 0 and remains in document and corpus means. It runs
only reference-based COMET; unavailable optional metrics are represented by an
explicit status and a JSON null value rather than a synthetic numeric score.
"""

from __future__ import annotations

import argparse
import hashlib
import importlib.metadata
import json
import platform
import re
import statistics
import time
from collections import Counter, defaultdict
from pathlib import Path
from typing import Iterable

import torch

# unbabel-comet 2.2.7 can select an unusable MPS DataLoader path on macOS even
# when CPU inference is requested. CUDA inference is unaffected by this guard.
torch.backends.mps.is_available = lambda: False

from comet import download_model, load_from_checkpoint  # noqa: E402


POSITION_BUCKETS = ("beginning", "middle", "end")


def read_jsonl(path: Path) -> list[dict]:
    with path.open(encoding="utf-8") as stream:
        return [json.loads(line) for line in stream if line.strip()]


def sha256_file(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            digest.update(block)
    return digest.hexdigest()


def classify_window(row: dict) -> str:
    src = row.get("src", "")
    ref = row.get("ref", "")
    hypothesis = row.get("tgt", "")
    if src and ref and hypothesis:
        return "aligned"
    if src and ref and not hypothesis:
        return "under_translation_null"
    if not src and not ref and hypothesis:
        return "over_translation_null"
    if src and not ref:
        return "canonical_null_reference"
    raise ValueError(
        f"Unexpected empty-field pattern in {row.get('doc_id')} segment {row.get('seg_id')}: "
        f"src={bool(src)} ref={bool(ref)} tgt={bool(hypothesis)}"
    )


def mean(values: Iterable[float | None]) -> float | None:
    present = [value for value in values if value is not None]
    return statistics.fmean(present) if present else None


def ratio(numerator: int, denominator: int) -> float | None:
    return numerator / denominator if denominator else None


def segment_sort_key(row: dict) -> tuple[int, int | str]:
    value = row.get("seg_id", 0)
    try:
        return (0, int(value))
    except (TypeError, ValueError):
        return (1, str(value))


def position_bucket(fraction: float) -> str:
    if fraction < 1 / 3:
        return "beginning"
    if fraction < 2 / 3:
        return "middle"
    return "end"


def assign_source_positions(
    doc_rows: list[dict], source_lines: list[str] | None = None
) -> None:
    """Assign source spans and positions, preferably against original sentences.

    VecAlign joins a multi-sentence source window with spaces.  Those inserted
    spaces are not present in the original BWB character coordinates, so probe
    and position calculations first recover each window's ordered source
    sentence span when the manifest carries the original source lines.
    """

    if source_lines is not None:
        if not isinstance(source_lines, list) or not all(
            isinstance(line, str) for line in source_lines
        ):
            raise ValueError("evaluation_source_lines must be a list of strings")
        sentence_cursor = 0
        char_prefix = [0]
        for line in source_lines:
            char_prefix.append(char_prefix[-1] + len(line))
        for row in doc_rows:
            source = row.get("src", "")
            if not source:
                sentence_start = sentence_cursor
                sentence_end = sentence_cursor
            else:
                target = normalized_exact_text(source)
                sentence_start = sentence_cursor
                sentence_end = None
                for candidate_end in range(sentence_cursor + 1, len(source_lines) + 1):
                    candidates = (
                        " ".join(source_lines[sentence_cursor:candidate_end]),
                        "".join(source_lines[sentence_cursor:candidate_end]),
                    )
                    if any(normalized_exact_text(value) == target for value in candidates):
                        sentence_end = candidate_end
                        break
                if sentence_end is None:
                    raise ValueError(
                        "Cannot map aligned source window to original source lines: "
                        f"cursor={sentence_cursor} src={source[:160]!r}"
                    )
                sentence_cursor = sentence_end
            row["source_sentence_start"] = sentence_start
            row["source_sentence_end"] = sentence_end
            row["source_char_start"] = char_prefix[sentence_start]
            row["source_char_end"] = char_prefix[sentence_end]
        if sentence_cursor != len(source_lines):
            raise ValueError(
                "Aligned source does not cover all original source lines: "
                f"covered={sentence_cursor} expected={len(source_lines)}"
            )
        source_total = char_prefix[-1]
    else:
        source_total = sum(len(row.get("src", "")) for row in doc_rows)

    source_cursor = 0
    for index, row in enumerate(doc_rows):
        if source_lines is None:
            source_chars = len(row.get("src", ""))
            start = source_cursor
            end = start + source_chars
            row["source_char_start"] = start
            row["source_char_end"] = end
        else:
            start = row["source_char_start"]
            end = row["source_char_end"]
            source_chars = end - start
        if source_total:
            fraction = (start + source_chars / 2) / source_total
        else:
            # A pathological all-empty-source document still gets deterministic
            # buckets, while runtime metadata records the normal basis.
            fraction = (index + 0.5) / len(doc_rows)
        row["source_position_fraction"] = fraction
        row["position_bucket"] = position_bucket(fraction)
        source_cursor = end


def normalized_exact_text(text: str) -> str:
    return re.sub(r"\s+", " ", text).strip()


def duplicate_stats(texts: Iterable[str], *, example_limit: int = 20) -> dict:
    normalized = [normalized_exact_text(text) for text in texts]
    normalized = [text for text in normalized if text]
    counts = Counter(normalized)
    duplicate_occurrences = sum(count - 1 for count in counts.values() if count > 1)
    examples = [text for text, count in counts.items() if count > 1]
    return {
        "items": len(normalized),
        "duplicate_occurrences": duplicate_occurrences,
        "duplicate_ratio": ratio(duplicate_occurrences, len(normalized)),
        "duplicate_unique_items": len(examples),
        "duplicate_examples": examples[:example_limit],
        "duplicate_examples_truncated": len(examples) > example_limit,
    }


def row_totals(rows: list[dict]) -> dict[str, int]:
    return {
        "source_chars": sum(len(row.get("src", "")) for row in rows),
        "reference_chars": sum(len(row.get("ref", "")) for row in rows),
        "hypothesis_chars": sum(len(row.get("tgt", "")) for row in rows),
    }


def length_fields(totals: dict[str, int]) -> dict:
    source_chars = totals["source_chars"]
    reference_chars = totals["reference_chars"]
    hypothesis_chars = totals["hypothesis_chars"]
    return {
        **totals,
        "reference_source_char_ratio": ratio(reference_chars, source_chars),
        "hypothesis_source_char_ratio": ratio(hypothesis_chars, source_chars),
        "hypothesis_reference_char_ratio": ratio(hypothesis_chars, reference_chars),
    }


def null_text_statistics(rows: list[dict]) -> dict:
    """Reporting only: character mass in null blocks, never scoring weights."""
    evaluable = [row for row in rows if row['alignment_type'] != 'canonical_null_reference']
    source_chars = sum(len(row.get('src', '')) for row in evaluable)
    hypothesis_chars = sum(len(row.get('tgt', '')) for row in evaluable)
    under_chars = sum(len(row['src']) for row in evaluable
                      if row['alignment_type'] == 'under_translation_null')
    over_chars = sum(len(row['tgt']) for row in evaluable
                     if row['alignment_type'] == 'over_translation_null')
    return {
        'evaluable_source_chars': source_chars,
        'evaluable_hypothesis_chars': hypothesis_chars,
        'under_null_source_chars': under_chars,
        'over_null_hypothesis_chars': over_chars,
        'null_source_char_ratio': ratio(under_chars, source_chars),
        'null_hypothesis_char_ratio': ratio(over_chars, hypothesis_chars),
    }


def summarize_bucket(rows: list[dict]) -> dict:
    evaluable_rows = [row for row in rows if row["alignment_type"] != "canonical_null_reference"]
    null_rows = [row for row in evaluable_rows if row["alignment_type"] != "aligned"]
    under_nulls = sum(
        row["alignment_type"] == "under_translation_null" for row in rows
    )
    over_nulls = sum(
        row["alignment_type"] == "over_translation_null" for row in rows
    )
    totals = row_totals(rows)
    return {
        "windows": len(rows),
        "evaluable_windows": len(evaluable_rows),
        "canonical_null_reference_windows": len(rows) - len(evaluable_rows),
        "comet": mean(row["comet"] for row in evaluable_rows),
        "na_ratio": ratio(len(null_rows), len(evaluable_rows)),
        "under_translation_nulls": under_nulls,
        "over_translation_nulls": over_nulls,
        "under_translation_na_ratio": ratio(under_nulls, len(evaluable_rows)),
        "over_translation_na_ratio": ratio(over_nulls, len(evaluable_rows)),
        **length_fields(totals),
    }


def summarize_probe(doc_rows: list[dict], probe: dict | None) -> dict | None:
    if not probe:
        return None
    sentence_start = probe.get("source_sentence_start")
    sentence_end = probe.get("source_sentence_end")
    if isinstance(sentence_start, int) and isinstance(sentence_end, int):
        if not 0 <= sentence_start < sentence_end:
            raise ValueError(f"Invalid probe source sentence span: {probe}")
        mapped_total = max(
            (row.get("source_sentence_end", 0) for row in doc_rows), default=0
        )
        if sentence_end > mapped_total:
            raise ValueError(
                "Probe sentence span ends after aligned source: "
                f"end={sentence_end} source_total={mapped_total}"
            )
        selected = []
        boundary_crossings = 0
        for row in doc_rows:
            row_start = row.get("source_sentence_start")
            row_end = row.get("source_sentence_end")
            if not isinstance(row_start, int) or not isinstance(row_end, int):
                raise ValueError("Probe sentence selection requires mapped source windows")
            if row_end > row_start:
                midpoint = (row_start + row_end) / 2
                include = sentence_start <= midpoint < sentence_end
                boundary_crossings += int(
                    row_start < sentence_start < row_end
                    or row_start < sentence_end < row_end
                )
            else:
                include = sentence_start <= row_start < sentence_end or (
                    row_start == sentence_end == mapped_total
                )
            if include:
                selected.append(row)
        if not selected:
            raise ValueError(f"Probe selected no aligned windows: {probe}")
        return {
            "probe_id": probe.get("probe_id"),
            "source_sha256": probe.get("source_sha256"),
            "source_char_start": probe.get("source_char_start"),
            "source_char_end": probe.get("source_char_end"),
            "source_sentence_start": sentence_start,
            "source_sentence_end": sentence_end,
            "selection_rule": (
                "mapped_source_sentence_window_midpoint; source-empty window at "
                "sentence insertion cursor, including document-end cursor"
            ),
            "boundary_crossing_windows": boundary_crossings,
            **summarize_bucket(selected),
        }

    start = probe.get("source_char_start")
    end = probe.get("source_char_end")
    if not isinstance(start, int) or not isinstance(end, int) or not 0 <= start < end:
        raise ValueError(f"Invalid probe source span: {probe}")
    source_total = sum(len(row.get("src", "")) for row in doc_rows)
    if end > source_total:
        raise ValueError(
            f"Probe span ends after aligned source: end={end} source_total={source_total}"
        )

    selected = []
    boundary_crossings = 0
    for row in doc_rows:
        row_start = row["source_char_start"]
        row_end = row["source_char_end"]
        if row_end > row_start:
            midpoint = (row_start + row_end) / 2
            include = start <= midpoint < end
            boundary_crossings += int(
                row_start < start < row_end or row_start < end < row_end
            )
        else:
            # Source-empty over-translation windows are attached to the source
            # cursor at which the aligner inserted them.
            include = start <= row_start < end or row_start == end == source_total
        if include:
            selected.append(row)
    if not selected:
        raise ValueError(f"Probe selected no aligned windows: {probe}")
    return {
        "probe_id": probe.get("probe_id"),
        "source_sha256": probe.get("source_sha256"),
        "source_char_start": start,
        "source_char_end": end,
        "selection_rule": (
            "source_window_midpoint; source-empty window at insertion cursor, "
            "including document-end cursor"
        ),
        "boundary_crossing_windows": boundary_crossings,
        **summarize_bucket(selected),
    }


def summarize_case(
    doc_id: str,
    doc_rows: list[dict],
    case_metadata: dict,
    sentence_segmenter=None,
    hypothesis_sentences: list[str] | None = None,
) -> dict:
    evaluable_rows = [row for row in doc_rows if row["alignment_type"] != "canonical_null_reference"]
    null_rows = [row for row in evaluable_rows if row["alignment_type"] != "aligned"]
    aligned_rows = [row for row in doc_rows if row["alignment_type"] == "aligned"]
    position = {
        bucket: summarize_bucket(
            [row for row in doc_rows if row["position_bucket"] == bucket]
        )
        for bucket in POSITION_BUCKETS
    }

    if hypothesis_sentences is None:
        hypothesis_text = "\n".join(
            row.get("tgt", "") for row in doc_rows if row.get("tgt", "")
        )
        hypothesis_sentences = [
            normalized_exact_text(sentence.text)
            for sentence in sentence_segmenter(hypothesis_text).sents
            if sentence.text.strip()
        ]
    sentence_duplicates = duplicate_stats(hypothesis_sentences)
    window_duplicates = duplicate_stats(row.get("tgt", "") for row in doc_rows)
    totals = row_totals(doc_rows)
    benchmark_metadata = case_metadata.get("benchmark_metadata")
    probe = (
        benchmark_metadata.get("probe")
        if isinstance(benchmark_metadata, dict)
        else None
    )

    return {
        "case_id": case_metadata.get("case_id", doc_id),
        "segale_doc_id": doc_id,
        "operation": case_metadata.get("operation", "none"),
        "affected_segments": case_metadata.get("affected_segments", []),
        "benchmark_metadata": benchmark_metadata,
        "metadata": case_metadata.get("metadata", {}),
        "windows": len(doc_rows),
        "evaluable_windows": len(evaluable_rows),
        "canonical_null_reference_windows": len(doc_rows) - len(evaluable_rows),
        "aligned_windows": len(aligned_rows),
        "null_windows": len(null_rows),
        "under_translation_nulls": sum(
            row["alignment_type"] == "under_translation_null" for row in doc_rows
        ),
        "over_translation_nulls": sum(
            row["alignment_type"] == "over_translation_null" for row in doc_rows
        ),
        "na_ratio": ratio(len(null_rows), len(evaluable_rows)),
        "comet": mean(row["comet"] for row in evaluable_rows),
        "comet_aligned_only": mean(row["comet"] for row in aligned_rows),
        "diagnostics": null_text_statistics(doc_rows),
        **length_fields(totals),
        "hypothesis_sentences": sentence_duplicates["items"],
        "exact_duplicate_sentence_occurrences": sentence_duplicates[
            "duplicate_occurrences"
        ],
        "exact_duplicate_sentence_ratio": sentence_duplicates["duplicate_ratio"],
        "exact_duplicate_sentence_unique_items": sentence_duplicates[
            "duplicate_unique_items"
        ],
        "exact_duplicate_sentence_examples": sentence_duplicates["duplicate_examples"],
        "exact_duplicate_sentence_examples_truncated": sentence_duplicates[
            "duplicate_examples_truncated"
        ],
        "nonempty_hypothesis_windows": window_duplicates["items"],
        "exact_duplicate_window_occurrences": window_duplicates["duplicate_occurrences"],
        "exact_duplicate_window_ratio": window_duplicates["duplicate_ratio"],
        "exact_duplicate_window_unique_items": window_duplicates[
            "duplicate_unique_items"
        ],
        "exact_duplicate_window_examples": window_duplicates["duplicate_examples"],
        "exact_duplicate_window_examples_truncated": window_duplicates[
            "duplicate_examples_truncated"
        ],
        "position_buckets": position,
        "probe": summarize_probe(doc_rows, probe),
    }


def aggregate_cases(cases: list[dict], rows: list[dict]) -> dict:
    totals = row_totals(rows)
    evaluable_rows = [row for row in rows if row["alignment_type"] != "canonical_null_reference"]
    null_rows = [row for row in evaluable_rows if row["alignment_type"] != "aligned"]
    aligned_rows = [row for row in rows if row["alignment_type"] == "aligned"]

    macro = {
        "comet": mean(case["comet"] for case in cases),
        "comet_aligned_only": mean(case["comet_aligned_only"] for case in cases),
        "na_ratio": mean(case["na_ratio"] for case in cases),
        "reference_source_char_ratio": mean(
            case["reference_source_char_ratio"] for case in cases
        ),
        "hypothesis_source_char_ratio": mean(
            case["hypothesis_source_char_ratio"] for case in cases
        ),
        "hypothesis_reference_char_ratio": mean(
            case["hypothesis_reference_char_ratio"] for case in cases
        ),
        "exact_duplicate_sentence_ratio": mean(
            case["exact_duplicate_sentence_ratio"] for case in cases
        ),
        "exact_duplicate_window_ratio": mean(
            case["exact_duplicate_window_ratio"] for case in cases
        ),
        "position_buckets": {
            bucket: {
                "comet": mean(
                    case["position_buckets"][bucket]["comet"] for case in cases
                ),
                "na_ratio": mean(
                    case["position_buckets"][bucket]["na_ratio"] for case in cases
                ),
                "hypothesis_reference_char_ratio": mean(
                    case["position_buckets"][bucket][
                        "hypothesis_reference_char_ratio"
                    ]
                    for case in cases
                ),
            }
            for bucket in POSITION_BUCKETS
        },
    }

    sentence_count = sum(case["hypothesis_sentences"] for case in cases)
    sentence_duplicates = sum(
        case["exact_duplicate_sentence_occurrences"] for case in cases
    )
    window_count = sum(case["nonempty_hypothesis_windows"] for case in cases)
    window_duplicates = sum(
        case["exact_duplicate_window_occurrences"] for case in cases
    )
    weighted = {
        "comet": mean(row["comet"] for row in evaluable_rows),
        "comet_aligned_only": mean(row["comet"] for row in aligned_rows),
        "na_ratio": ratio(len(null_rows), len(evaluable_rows)),
        **length_fields(totals),
        "hypothesis_sentences": sentence_count,
        "exact_duplicate_sentence_occurrences": sentence_duplicates,
        "exact_duplicate_sentence_ratio": ratio(sentence_duplicates, sentence_count),
        "nonempty_hypothesis_windows": window_count,
        "exact_duplicate_window_occurrences": window_duplicates,
        "exact_duplicate_window_ratio": ratio(window_duplicates, window_count),
        "position_buckets": {
            bucket: summarize_bucket(
                [row for row in rows if row["position_bucket"] == bucket]
            )
            for bucket in POSITION_BUCKETS
        },
    }
    return {
        "documents": len(cases),
        "windows": len(rows),
        "evaluable_windows": len(evaluable_rows),
        "canonical_null_reference_windows": len(rows) - len(evaluable_rows),
        "aligned_windows": len(aligned_rows),
        "null_windows": len(null_rows),
        "macro": macro,
        "weighted": weighted,
    }


def package_versions() -> dict[str, str | None]:
    versions = {}
    for package in ("segale", "spacy", "transformers", "unbabel-comet", "numpy"):
        try:
            versions[package] = importlib.metadata.version(package)
        except importlib.metadata.PackageNotFoundError:
            versions[package] = None
    return versions


def metric_statuses(aggregate: dict) -> dict:
    not_requested = {
        "status": "not_requested",
        "availability": "not_run_in_this_experiment",
        "value": None,
    }
    unavailable = {
        "status": "unavailable",
        "availability": "unavailable_in_comet_only_evaluator",
        "value": None,
    }
    return {
        "comet": {
            "status": "ok",
            "value": {
                "macro": aggregate["macro"]["comet"],
                "weighted": aggregate["weighted"]["comet"],
            },
            "null_alignment_score": 0.0,
        },
        "comet_qe": dict(not_requested),
        "metricx": dict(unavailable),
        "metricx_qe": dict(unavailable),
    }


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--input-file", type=Path, required=True)
    parser.add_argument("--manifest", type=Path, required=True)
    parser.add_argument("--output-dir", type=Path, required=True)
    parser.add_argument("--model", default="Unbabel/wmt22-comet-da")
    parser.add_argument(
        "--model-checkpoint",
        type=Path,
        help="Local COMET .ckpt; when set, skip model download/resolution",
    )
    parser.add_argument(
        "--model-checkpoint-sha256",
        help="Preverified checkpoint SHA-256 supplied by the runner",
    )
    parser.add_argument("--batch-size", type=int, default=4)
    parser.add_argument(
        "--gpus",
        type=int,
        default=0,
        help="Number of CUDA GPUs passed to COMET (0 keeps CPU behavior)",
    )
    parser.add_argument("--spacy-model", default="es_core_news_sm")
    parser.add_argument(
        "--target-sentences",
        type=Path,
        help="Optional target sentence sidecar emitted by the aligner",
    )
    parser.add_argument("--encoder-model", type=Path, help="Pinned local XLM-R tokenizer/config")
    args = parser.parse_args()

    if args.gpus < 0:
        parser.error("--gpus must be non-negative")
    if args.batch_size < 1:
        parser.error("--batch-size must be positive")
    if args.model_checkpoint_sha256 and not re.fullmatch(
        r"[a-f0-9]{64}", args.model_checkpoint_sha256
    ):
        parser.error("--model-checkpoint-sha256 must be a lowercase SHA-256")

    total_started = time.perf_counter()

    rows = read_jsonl(args.input_file)
    if not rows:
        raise ValueError(f"No rows in {args.input_file}")

    grouped: dict[str, list[dict]] = defaultdict(list)
    for row in rows:
        if "doc_id" not in row:
            raise ValueError(f"Aligned row has no doc_id: {row}")
        row["alignment_type"] = classify_window(row)
        grouped[str(row["doc_id"])].append(row)
    for doc_rows in grouped.values():
        doc_rows.sort(key=segment_sort_key)

    manifest = json.loads(args.manifest.read_text(encoding="utf-8"))
    manifest_cases = manifest.get("cases", [])
    manifest_case_ids = [str(case["case_id"]) for case in manifest_cases]
    if len(manifest_case_ids) != len(set(manifest_case_ids)):
        raise ValueError("Manifest contains duplicate case_id values")
    manifest_doc_ids = [str(case.get("segale_doc_id", case["case_id"])) for case in manifest_cases]
    if len(manifest_doc_ids) != len(set(manifest_doc_ids)):
        raise ValueError("Manifest contains duplicate SEGALE document IDs")
    aligned_doc_ids = set(grouped)
    expected_doc_ids = set(manifest_doc_ids)
    if aligned_doc_ids != expected_doc_ids:
        raise ValueError(
            "Aligned/manifest document coverage mismatch: "
            f"missing={sorted(expected_doc_ids - aligned_doc_ids)} "
            f"unexpected={sorted(aligned_doc_ids - expected_doc_ids)}"
        )
    for doc_id, doc_rows in grouped.items():
        segment_ids = []
        for row in doc_rows:
            if "seg_id" not in row:
                raise ValueError(f"Aligned row has no seg_id in document {doc_id}")
            segment_ids.append(str(row["seg_id"]))
        if len(segment_ids) != len(set(segment_ids)):
            raise ValueError(f"Aligned document {doc_id} contains duplicate seg_id values")

    cases_by_id = {
        str(case.get("segale_doc_id", case["case_id"])): case
        for case in manifest_cases
    }
    mapped_source_documents = 0
    for doc_id, doc_rows in grouped.items():
        source_lines = cases_by_id[doc_id].get("evaluation_source_lines")
        mapped_source_documents += isinstance(source_lines, list)
        assign_source_positions(doc_rows, source_lines)
    rows = [row for doc_rows in grouped.values() for row in doc_rows]

    scorable = []
    scorable_rows = []
    for row in rows:
        if row["alignment_type"] == "aligned":
            scorable.append({"src": row["src"], "ref": row["ref"], "mt": row["tgt"]})
            scorable_rows.append(row)
        elif row["alignment_type"] in {"under_translation_null", "over_translation_null"}:
            row["comet"] = 0.0
        else:
            row["comet"] = None

    model_path: Path | None = None
    if args.model_checkpoint:
        # Preserve snapshot symlinks: COMET locates hparams.yaml relative to
        # the checkpoint path rather than its resolved blob-store target.
        model_path = args.model_checkpoint.expanduser().absolute()
        if not model_path.is_file():
            raise FileNotFoundError(f"COMET checkpoint does not exist: {model_path}")
    if scorable:
        if model_path is None:
            model_path = Path(download_model(args.model))
        model_load_started = time.perf_counter()
        # Portable release: use the pinned local encoder config/tokenizer.
        if args.encoder_model:
            from comet.models import RegressionMetric
            model = RegressionMetric.load_from_checkpoint(str(model_path),
                map_location=torch.device("cpu"), strict=False, load_pretrained_weights=False,
                pretrained_model=str(args.encoder_model), local_files_only=True)
        else:
            model = load_from_checkpoint(str(model_path))
        model_load_seconds = time.perf_counter() - model_load_started
        prediction_started = time.perf_counter()
        prediction = model.predict(
            scorable,
            batch_size=args.batch_size,
            gpus=args.gpus,
            num_workers=0,
        )
        prediction_seconds = time.perf_counter() - prediction_started
        scores = prediction.scores if hasattr(prediction, "scores") else prediction["scores"]
        for row, score in zip(scorable_rows, scores, strict=True):
            row["comet"] = float(score)
    else:
        model_load_seconds = 0.0
        prediction_seconds = 0.0

    aggregation_started = time.perf_counter()
    target_sentences_by_doc = None
    if args.target_sentences:
        target_rows = read_jsonl(args.target_sentences)
        target_sentences_by_doc = {}
        for row in target_rows:
            doc_id = str(row.get("doc_id"))
            sentences = row.get("sentences")
            if doc_id in target_sentences_by_doc:
                raise ValueError(f"Duplicate target sentence document: {doc_id}")
            if not isinstance(sentences, list) or not all(
                isinstance(sentence, str) for sentence in sentences
            ):
                raise ValueError(f"Invalid target sentence document: {doc_id}")
            target_sentences_by_doc[doc_id] = sentences
        if set(target_sentences_by_doc) != expected_doc_ids:
            raise ValueError("Target sentence/manifest document coverage mismatch")
        for doc_id, sentences in target_sentences_by_doc.items():
            sidecar_text = normalized_exact_text(" ".join(sentences))
            aligned_text = normalized_exact_text(
                " ".join(
                    row.get("tgt", "")
                    for row in grouped[doc_id]
                    if row.get("tgt", "")
                )
            )
            if sidecar_text != aligned_text:
                raise ValueError(
                    f"Target sentence/aligned text mismatch: {doc_id}"
                )
        sentence_segmenter = None
    else:
        import spacy

        sentence_segmenter = spacy.load(args.spacy_model)
    summaries = [
        summarize_case(
            doc_id,
            doc_rows,
            cases_by_id.get(doc_id, {}),
            sentence_segmenter,
            target_sentences_by_doc.get(doc_id)
            if target_sentences_by_doc is not None
            else None,
        )
        for doc_id, doc_rows in grouped.items()
    ]
    manifest_order = {str(case["case_id"]): index for index, case in enumerate(manifest_cases)}
    summaries.sort(
        key=lambda item: (
            manifest_order.get(item["case_id"], len(manifest_order)),
            item["case_id"],
        )
    )
    aggregate = aggregate_cases(summaries, rows)
    aggregation_seconds = time.perf_counter() - aggregation_started

    args.output_dir.mkdir(parents=True, exist_ok=True)
    per_window_path = args.output_dir / "per_window.jsonl"
    with per_window_path.open("w", encoding="utf-8") as stream:
        for row in rows:
            stream.write(json.dumps(row, ensure_ascii=False) + "\n")

    if mapped_source_documents == len(grouped):
        position_basis = (
            "window_midpoint_in_original_source_characters_after_ordered_sentence_mapping"
        )
    elif mapped_source_documents == 0:
        position_basis = "window_midpoint_in_cumulative_aligned_source_characters"
    else:
        position_basis = (
            "per-document: original source characters when manifest sentences are "
            "available, otherwise cumulative aligned source characters"
        )
    runtime = {
        "platform": platform.platform(),
        "machine": platform.machine(),
        "python": platform.python_version(),
        "torch": torch.__version__,
        "packages": package_versions(),
        "comet_model": args.model,
        "comet_checkpoint": str(model_path) if model_path else None,
        "comet_checkpoint_sha256": (
            args.model_checkpoint_sha256
            if model_path and args.model_checkpoint_sha256
            else sha256_file(model_path) if model_path else None
        ),
        "comet_device": "cuda" if args.gpus else "cpu",
        "comet_gpus": args.gpus,
        "comet_batch_size": args.batch_size,
        "sentence_segmenter": args.spacy_model,
        "position_bucket_basis": position_basis,
        "source_sentence_mapped_documents": mapped_source_documents,
        "target_sentences": (
            str(args.target_sentences.absolute()) if args.target_sentences else None
        ),
        "target_sentences_sha256": (
            sha256_file(args.target_sentences) if args.target_sentences else None
        ),
        "target_sentence_source": (
            "alignment_sidecar" if args.target_sentences else "scorer_spacy"
        ),
        "probe_window_basis": (
            "mapped source-sentence-window midpoint; source-empty over-translation "
            "window at sentence insertion cursor including document end; legacy "
            "character fallback"
        ),
        "exact_duplicate_basis": "case-sensitive_text_after_whitespace_normalization",
        "aligned_input": str(args.input_file.absolute()),
        "aligned_input_sha256": sha256_file(args.input_file),
        "manifest": str(args.manifest.absolute()),
        "manifest_sha256": sha256_file(args.manifest),
        "phase_seconds": {
            "model_load": model_load_seconds,
            "prediction": prediction_seconds,
            "aggregation": aggregation_seconds,
            "total": time.perf_counter() - total_started,
        },
    }
    result = {
        "experiment": manifest.get("experiment", {}),
        "runtime": runtime,
        "metrics": metric_statuses(aggregate),
        "aggregate": aggregate,
        "cases": summaries,
    }
    (args.output_dir / "summary.json").write_text(
        json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
    )
    print(
        f"Scored {len(rows)} windows across {len(summaries)} cases; "
        f"COMET macro={aggregate['macro']['comet']:.6f}, "
        f"weighted={aggregate['weighted']['comet']:.6f}"
    )


if __name__ == "__main__":
    main()