Spaces:
Build error

trackademi / tests /test_text_quality.py
anwarKXC
feat: implement Arabic text-layer trustworthiness scoring and add tests
6bb4af3
Raw History Blame Contribute Delete
9.51 kB
# -*- coding: utf-8 -*-
"""Text-layer trustworthiness scoring.
The bad samples are verbatim chunks from the 2026-08-05 biology book
(document aa28b229-โ€ฆ), which extracted 252 pages of Arabic-looking gibberish
through a broken font cmap and was indexed as if it had succeeded. They are
the regression the scorer exists to catch.
"""
from app.services.text_quality import (
TextLayerVerdict,
assess_pdf_text_layer,
score_text_quality,
select_sample_pages,
)
# Real corrupted extraction output โ€” broken glyph map, visually plausible,
# semantically unreadable. Note the digits welded inside words (ุณุŒ3ุงู„ุฏุงุฑ).
SCRAMBLED = [
"ู…ู† ูุฑุงุบูƒ ุจุนุฏ ุณุŒ3ุงู„ุฏุงุฑ ุนุฒูŠุฒูŠ ุนู„ูŠ ู‚ุงุฏุฑุง ุชูƒูˆู† ุงุงู† ูŠู†ุจุบูŠ ุงู„ูˆุญุฏู‡ุŒ ู‡ุฐู‡ ุตู‡3ุฏุฑุง "
":ุงุงู† ุณูŠุฌู„ู† ุบุฑูŠ ู„ุชูƒุงุซุฑูˆ ุณูŠุฌู„ู† ู„ุชูƒุงุซุฑ ุจู†ูŠ ุชูุฑู‚ . 1 .)(ุณูŠู„ุงู„ุฌู† .2ุนูŠูˆุจู‡ ูˆุชุจู†ูŠ "
"ุฆุฏู‡ูˆููˆ ุณูŠุฌู„ู† ุบุฑูŠ ู„ุชูƒุงุซุฑ ุตุณุงุฆุฎ ุชุนุฏุฏ .ู…ู„ุฎุชู„ูู‡ ุณูŠุฌู„ู† ุบุฑ",
"ุซุงู†ูˆูŠ ุซุงู„ุซ - ุงู„ุงุญูŠุงุก 2 ุณูŠุงุฌู„ู† ุบุฑูŠ ุงู„ุชูƒุงุซุฑ ููˆุงุฆุฏ 2 . 1 .ูˆุฌูŠุฒ ูˆู‚ุช ูŠู ูƒุซุฑูŠู‡ "
"ุงูุฑุงุฏ ุงู†ุชุงุฌ 1 .1 .ุฌุฏูŠุฏู‡ ุจูŠุฆู‡ ู„ุงุนู…ุงุฑ ูˆุงุญุฏ ูุฑุฏ ูˆุฌูˆุฏ ูŠูƒููŠ2 .2 ุงู…ู„ุญุงูุธู‡ ุชุชู… "
"ูˆุจู‡ุฐุง ุงู„ุงู…ุŒ ู„ู„ู†ุจุงุช ู…ุทุงุจู‚ุง ูŠูƒูˆู† ู†ุจุงุช ุนู„ูŠ ุตูˆู„ุงุญู„3 .3 .ุงู„ุงู… ุงู„ู†ุจุงุช ูŠู ุงู…ู„ุชูˆูุฑู‡",
"ู„ุจูŠุฆูŠู‡ ู„ุธุฑูˆู ุชูƒูˆู† ูˆุนู†ุฏู…ุง . ุฒูŠุฌูŠู‡ ุบุงุงุจูˆ ู…ูƒูˆู†ู‡ ุŒ)ุจูˆุบูŠ ุตูƒูŠ( ุจูˆุบูŠู‡ .ุกู„ู‡ูˆ ูŠู "
"ุฑุดูˆุชู†ุช ุบู„ุงุจูˆ ูˆุชุฎุฑุฌ ู„ุจูˆุบูŠู‡ ุญู„ุงูุธู‡ ุชู†ูุฌุฑ ุณุจู‡3ู…ู„ู†ุง ู„ุจูŠุฆูŠู‡ ู„ุธุฑูˆูุฑูˆ ูุฑุชุฑ ูˆุชูˆ "
"ุงุฐูŠ ุฎู„ูŠู‡ ูƒู„ ูˆุชู†ู…ูˆ ุฎู„ูŠุชู†ูŠ ุงูŠู„ ู„ุงู… ุฎู„ู„ูŠู‡ ุณู…ุชู†ู‚ ุญูŠุซ",
]
# Ordinary Arabic textbook prose.
CLEAN = [
"ุงู„ูˆุญุฏุฉ ุงู„ุฃูˆู„ู‰: ุงู„ุชูƒุงุซุฑ ููŠ ุงู„ูƒุงุฆู†ุงุช ุงู„ุญูŠุฉ. ูŠุนุฏ ุงู„ุชูƒุงุซุฑ ู…ู† ุฃู‡ู… ุงู„ุฎุตุงุฆุต ุงู„ุชูŠ "
"ุชู…ูŠุฒ ุงู„ูƒุงุฆู†ุงุช ุงู„ุญูŠุฉุŒ ูู‡ูˆ ุงู„ุนู…ู„ูŠุฉ ุงู„ุชูŠ ุชุถู…ู† ุงุณุชู…ุฑุงุฑ ุงู„ู†ูˆุน ูˆุงู„ุญูุงุธ ุนู„ูŠู‡ ู…ู† "
"ุงู„ุงู†ู‚ุฑุงุถ. ูˆูŠู†ู‚ุณู… ุงู„ุชูƒุงุซุฑ ุฅู„ู‰ ู†ูˆุนูŠู† ุฑุฆูŠุณูŠูŠู† ู‡ู…ุง ุงู„ุชูƒุงุซุฑ ุงู„ู„ุงุฌู†ุณูŠ ูˆุงู„ุชูƒุงุซุฑ "
"ุงู„ุฌู†ุณูŠ. ูˆููŠ ู‡ุฐุง ุงู„ุฏุฑุณ ุณูˆู ู†ุฏุฑุณ ุงู„ูุฑูˆู‚ ุจูŠู† ุงู„ู†ูˆุนูŠู†ุŒ ูƒู…ุง ู†ุชุนุฑู ุนู„ู‰ ููˆุงุฆุฏ ูƒู„ "
"ู…ู†ู‡ู…ุง ูˆุนูŠูˆุจู‡. ูˆู…ู† ุงู„ู…ุนุฑูˆู ุฃู† ุงู„ุชูƒุงุซุฑ ุงู„ู„ุงุฌู†ุณูŠ ู„ุง ูŠุญุชุงุฌ ุฅู„ู‰ ูˆุฌูˆุฏ ูุฑุฏูŠู†.",
"ุงู„ูุตู„ ุงู„ุซุงู†ูŠ: ุงู„ูˆุฑุงุซุฉ. ุชู‡ุชู… ุงู„ูˆุฑุงุซุฉ ุจุฏุฑุงุณุฉ ูƒูŠููŠุฉ ุงู†ุชู‚ุงู„ ุงู„ุตูุงุช ู…ู† ุงู„ุขุจุงุก "
"ุฅู„ู‰ ุงู„ุฃุจู†ุงุก ุนุจุฑ ุงู„ุฃุฌูŠุงู„. ูˆู‚ุฏ ูˆุถุน ุงู„ุนุงู„ู… ู…ู†ุฏู„ ุงู„ู‚ูˆุงู†ูŠู† ุงู„ุฃุณุงุณูŠุฉ ุงู„ุชูŠ ุชูุณุฑ "
"ู‡ุฐุง ุงู„ุงู†ุชู‚ุงู„ุŒ ุจุนุฏ ุฃู† ุฃุฌุฑู‰ ุชุฌุงุฑุจู‡ ุงู„ู…ุดู‡ูˆุฑุฉ ุนู„ู‰ ู†ุจุงุช ุงู„ุจุงุฒู„ุงุก. ูˆู…ู† ุฎู„ุงู„ ู‡ุฐู‡ "
"ุงู„ุชุฌุงุฑุจ ุงุณุชู†ุชุฌ ุฃู† ุงู„ุตูุงุช ุชู†ุชู‚ู„ ุนู† ุทุฑูŠู‚ ุนูˆุงู…ู„ ูˆุฑุงุซูŠุฉุŒ ูˆู‡ูŠ ู…ุง ู†ุนุฑูู‡ ุงู„ูŠูˆู… "
"ุจุงุณู… ุงู„ุฌูŠู†ุงุช. ูˆูŠูˆุฌุฏ ู„ูƒู„ ุตูุฉ ุฒูˆุฌ ู…ู† ุงู„ุฌูŠู†ุงุชุŒ ูˆุงุญุฏ ู…ู† ุงู„ุฃุจ ูˆุขุฎุฑ ู…ู† ุงู„ุฃู….",
"ู…ุง ู‡ูŠ ุงู„ุฎู„ูŠุฉุŸ ุงู„ุฎู„ูŠุฉ ู‡ูŠ ูˆุญุฏุฉ ุงู„ุจู†ุงุก ูˆุงู„ูˆุธูŠูุฉ ููŠ ุฌุณู… ุงู„ูƒุงุฆู† ุงู„ุญูŠ. ูˆุชุชูƒูˆู† "
"ุงู„ุฎู„ูŠุฉ ู…ู† ุบุดุงุก ุจู„ุงุฒู…ูŠ ูŠุญูŠุท ุจู‡ุงุŒ ูˆุณูŠุชูˆุจู„ุงุฒู… ูŠุญุชูˆูŠ ุนู„ู‰ ุงู„ุนุถูŠุงุช ุงู„ู…ุฎุชู„ูุฉุŒ "
"ูˆู†ูˆุงุฉ ุชุชุญูƒู… ููŠ ุฌู…ูŠุน ุฃู†ุดุทุฉ ุงู„ุฎู„ูŠุฉ. ูˆูŠุฌุจ ุฃู† ู†ู„ุงุญุธ ุฃู† ุงู„ุฎู„ุงูŠุง ุงู„ู†ุจุงุชูŠุฉ ุชุฎุชู„ู "
"ุนู† ุงู„ุฎู„ุงูŠุง ุงู„ุญูŠูˆุงู†ูŠุฉ ููŠ ูˆุฌูˆุฏ ุฌุฏุงุฑ ุฎู„ูˆูŠ ูˆุงู„ุจู„ุงุณุชูŠุฏุงุช ุงู„ุฎุถุฑุงุก. ูˆู‡ุฐุง ุงู„ุงุฎุชู„ุงู "
"ูŠุคุฏูŠ ุฅู„ู‰ ุงุฎุชู„ุงู ููŠ ุทุฑูŠู‚ุฉ ุงู„ุชุบุฐูŠุฉ ุจูŠู† ุงู„ู†ุจุงุช ูˆุงู„ุญูŠูˆุงู†.",
]
# โ”€โ”€ Per-page scoring โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def test_clean_arabic_is_trusted():
for text in CLEAN:
quality = score_text_quality(text)
assert quality.verdict is TextLayerVerdict.TRUST, (
f"clean prose scored {quality.score} ({quality.verdict}): {quality.reasons}"
)
assert quality.is_trustworthy
def test_scrambled_arabic_is_sent_to_ocr():
"""The regression: this text is >89% Arabic characters and unreadable."""
for text in SCRAMBLED:
quality = score_text_quality(text)
assert quality.verdict in (TextLayerVerdict.OCR, TextLayerVerdict.REJECT), (
f"scrambled text scored {quality.score} ({quality.verdict}) โ€” "
f"it would have been trusted"
)
assert not quality.is_trustworthy
def test_scrambled_text_still_passes_the_old_arabic_ratio_check():
"""Documents *why* the old gate failed: the characters really are Arabic.
If this ever stops holding, the corpus was replaced with something that
the cheap ratio check would have caught anyway, and the regression value
of the other tests goes with it.
"""
for text in SCRAMBLED:
assert score_text_quality(text).arabic_ratio > 0.85
def test_populations_are_clearly_separated():
worst_clean = min(score_text_quality(t).score for t in CLEAN)
best_scrambled = max(score_text_quality(t).score for t in SCRAMBLED)
assert worst_clean > best_scrambled + 0.3, (
f"margin too thin: clean floor {worst_clean}, scrambled ceiling {best_scrambled}"
)
def test_corruption_markers_do_not_fire_on_clean_text():
"""Precision matters more than recall here โ€” these drive a hard penalty."""
for text in CLEAN:
assert score_text_quality(text).corruption_ratio < 0.05
def test_digits_welded_into_words_are_flagged():
quality = score_text_quality(
"ุณุŒ3ุงู„ุฏุงุฑ ุตู‡3ุฏุฑุง ุณู‡3ุณุง3ุง 5ู„ุงู„ุง ุตูˆู„ุงุญู„3 ุณุชู…ุฑุงุฑ3ุงู„ ุณูŠุฌู„ู† ุบุฑูŠ ู„ุชูƒุงุซุฑูˆ "
"ุจู†ูŠ ุชูุฑู‚ ุนูŠูˆุจู‡ ูˆุชุจู†ูŠ ุฆุฏู‡ูˆููˆ ุตุณุงุฆุฎ ุชุนุฏุฏ ู…ู„ุฎุชู„ูู‡"
)
assert quality.corruption_ratio > 0.1
assert not quality.is_trustworthy
def test_empty_and_whitespace_text_is_rejected():
for text in ("", " \n\t "):
assert score_text_quality(text).verdict is TextLayerVerdict.REJECT
def test_sparse_page_is_not_forced_to_ocr():
"""A cover or full-page diagram can't be judged โ€” OCR-ing it wastes GPU."""
quality = score_text_quality("ุงู„ูุตู„ ุงู„ุฃูˆู„")
assert quality.verdict is TextLayerVerdict.VERIFY
assert quality.is_trustworthy
assert "too little text" in " ".join(quality.reasons)
def test_latin_text_is_not_penalised_as_arabic_corruption():
"""English pages route on the `en` config, but must not look corrupt."""
quality = score_text_quality("The cell is the basic unit of life.")
assert quality.verdict is TextLayerVerdict.VERIFY # too few Arabic tokens to judge
# โ”€โ”€ Document-level pre-flight โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def test_document_with_broken_font_is_condemned_wholesale():
pages = {i + 1: SCRAMBLED[i % len(SCRAMBLED)] for i in range(40)}
assessment = assess_pdf_text_layer(pages)
assert assessment.trustworthy is False
assert assessment.mean_score < 0.5
assert "unusable" in assessment.reason
def test_document_with_clean_text_layer_is_trusted():
pages = {i + 1: CLEAN[i % len(CLEAN)] for i in range(40)}
assessment = assess_pdf_text_layer(pages)
assert assessment.trustworthy is True
assert assessment.mean_score > 0.8
def test_preflight_samples_rather_than_reading_everything():
pages = {i + 1: CLEAN[i % len(CLEAN)] for i in range(200)}
assessment = assess_pdf_text_layer(pages, sample_size=5)
assert len(assessment.sampled_pages) <= 5
def test_preflight_skips_front_matter():
"""Front matter is unrepresentative; sampling it invites false verdicts."""
pages = {i + 1: CLEAN[i % len(CLEAN)] for i in range(100)}
assessment = assess_pdf_text_layer(pages)
assert min(assessment.sampled_pages) > 5
def test_a_few_bad_pages_do_not_condemn_a_good_document():
"""Scanned inserts in an otherwise fine book stay on per-page fallback."""
pages = {i + 1: CLEAN[i % len(CLEAN)] for i in range(40)}
pages[20] = SCRAMBLED[0]
pages[24] = SCRAMBLED[1]
assert assess_pdf_text_layer(pages).trustworthy is True
def test_sample_selection_spreads_across_the_body():
sample = select_sample_pages(list(range(1, 253)), sample_size=5)
assert len(sample) == 5
assert min(sample) > 25 # front matter skipped
assert max(sample) <= 252
assert sample == sorted(sample)
assert len(set(sample)) == 5 # no wasted duplicate reads
def test_sample_selection_handles_short_documents():
assert select_sample_pages([1, 2, 3], sample_size=5) == [1, 2, 3]
assert select_sample_pages([]) == []
def test_document_with_no_text_at_all_is_untrustworthy():
assessment = assess_pdf_text_layer({1: "", 2: " ", 3: ""})
assert assessment.trustworthy is False
def test_mostly_empty_document_is_not_condemned_on_sparse_pages():
"""Sparse pages abstain instead of voting the text layer down."""
pages = {i + 1: "ุดูƒู„ ูฃ" for i in range(20)}
assert assess_pdf_text_layer(pages).trustworthy is True