-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom_gpt_o1.py
68 lines (57 loc) · 2.28 KB
/
from_gpt_o1.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
import re
corner_map = {
'┌': 'open', '┬': 'open', '┐': 'open',
'├': 'split', '┼': 'split', '┤': 'split',
'└': 'close', '┴': 'close', '┘': 'close',
}
def parse_box_drawing_chunk(text):
"""
Parse a chunk of text containing multiple box-drawing 'forms'.
Return a list of top-level forms (each is a nested Python list).
"""
lines = text.splitlines()
# stack[0] = the root: a list of all top-level forms
# deeper indices hold nested sub-lists for nested corners
stack = [[]] # root = a list containing 0..N top-level forms
def current_list():
return stack[-1]
for line in lines:
line = line.rstrip()
# Separate box art from payload
match = re.match(r'^([\u2500-\u257F\s]*)(.*)$', line)
if match:
box_part, token_part = match.groups()
token_part = token_part.strip()
else:
box_part, token_part = "", line.strip()
# Extract all corners in left-to-right order
# e.g. "└─┴─┴─┴─┴─" might yield ['└', '┴', '┴', '┴', '┴']
corners = re.findall(r'[┌┬┐├┼┤└┴┘]', box_part)
# Process each corner
for corner in corners:
ctype = corner_map.get(corner)
if ctype == 'open':
# Open a new nested list
new_list = []
current_list().append(new_list)
stack.append(new_list)
elif ctype == 'split':
# Close the current list, then open a sibling at same level
if len(stack) > 1:
stack.pop()
sibling = []
current_list().append(sibling)
stack.append(sibling)
elif ctype == 'close':
# Close the current list (pop once)
if len(stack) > 1:
stack.pop()
# else: unknown corner, ignore or handle error
# Add tokens (if any) to the *current* list
if token_part:
current_list().extend(token_part.split())
# At the end, if something didn’t close, pop back to root
while len(stack) > 1:
stack.pop()
# stack[0] is now a list of top-level forms
return stack[0]