Datasets:
Formats:
parquet
Size:
100K - 1M
Tags:
table-recognition
table-structure-recognition
document-image-analysis
ocr
document-understanding
cv
License:
Upload scripts/fill_html.py with huggingface_hub
Browse files- scripts/fill_html.py +104 -0
scripts/fill_html.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Fill missing `html` by converting each row's `otsl` (the reverse direction).
|
| 3 |
+
|
| 4 |
+
OTSL and HTML are interconvertible. Sources that ship OTSL but no HTML
|
| 5 |
+
(TabRecSet) get HTML generated here with a worker pool.
|
| 6 |
+
|
| 7 |
+
Usage: python3 fill_html.py <in.parquet> <out.parquet> [--workers N]
|
| 8 |
+
"""
|
| 9 |
+
import multiprocessing as mp
|
| 10 |
+
import re
|
| 11 |
+
import sys
|
| 12 |
+
|
| 13 |
+
import pyarrow as pa
|
| 14 |
+
import pyarrow.parquet as pq
|
| 15 |
+
|
| 16 |
+
# OTSL -> HTML cell kind. PaddleOCR's tokens:
|
| 17 |
+
# <fcel> new cell
|
| 18 |
+
# <ecel> empty/blank cell
|
| 19 |
+
# <lcel> cell merged with the one to its left (colspan)
|
| 20 |
+
# <ulcel> cell merged with the one above (rowspan)
|
| 21 |
+
# <xcel> merged both ways
|
| 22 |
+
# <nl> end of row
|
| 23 |
+
STRUCT = re.compile(r"<(fcel|ecel|lcel|ulcel|xcel|ucel|nl)>")
|
| 24 |
+
TAG_RE = re.compile(r"<(/?)([a-zA-Z]+)[^>]*>")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _cell_html(tag, text):
|
| 28 |
+
if tag in ("lcel",):
|
| 29 |
+
return "" # continuation of the previous cell
|
| 30 |
+
if tag == "ecel":
|
| 31 |
+
return "<td></td>"
|
| 32 |
+
if tag in ("ulcel", "ucel", "xcel"):
|
| 33 |
+
return "<td></td>" # vertical continuation, rendered as a blank cell
|
| 34 |
+
return f"<td>{text}</td>"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def otsl_to_html(otsl):
|
| 38 |
+
"""Rebuild a minimal <table> from an OTSL token string."""
|
| 39 |
+
if not otsl:
|
| 40 |
+
return ""
|
| 41 |
+
# split into rows, then into (token, text) pairs
|
| 42 |
+
rows_html = []
|
| 43 |
+
for row in STRUCT.split(otsl):
|
| 44 |
+
if not row:
|
| 45 |
+
continue
|
| 46 |
+
parts = row.split(">")
|
| 47 |
+
cells, buf = [], ""
|
| 48 |
+
for p in parts:
|
| 49 |
+
m = re.fullmatch(r"\s*(fcel|ecel|lcel|ulcel|xcel|ucel)\s*", p)
|
| 50 |
+
if m:
|
| 51 |
+
cells.append((m.group(1), buf))
|
| 52 |
+
buf = ""
|
| 53 |
+
else:
|
| 54 |
+
buf += p
|
| 55 |
+
out = "".join(_cell_html(t, tx) for t, tx in cells)
|
| 56 |
+
if out:
|
| 57 |
+
rows_html.append(f"<tr>{out}</tr>")
|
| 58 |
+
if not rows_html:
|
| 59 |
+
return ""
|
| 60 |
+
return "<table>" + "".join(rows_html) + "</table>"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def work(chunk):
|
| 64 |
+
return [otsl_to_html(o) for o in chunk]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def main():
|
| 68 |
+
src, dst = sys.argv[1], sys.argv[2]
|
| 69 |
+
workers = int(sys.argv[sys.argv.index("--workers") + 1]) if "--workers" in sys.argv else 128
|
| 70 |
+
|
| 71 |
+
pf = pq.ParquetFile(src)
|
| 72 |
+
schema = pf.schema_arrow
|
| 73 |
+
out = pq.ParquetWriter(dst, schema, compression="zstd")
|
| 74 |
+
total = filled = 0
|
| 75 |
+
|
| 76 |
+
with mp.Pool(workers) as pool:
|
| 77 |
+
for rg in range(pf.num_row_groups):
|
| 78 |
+
tbl = pf.read_row_group(rg)
|
| 79 |
+
htmls = tbl.column("html").to_pylist()
|
| 80 |
+
otsls = tbl.column("otsl").to_pylist()
|
| 81 |
+
todo = [i for i, h in enumerate(htmls) if not h and otsls[i]]
|
| 82 |
+
if todo:
|
| 83 |
+
chunks = [otsls[i] for i in todo]
|
| 84 |
+
n = (len(chunks) + workers * 4 - 1) // (workers * 4)
|
| 85 |
+
pieces = [chunks[k:k + n] for k in range(0, len(chunks), n)]
|
| 86 |
+
done = 0
|
| 87 |
+
for piece, res in zip(pieces, pool.map(work, pieces)):
|
| 88 |
+
for i, s in zip(todo[done:done + len(piece)], res):
|
| 89 |
+
htmls[i] = s
|
| 90 |
+
done += len(piece)
|
| 91 |
+
filled += done
|
| 92 |
+
i = schema.get_field_index("html")
|
| 93 |
+
tbl = tbl.set_column(i, schema.field("html"),
|
| 94 |
+
pa.array(htmls, type=schema.field("html").type))
|
| 95 |
+
out.write_table(tbl)
|
| 96 |
+
total += tbl.num_rows
|
| 97 |
+
print(f" {total} rows, filled {filled}", flush=True)
|
| 98 |
+
|
| 99 |
+
out.close()
|
| 100 |
+
print(f"done: {total} rows, {filled} html generated -> {dst}")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
main()
|