-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure_engine.py
More file actions
55 lines (34 loc) · 898 Bytes
/
Copy pathstructure_engine.py
File metadata and controls
55 lines (34 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def group_text_blocks(blocks, y_threshold=25):
blocks = sorted(
blocks,
key=lambda x: (
x["coords"][0][1],
x["coords"][0][0]
)
)
grouped = []
current_group = []
last_y = None
for block in blocks:
y = block["coords"][0][1]
if last_y is None:
current_group.append(block)
elif abs(y - last_y) < y_threshold:
current_group.append(block)
else:
grouped.append(current_group)
current_group = [block]
last_y = y
if current_group:
grouped.append(current_group)
paragraphs = []
for group in grouped:
text = " ".join([
item["text"]
for item in group
])
paragraphs.append({
"text": text,
"blocks": group
})
return paragraphs