Finish: checkbox detection (U+F0A8) + X filling, comprehensive intake (19 Qs), scan all pages
This commit is contained in:
@@ -85,6 +85,30 @@ def detect_underscores(pno, page):
|
||||
return fields
|
||||
|
||||
|
||||
def detect_checkboxes(pno, page):
|
||||
"""Find checkbox glyphs (U+F0A8) and their labels. Type 'checkbox'."""
|
||||
fields = []
|
||||
words = page.get_text("words")
|
||||
groups = {}
|
||||
for w in words:
|
||||
x0, y0, x1, y1, word = w[0], w[1], w[2], w[3], w[4]
|
||||
key = int(y0 / 5) * 5
|
||||
groups.setdefault(key, []).append((x0, x1, y0, word))
|
||||
for y in sorted(groups):
|
||||
ws = sorted(groups[y], key=lambda t: t[0])
|
||||
for i, (x0, x1, y0, word) in enumerate(ws):
|
||||
if "\uf0a8" in word:
|
||||
label_words = []
|
||||
for wx0, wx1, _, w in ws[i + 1:]:
|
||||
if "\uf0a8" in w:
|
||||
break
|
||||
label_words.append(w)
|
||||
label = " ".join(label_words).strip()
|
||||
fields.append({"page": pno, "x": x0, "y": y0, "type": "checkbox",
|
||||
"label": label})
|
||||
return fields
|
||||
|
||||
|
||||
def main():
|
||||
all_fields = {}
|
||||
for fn in sorted(os.listdir(FORMS_DIR)):
|
||||
@@ -99,9 +123,9 @@ def main():
|
||||
continue
|
||||
fields = []
|
||||
seen_keys = set()
|
||||
for pno in range(min(len(d), 3)): # caption + first 2 pages
|
||||
lines = lines_with_pos(d[pno])
|
||||
for pno in range(min(len(d), 12)): # caption + all pages
|
||||
if pno == 0:
|
||||
lines = lines_with_pos(d[pno])
|
||||
for f in detect_caption(lines):
|
||||
f["page"] = 0
|
||||
if f["key"] not in seen_keys:
|
||||
@@ -110,6 +134,9 @@ def main():
|
||||
for f in detect_underscores(pno, d[pno]):
|
||||
f["key"] = "f_" + str(len(fields))
|
||||
fields.append(f)
|
||||
for f in detect_checkboxes(pno, d[pno]):
|
||||
f["key"] = "cb_" + str(len(fields))
|
||||
fields.append(f)
|
||||
d.close()
|
||||
all_fields[fid] = fields
|
||||
out = "/opt/astraea/forms/fieldmap.json"
|
||||
|
||||
Reference in New Issue
Block a user