grob-products-updated / scripts /dump_pages.py
bep40's picture
Upload scripts/dump_pages.py
e8b45d7 verified
Raw History Blame Contribute Delete
2.11 kB
import requests, re, json, os
H = "https://huggingface.co"
from pymupdf import Document as MuDoc
def fold(s):
MP = {'\u0103':'a','\u00e2':'a','\u0111':'d','\u00ea':'e','\u00f4':'o','\u01a1':'o','\u01b0':'u','\u00e9':'e','\u00e8':'e','\u00f9':'u','\u00fa':'u','\u00ed':'i','\u00f3':'o','\u00f2':'o','\u00f5':'o','\u0169':'u','\u0129':'i','\u00fd':'y','\u1eaf':'a','\u1eb1':'a','\u1eb3':'a','\u1eb5':'a','\u1eb7':'a','\u1ea5':'a','\u1ea7':'a','\u1ea9':'a','\u1eab':'a','\u1ead':'a','\u1ebf':'e','\u1ec1':'e','\u1ec3':'e','\u1ec5':'e','\u1ebb':'e','\u1ebd':'e','\u1eb9':'e','\u1ed1':'o','\u1ed3':'o','\u1ed5':'o','\u1ed7':'o','\u1ed9':'o','\u1edb':'o','\u1edd':'o','\u1edf':'o','\u1ee1':'o','\u1ee3':'o','\u1ecf':'o','\u1ee5':'o','\u1ee9':'u','\u1eeb':'u','\u1eed':'u','\u1eef':'u','\u1ef1':'u','\u1ee7':'u','\u0169':'u'}
return ''.join(MP.get(c,c) for c in str(s or '').lower())
pdf = requests.get(H + "/datasets/bep40/grob-products-updated/resolve/main/pdfs/imudex_2026.pdf", timeout=600).content
open("/tmp/imudex.pdf","wb").write(pdf)
doc = MuDoc("/tmp/imudex.pdf")
code_pat = re.compile(r'^[\d\s.]{6,}$')
def dump(pgn):
print("="*80)
print("PDF PAGE", pgn+1)
pg = doc[pgn]
d = pg.get_text("dict")
lines=[]
for b in d["blocks"]:
if b["type"]!=0: continue
for l in b["lines"]:
txt="".join(s["text"] for s in l["spans"]).strip()
if txt:
x0,y0,x1,y1=l["bbox"]
lines.append((round(x0,1),round(y0,1),round(x1,1),round(y1,1),txt))
# images
print(" IMAGES (>160px):")
for ip in pg.get_image_info():
bb=ip["bbox"]; w=ip.get("width") or 0; h=ip.get("height") or 0
if w>=160 and h>=110:
print(f" bbox=[{round(bb[0])},{round(bb[1])},{round(bb[2])},{round(bb[3])}] {w}x{h}")
print(" TEXT LINES sorted by y: (showing all)")
for ln in sorted(lines, key=lambda l:(l[1],l[0])):
tag = "CODE" if code_pat.match(ln[4].strip()) else (" " )
print(f" y={ln[1]:6.1f} x={ln[0]:6.1f}-{ln[2]:6.1f} {tag} {ln[4][:60]!r}")
for p in [78,80]:
dump(p)