Spaces:
Build error
Build error
Download tests/test_text_quality.py from anwarkxc/trackademi: direct link, hf CLI and curl.
- Browser
- Download file 9.51 kB
-
https://huggingface.co/spaces/anwarkxc/trackademi/resolve/main/tests/test_text_quality.py
- Command line
-
hf download hf://spaces/anwarkxc/trackademi/tests/test_text_quality.py
-
curl -L -o test_text_quality.py https://huggingface.co/spaces/anwarkxc/trackademi/resolve/main/tests/test_text_quality.py
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 | |