#!/usr/bin/env python3
"""Recompute the published Oracle Max Hebrew result from its per-clip file.

Standard library only. Usage:

    python3 score_wer.py oracle-max-hebrew-fleurs-792-2026-11-21.csv

It prints the word error rate over all clips (errors divided by reference words, one
figure for the whole set), the 95% interval from a percentile bootstrap over clips, the
number of sentences with no error, and a recount of every clip's errors from the cleaned
texts in the file (word-level edit distance), which must equal the file's own counts.

The interval uses Python's random generator, seeded 20261121, 10,000 resamples. The
published interval was computed with numpy's generator and the same seed, so the two can
differ in the last digit; both are percentile intervals of the same kind.
"""
import csv
import random
import sys

SEED = 20261121
RESAMPLES = 10000


def edit_distance(ref, hyp):
    """Minimum substitutions + deletions + insertions turning ref into hyp (word lists)."""
    previous = list(range(len(hyp) + 1))
    for i, r in enumerate(ref, 1):
        current = [i]
        for j, h in enumerate(hyp, 1):
            current.append(min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + (r != h)))
        previous = current
    return previous[-1]


def percentile(sorted_values, q):
    """Linear interpolation between ranks, the same rule numpy.percentile uses."""
    position = q / 100 * (len(sorted_values) - 1)
    low = int(position)
    high = min(low + 1, len(sorted_values) - 1)
    return sorted_values[low] + (sorted_values[high] - sorted_values[low]) * (position - low)


def main(path):
    with open(path, newline="", encoding="utf-8") as handle:
        rows = list(csv.DictReader(handle))
    errors, words, recount_mismatches = [], [], 0
    for row in rows:
        substitutions, deletions = int(row["substitutions"]), int(row["deletions"])
        insertions, hits = int(row["insertions"]), int(row["hits"])
        errors.append(substitutions + deletions + insertions)
        words.append(substitutions + deletions + hits)
        reference = row["norm_reference_text"].split()
        hypothesis = row["norm_predicted_text"].split()
        if len(reference) != words[-1] or edit_distance(reference, hypothesis) != errors[-1]:
            recount_mismatches += 1
    n = len(rows)
    total_errors, total_words = sum(errors), sum(words)
    rng = random.Random(SEED)
    rates = []
    for _ in range(RESAMPLES):
        picks = [rng.randrange(n) for _ in range(n)]
        rates.append(sum(errors[i] for i in picks) / sum(words[i] for i in picks))
    rates.sort()
    print(f"clips: {n}")
    print(f"reference words: {total_words}")
    print(f"word errors: {total_errors}")
    print(f"word error rate: {100 * total_errors / total_words:.1f}%")
    print(f"95% interval: {100 * percentile(rates, 2.5):.1f}% to {100 * percentile(rates, 97.5):.1f}%"
          f" ({RESAMPLES} resamples over clips, seed {SEED})")
    print(f"sentences with no error: {sum(1 for e in errors if e == 0)}")
    print(f"recount of errors from the cleaned texts: {n - recount_mismatches} of {n} clips agree with the file")
    return 0 if recount_mismatches == 0 else 1


if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit(__doc__)
    sys.exit(main(sys.argv[1]))
