tetrak_ocr.qa_score

Reference-free OCR quality metrics.

Two metrics are combined into a single score used to rank competing transcripts produced by different OCR backends, and to decide whether a document should be routed to the triage queue.

dictionary_coverage

Fraction of word tokens that are recognised English words, using pyspellchecker (pure Python, ~100 KB, no model downloads). Effective at catching non-word OCR noise (“tbe”, “h0use”, “rece1ved”).

perplexity_score

GPT-2 language model perplexity via the Hugging Face transformers library. Lower values mean more coherent English prose. The GPT-2 model (~500 MB) is downloaded on first use and cached at ~/.cache/huggingface/. The model and tokeniser are kept as module-level singletons so the cost is paid once per process.

combined_score

dict_coverage × (1 / log1p(perplexity)). Higher is better.

LowQualityError

Raised by tetrak_ocr.auto_local when the best backend’s combined_score falls below MIN_QUALITY_THRESHOLD. Carries the per-backend scores and raw transcripts so tetrak_ocr.batch can write a useful triage manifest.

These metrics read English, and only English. dictionary_coverage counts [a-zA-Z] tokens against an English wordlist and GPT-2 is an English language model, so a perfectly good transcript in another script scores 0.0 and lands below any threshold. Use scores_english_text before applying the gate to material that may not be English – see tetrak-ocr batch –quality-gate, which refuses rather than triaging a whole run.

Functions

combined_score(text)

Return a single quality score combining dictionary coverage and perplexity.

dictionary_coverage(text)

Return the fraction of word tokens that are recognised English words.

is_available()

True when every package the scoring path imports is installed.

perplexity_score(text)

Return the GPT-2 perplexity of the text.

scores_english_text(text)

Whether these metrics can say anything about text.

Exceptions

LowQualityError(message, scores, transcripts)

Raised when all OCR backends produce output below MIN_QUALITY_THRESHOLD.