-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeparsetree.py
304 lines (294 loc) · 10.1 KB
/
makeparsetree.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def set_fraction(compounds):
compound = []
i = 0
while i < len(compounds):
now = compounds[i]
if now['did'] == False and now['symbol'] == '-':
nowX = (now['xmin'] + now['xmax']) / 2
nowY = (now['ymin'] + now['ymax']) / 2
j = i
while True:
if j == 0: break
prev = compounds[j - 1]
if prev['did']: break
prevX = (prev['xmin'] + prev['xmax']) / 2
prevY = (prev['ymin'] + prev['ymax']) / 2
if now['xmin'] <= prevX and now['xmax'] >= prevX:
j -= 1
else:
break
up = []
up_tmp = []
down = []
down_tmp = []
while j < len(compounds):
if j != i:
check = compounds[j]
checkX = (check['xmin'] + check['xmax']) / 2
checkY = (check['ymin'] + check['ymax']) / 2
if now['xmin'] <= checkX <= now['xmax']:
if now['ymax'] < checkY and now['ymax'] < checkY:
down.append(check)
down_tmp.append(check.copy())
elif now['ymax'] > checkY and now['ymin'] > checkY:
up.append(check)
up_tmp.append(check.copy())
else:
break
j += 1
if len(up) != 0 and len(down) != 0:
for d in down:
d['did'] = True
for u in up:
u['did'] = True
now['did'] = True
up = set_fraction(up_tmp)
down = set_fraction(down_tmp)
compound.append({
'class': 'fraction',
'symbol': '-',
'did': False,
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': up,
'down': down,
'inner': [],
})
i += 1
for i in range(0, len(compounds)):
now = compounds[i]
if not now['did']:
compound.append({
'class': 'normal',
'symbol': now['symbol'],
'did': False,
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': [],
'down': [],
'inner': [],
})
compound = sorted(compound, key=lambda k: (k['xmin'], k['ymin']))
return compound
def set_square_root(bonding_boxes):
compound = []
i = 0
while i < len(bonding_boxes):
now = bonding_boxes[i]
if now['did']:
i += 1
continue
elif now['class'] == 'fraction':
compound.append({
'class': 'fraction',
'symbol': '-',
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'did': False,
'up': set_square_root(now['up']),
'down': set_square_root(now['down']),
'inner': [],
})
i += 1
continue
# 마지막 문자 && compound에 속해 있지 않는 경우 compound에 넣고 종료
if i == len(bonding_boxes) - 1:
now['did'] = True
compound.append({
'class': 'normal',
'did': False,
'symbol': now['symbol'],
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': [],
'down': [],
'inner': [],
})
i += 1
continue
# square root를 재귀적으로 짜야한다.
inner = []
while i < len(bonding_boxes) - 1:
later = bonding_boxes[i + 1]
if later['did']:
continue
laterX = (later['xmin'] + later['xmax']) / 2
laterY = (later['ymin'] + later['ymax']) / 2
if now['xmin'] <= laterX <= now['xmax'] and now['ymin'] <= laterY <= now['ymax']:
inner.append(later.copy())
later['did'] = True
else:
break
i += 1
# if now['xmin'] < later['xmin'] and now['ymin'] < later['ymin'] and \
# now['xmax'] > later['xmax'] and now['ymax'] > later['ymax']:
# inner.append(later.copy())
# later['did'] = True
# else:
# break
# i += 1
if len(inner) == 0:
now['did'] = True
compound.append({
'class': 'normal',
'symbol': now['symbol'],
'did': False,
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': [],
'down': [],
'inner': []
})
else:
compound.append({
'class': 'square_root',
'symbol': '√',
'did': False,
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': [],
'down': [],
'inner': set_square_root(inner),
})
i += 1
return compound
def set_script(compounds):
compound = []
i = 0
while i < len(compounds):
now = compounds[i]
if now['did']:
i += 1
continue
elif now['class'] == 'fraction':
compound.append({
'class': 'fraction',
'symbol': '-',
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'did': False,
'up': set_script(now['up']),
'down': set_script(now['down']),
'inner': [],
})
i += 1
continue
elif now['class'] == 'square_root':
compound.append({
'class': 'square_root',
'symbol': '√',
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'did': False,
'up': [],
'down': [],
'inner': set_script(now['inner']),
})
i += 1
continue
# 마지막 문자 && compound에 속해 있지 않는 경우 compound에 넣고 종료
# if i == len(compounds) - 1:
# now['did'] = True
# compound.append({
# 'class': 'normal',
# 'did': False,
# 'symbol': now['symbol'],
# 'xmin': now['xmin'],
# 'xmax': now['xmax'],
# 'ymin': now['ymin'],
# 'ymax': now['ymax'],
# 'up': [],
# 'down': [],
# 'inner': [],
# })
# i += 1
# continue
# if fraction symbol is detected, divide up, down while changing 'did' flags
up = []
while i < len(compounds) - 1:
later = compounds[i + 1]
if later['did']:
continue
k = 0.5
if now['ymin'] >= later['ymin'] and now['ymin'] + k * (now['ymax'] - now['ymin']) >= later['ymax']:
up.append(later.copy())
later['did'] = True
else:
break
i += 1
if len(up) == 0:
now['did'] = True
compound.append({
'class': 'normal',
'symbol': now['symbol'],
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': [],
'down': [],
'inner': [],
'did': False,
})
else:
now['did'] = True
up = set_script(up)
compound.append({
'class': 'script',
'symbol': now['symbol'],
'did': False,
'xmin': now['xmin'],
'xmax': now['xmax'],
'ymin': now['ymin'],
'ymax': now['ymax'],
'up': up,
'down': [],
'inner': [],
})
i += 1
return compound
def print_compounds(compounds):
ret = ""
if len(compounds) != 0:
for i in range(len(compounds)):
compound = compounds[i]
up = compound['up']
down = compound['down']
inner = compound['inner']
classification = compound['class']
symbol = compound['symbol']
if classification == 'fraction':
ret += '!frac{'
ret += print_compounds(up)
ret += "}{"
ret += print_compounds(down)
ret += "}"
elif classification == 'square_root':
ret += '!sqrt{'
ret += print_compounds(inner)
ret += "}"
elif classification == 'script':
ret += symbol
if len(up) != 0:
ret += '^{'
ret += print_compounds(up)
ret += '}'
else:
ret += symbol
return ret