"""Scores a transcript against an answer key: the word error rate we report.

    pip install jiwer whisper-normalizer
    python score.py answer-key-8-minutes.txt transcript.txt

Both texts go through Whisper's own English normalizer (lower case, no
punctuation, numbers as digits), plus a few British spellings the book uses,
so only the words count. The answer key is Lionel Giles's translation as
Project Gutenberg publishes it without commentary (eBook #17405), with the
LibriVox announcements the reader says and the three sentences she skips left
out.
"""
import sys

import jiwer
from whisper_normalizer.english import EnglishTextNormalizer

spellings = {
    "skilful": "skillful", "skilfully": "skillfully", "manoeuvering": "maneuvering",
    "manoeuvre": "maneuver", "manoeuvres": "maneuvers", "manoeuvring": "maneuvering",
    "marshalling": "marshaling", "hearkens": "harkens", "succour": "succor",
}
whisper = EnglishTextNormalizer()


def words(text):
    text = text.replace("’", "'").replace("‘", "'")
    return " ".join(spellings.get(w, w) for w in whisper(text).split())


key = words(open(sys.argv[1], encoding="utf-8").read())
heard = words(open(sys.argv[2], encoding="utf-8").read())
result = jiwer.process_words(key, heard)
print(f"Word error rate: {result.wer * 100:.2f}%")
print(f"{result.substitutions} wrong, {result.deletions} missing, {result.insertions} extra, out of {len(key.split())} words")
