Add a local searchable-PDF orientation reader, a stale OCR example, and verified results on 343 USGS and 553 Bengali archive pages
88248d0 verified Download text-layer/study/check_cli.py from foliokestrel/folio-orient: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/foliokestrel/folio-orient/resolve/main/text-layer/study/check_cli.py
- Command line
-
hf download hf://foliokestrel/folio-orient/text-layer/study/check_cli.py
-
curl -L -o check_cli.py https://huggingface.co/foliokestrel/folio-orient/resolve/main/text-layer/study/check_cli.py
3.43 kB
| """Exercise the reader commands and compare actual CSV rows to frozen decisions.""" | |
| import argparse | |
| import csv | |
| from hashlib import sha256 | |
| from html.parser import HTMLParser | |
| from io import StringIO | |
| import json | |
| from pathlib import Path | |
| import shutil | |
| import subprocess | |
| import sys | |
| from tempfile import TemporaryDirectory | |
| from urllib.parse import unquote | |
| STUDY = Path(__file__).resolve().parent | |
| ROOT = STUDY.parent | |
| class Report(HTMLParser): | |
| def __init__(self): | |
| super().__init__() | |
| self.hrefs = [] | |
| self.thumbnails = [] | |
| def handle_starttag(self, tag, attrs): | |
| attrs = dict(attrs) | |
| if tag == "a": | |
| self.hrefs.append(unquote(attrs["href"])) | |
| if tag == "img": | |
| assert attrs["src"].startswith("data:image/jpeg;base64,") | |
| self.thumbnails.append(attrs["alt"]) | |
| def run(*args, ok=True): | |
| result = subprocess.run([sys.executable, str(ROOT/"review_text.py"), *map(str,args)], | |
| capture_output=True, text=True) | |
| assert (result.returncode==0) == ok, (result.returncode,result.stderr) | |
| return result.stdout | |
| def main(pdfs): | |
| with TemporaryDirectory() as tmp: | |
| folder = Path(tmp) | |
| pdf = folder/"Field & notes #1.pdf" | |
| shutil.copyfile(ROOT/"examples/field-notes.pdf",pdf) | |
| before = pdf.read_bytes() | |
| out = folder/"review.html" | |
| lines = run(pdf,"--html",out,"--previews","2") | |
| assert lines == (ROOT/"examples/review.csv").read_text() | |
| doc = Report() | |
| doc.feed(out.read_text()) | |
| assert sorted(doc.hrefs) == [pdf.name+f"#page={p}" for p in range(1,6)] | |
| assert len(doc.thumbnails) == 2 and "Page 4" in doc.thumbnails[1] | |
| report_hash = sha256(out.read_bytes()).digest() | |
| run(pdf,"--html",out,ok=False) | |
| run(pdf,"--html",pdf,ok=False) | |
| run(pdf,"--previews","-1",ok=False) | |
| assert pdf.read_bytes()==before and sha256(out.read_bytes()).digest()==report_hash | |
| damaged = folder/"broken.pdf" | |
| damaged.write_bytes(b"not a PDF") | |
| run(damaged,"--html",folder/"bad.html",ok=False) | |
| assert not (folder/"bad.html").exists() | |
| if pdfs is not None: | |
| rows = [json.loads(line) for line in (STUDY/"pages.jsonl").read_text().splitlines()] | |
| for series in sorted({r["series"] for r in rows}): | |
| src = pdfs/f"wsp{series}.pdf" | |
| actual = list(csv.DictReader(StringIO(run(src)))) | |
| expected = [r for r in rows if r["series"]==series] | |
| assert len(actual)==len(expected) | |
| for r,ref in zip(actual, expected): | |
| text = ref["text"] | |
| turn = str(text["predicted"]) if text["accepted"] else "" | |
| assert int(r["page"]) == ref["page"] and r["clockwise"] == turn | |
| assert r["action"] == ("inspect" if turn=="" else ("keep" if turn=="0" else "turn")) | |
| assert [int(r[f"votes_{n}"]) for n in (0,90,180,270)] == text["votes"] | |
| assert r["agreement"] == (str(text["score"]) if text["score"] is not None else "") | |
| print(f"Matched the reader CSV against {len(rows)} studied pages.") | |
| print("Verified the five-page example, links, preview limit, input preservation and failure handling.") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("pdfs", nargs="?", type=Path) | |
| main(parser.parse_args().pdfs) | |