"tests/pulsar/test_small_spheres.py" did not exist on "d565032399bde29fcbbad99a75987dbae923bca8"
equations_replace.py 20 KB
Newer Older
赵小蒙's avatar
赵小蒙 committed
1
2
3
"""
对pymupdf返回的结构里的公式进行替换,替换为模型识别的公式结果
"""
许瑞's avatar
许瑞 committed
4

赵小蒙's avatar
赵小蒙 committed
5
from magic_pdf.libs.commons import fitz
赵小蒙's avatar
赵小蒙 committed
6
7
8
9
import json
import os
from pathlib import Path
from loguru import logger
10
from magic_pdf.libs.ocr_content_type import ContentType
赵小蒙's avatar
赵小蒙 committed
11

12
13
TYPE_INLINE_EQUATION = ContentType.InlineEquation
TYPE_INTERLINE_EQUATION = ContentType.InterlineEquation
赵小蒙's avatar
赵小蒙 committed
14
15
16
17
18
19
20


def combine_chars_to_pymudict(block_dict, char_dict):
    """
    把block级别的pymupdf 结构里加入char结构
    """
    # 因为block_dict 被裁剪过,因此先把他和char_dict文字块对齐,才能进行补充
许瑞's avatar
许瑞 committed
21
22
23
    char_map = {tuple(item["bbox"]): item for item in char_dict}

    for i in range(len(block_dict)):  # blcok
赵小蒙's avatar
赵小蒙 committed
24
        block = block_dict[i]
许瑞's avatar
许瑞 committed
25
        key = block["bbox"]
赵小蒙's avatar
赵小蒙 committed
26
        char_dict_item = char_map[tuple(key)]
许瑞's avatar
许瑞 committed
27
28
29
30
31
32
        char_dict_map = {tuple(item["bbox"]): item for item in char_dict_item["lines"]}
        for j in range(len(block["lines"])):
            lines = block["lines"][j]
            with_char_lines = char_dict_map[lines["bbox"]]
            for k in range(len(lines["spans"])):
                spans = lines["spans"][k]
赵小蒙's avatar
赵小蒙 committed
33
                try:
许瑞's avatar
许瑞 committed
34
                    chars = with_char_lines["spans"][k]["chars"]
赵小蒙's avatar
赵小蒙 committed
35
                except Exception as e:
许瑞's avatar
许瑞 committed
36
37
38
39
                    logger.error(char_dict[i]["lines"][j])

                spans["chars"] = chars

赵小蒙's avatar
赵小蒙 committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    return block_dict


def calculate_overlap_area_2_minbox_area_ratio(bbox1, min_bbox):
    """
    计算box1和box2的重叠面积占最小面积的box的比例
    """
    # Determine the coordinates of the intersection rectangle
    x_left = max(bbox1[0], min_bbox[0])
    y_top = max(bbox1[1], min_bbox[1])
    x_right = min(bbox1[2], min_bbox[2])
    y_bottom = min(bbox1[3], min_bbox[3])

    if x_right < x_left or y_bottom < y_top:
        return 0.0

    # The area of overlap area
    intersection_area = (x_right - x_left) * (y_bottom - y_top)
许瑞's avatar
许瑞 committed
58
59
    min_box_area = (min_bbox[3] - min_bbox[1]) * (min_bbox[2] - min_bbox[0])
    if min_box_area == 0:
赵小蒙's avatar
赵小蒙 committed
60
61
62
        return 0
    else:
        return intersection_area / min_box_area
许瑞's avatar
许瑞 committed
63

赵小蒙's avatar
赵小蒙 committed
64
65

def _is_xin(bbox1, bbox2):
许瑞's avatar
许瑞 committed
66
67
68
    area1 = abs(bbox1[2] - bbox1[0]) * abs(bbox1[3] - bbox1[1])
    area2 = abs(bbox2[2] - bbox2[0]) * abs(bbox2[3] - bbox2[1])
    if area1 < area2:
赵小蒙's avatar
赵小蒙 committed
69
70
71
72
        ratio = calculate_overlap_area_2_minbox_area_ratio(bbox2, bbox1)
    else:
        ratio = calculate_overlap_area_2_minbox_area_ratio(bbox1, bbox2)

许瑞's avatar
许瑞 committed
73
    return ratio > 0.6
赵小蒙's avatar
赵小蒙 committed
74
75
76
77
78
79
80


def remove_text_block_in_interline_equation_bbox(interline_bboxes, text_blocks):
    """消除掉整个块都在行间公式块内部的文本块"""
    for eq_bbox in interline_bboxes:
        removed_txt_blk = []
        for text_blk in text_blocks:
许瑞's avatar
许瑞 committed
81
82
83
84
85
            text_bbox = text_blk["bbox"]
            if (
                calculate_overlap_area_2_minbox_area_ratio(eq_bbox["bbox"], text_bbox)
                >= 0.7
            ):
赵小蒙's avatar
赵小蒙 committed
86
87
88
89
90
91
92
93
94
95
96
97
98
                removed_txt_blk.append(text_blk)
        for blk in removed_txt_blk:
            text_blocks.remove(blk)

    return text_blocks


def _is_in_or_part_overlap(box1, box2) -> bool:
    """
    两个bbox是否有部分重叠或者包含
    """
    if box1 is None or box2 is None:
        return False
许瑞's avatar
许瑞 committed
99

赵小蒙's avatar
赵小蒙 committed
100
101
102
    x0_1, y0_1, x1_1, y1_1 = box1
    x0_2, y0_2, x1_2, y1_2 = box2

许瑞's avatar
许瑞 committed
103
104
105
106
107
108
    return not (
        x1_1 < x0_2  # box1在box2的左边
        or x0_1 > x1_2  # box1在box2的右边
        or y1_1 < y0_2  # box1在box2的上边
        or y0_1 > y1_2
    )  # box1在box2的下边
赵小蒙's avatar
赵小蒙 committed
109

许瑞's avatar
许瑞 committed
110
111
112
def remove_text_block_overlap_interline_equation_bbox(
    interline_eq_bboxes, pymu_block_list
):
113

赵小蒙's avatar
赵小蒙 committed
114
115
116
117
118
    """消除掉行行内公式有部分重叠的文本块的内容。
    同时重新计算消除重叠之后文本块的大小"""
    deleted_block = []
    for text_block in pymu_block_list:
        deleted_line = []
许瑞's avatar
许瑞 committed
119
        for line in text_block["lines"]:
赵小蒙's avatar
赵小蒙 committed
120
            deleted_span = []
许瑞's avatar
许瑞 committed
121
            for span in line["spans"]:
赵小蒙's avatar
赵小蒙 committed
122
                deleted_chars = []
许瑞's avatar
许瑞 committed
123
124
125
126
127
128
129
                for char in span["chars"]:
                    if any(
                        [
                            _is_in_or_part_overlap(char["bbox"], eq_bbox["bbox"])
                            for eq_bbox in interline_eq_bboxes
                        ]
                    ):
赵小蒙's avatar
赵小蒙 committed
130
131
132
                        deleted_chars.append(char)
                # 检查span里没有char则删除这个span
                for char in deleted_chars:
许瑞's avatar
许瑞 committed
133
                    span["chars"].remove(char)
赵小蒙's avatar
赵小蒙 committed
134
                # 重新计算这个span的大小
许瑞's avatar
许瑞 committed
135
                if len(span["chars"]) == 0:  # 删除这个span
赵小蒙's avatar
赵小蒙 committed
136
137
                    deleted_span.append(span)
                else:
许瑞's avatar
许瑞 committed
138
139
140
141
142
143
144
                    span["bbox"] = (
                        min([b["bbox"][0] for b in span["chars"]]),
                        min([b["bbox"][1] for b in span["chars"]]),
                        max([b["bbox"][2] for b in span["chars"]]),
                        max([b["bbox"][3] for b in span["chars"]]),
                    )

赵小蒙's avatar
赵小蒙 committed
145
146
            # 检查这个span
            for span in deleted_span:
许瑞's avatar
许瑞 committed
147
148
                line["spans"].remove(span)
            if len(line["spans"]) == 0:  # 删除这个line
赵小蒙's avatar
赵小蒙 committed
149
150
                deleted_line.append(line)
            else:
许瑞's avatar
许瑞 committed
151
152
153
154
155
156
                line["bbox"] = (
                    min([b["bbox"][0] for b in line["spans"]]),
                    min([b["bbox"][1] for b in line["spans"]]),
                    max([b["bbox"][2] for b in line["spans"]]),
                    max([b["bbox"][3] for b in line["spans"]]),
                )
赵小蒙's avatar
赵小蒙 committed
157
158
159

        # 检查这个block是否可以删除
        for line in deleted_line:
许瑞's avatar
许瑞 committed
160
161
            text_block["lines"].remove(line)
        if len(text_block["lines"]) == 0:  # 删除block
赵小蒙's avatar
赵小蒙 committed
162
163
            deleted_block.append(text_block)
        else:
许瑞's avatar
许瑞 committed
164
165
166
167
168
169
            text_block["bbox"] = (
                min([b["bbox"][0] for b in text_block["lines"]]),
                min([b["bbox"][1] for b in text_block["lines"]]),
                max([b["bbox"][2] for b in text_block["lines"]]),
                max([b["bbox"][3] for b in text_block["lines"]]),
            )
赵小蒙's avatar
赵小蒙 committed
170
171
172
173

    # 检查text block删除
    for block in deleted_block:
        pymu_block_list.remove(block)
许瑞's avatar
许瑞 committed
174
    if len(pymu_block_list) == 0:
赵小蒙's avatar
赵小蒙 committed
175
176
177
178
179
180
181
182
        return []

    return pymu_block_list


def insert_interline_equations_textblock(interline_eq_bboxes, pymu_block_list):
    """在行间公式对应的地方插上一个伪造的block"""
    for eq in interline_eq_bboxes:
许瑞's avatar
许瑞 committed
183
184
        bbox = eq["bbox"]
        latex_content = eq["latex"]
赵小蒙's avatar
赵小蒙 committed
185
        text_block = {
许瑞's avatar
许瑞 committed
186
187
188
189
190
191
192
193
            "number": len(pymu_block_list),
            "type": 0,
            "bbox": bbox,
            "lines": [
                {
                    "spans": [
                        {
                            "size": 9.962599754333496,
194
                            "_type": TYPE_INTERLINE_EQUATION,
许瑞's avatar
许瑞 committed
195
196
197
198
199
                            "flags": 4,
                            "font": TYPE_INTERLINE_EQUATION,
                            "color": 0,
                            "ascender": 0.9409999847412109,
                            "descender": -0.3050000071525574,
200
                            "text": f"\n$$\n{latex_content}\n$$\n",
许瑞's avatar
许瑞 committed
201
202
203
204
205
206
207
208
209
210
                            "origin": [bbox[0], bbox[1]],
                            "bbox": bbox,
                        }
                    ],
                    "wmode": 0,
                    "dir": [1.0, 0.0],
                    "bbox": bbox,
                }
            ],
        }
赵小蒙's avatar
赵小蒙 committed
211
        pymu_block_list.append(text_block)
许瑞's avatar
许瑞 committed
212
213


赵小蒙's avatar
赵小蒙 committed
214
215
216
217
218
219
def x_overlap_ratio(box1, box2):
    a, _, c, _ = box1
    e, _, g, _ = box2

    # 计算重叠宽度
    overlap_x = max(min(c, g) - max(a, e), 0)
许瑞's avatar
许瑞 committed
220

赵小蒙's avatar
赵小蒙 committed
221
222
223
224
225
226
227
228
    # 计算box1的宽度
    width1 = g - e

    # 计算重叠比例
    overlap_ratio = overlap_x / width1 if width1 != 0 else 0

    return overlap_ratio

许瑞's avatar
许瑞 committed
229

赵小蒙's avatar
赵小蒙 committed
230
def __is_x_dir_overlap(bbox1, bbox2):
许瑞's avatar
许瑞 committed
231
232
    return not (bbox1[2] < bbox2[0] or bbox1[0] > bbox2[2])

赵小蒙's avatar
赵小蒙 committed
233
234
235
236
237
238
239
240

def __y_overlap_ratio(box1, box2):
    """"""
    _, b, _, d = box1
    _, f, _, h = box2

    # 计算重叠高度
    overlap_y = max(min(d, h) - max(b, f), 0)
许瑞's avatar
许瑞 committed
241

赵小蒙's avatar
赵小蒙 committed
242
243
244
245
246
247
248
    # 计算box1的高度
    height1 = d - b

    # 计算重叠比例
    overlap_ratio = overlap_y / height1 if height1 != 0 else 0

    return overlap_ratio
许瑞's avatar
许瑞 committed
249
250


赵小蒙's avatar
赵小蒙 committed
251
def replace_line_v2(eqinfo, line):
许瑞's avatar
许瑞 committed
252
    """
赵小蒙's avatar
赵小蒙 committed
253
254
255
256
257
258
259
    扫描这一行所有的和公式框X方向重叠的char,然后计算char的左、右x0, x1,位于这个区间内的span删除掉。
    最后与这个x0,x1有相交的span0, span1内部进行分割。
    """
    first_overlap_span = -1
    first_overlap_span_idx = -1
    last_overlap_span = -1
    delete_chars = []
许瑞's avatar
许瑞 committed
260
261
262
263
264
265
    for i in range(0, len(line["spans"])):
        if line["spans"][i].get("_type", None) is not None:
            continue  # 忽略,因为已经是插入的伪造span公式了

        for char in line["spans"][i]["chars"]:
            if __is_x_dir_overlap(eqinfo["bbox"], char["bbox"]):
赵小蒙's avatar
赵小蒙 committed
266
                line_txt = ""
许瑞's avatar
许瑞 committed
267
                for span in line["spans"]:
赵小蒙's avatar
赵小蒙 committed
268
                    span_txt = "<span>"
许瑞's avatar
许瑞 committed
269
270
                    for ch in span["chars"]:
                        span_txt = span_txt + ch["c"]
赵小蒙's avatar
赵小蒙 committed
271
272
273
274

                    span_txt = span_txt + "</span>"

                    line_txt = line_txt + span_txt
许瑞's avatar
许瑞 committed
275

赵小蒙's avatar
赵小蒙 committed
276
                if first_overlap_span_idx == -1:
许瑞's avatar
许瑞 committed
277
                    first_overlap_span = line["spans"][i]
赵小蒙's avatar
赵小蒙 committed
278
                    first_overlap_span_idx = i
许瑞's avatar
许瑞 committed
279
                last_overlap_span = line["spans"][i]
赵小蒙's avatar
赵小蒙 committed
280
281
282
                delete_chars.append(char)

    # 第一个和最后一个char要进行检查,到底属于公式多还是属于正常span多
许瑞's avatar
许瑞 committed
283
284
285
    if len(delete_chars) > 0:
        ch0_bbox = delete_chars[0]["bbox"]
        if x_overlap_ratio(eqinfo["bbox"], ch0_bbox) < 0.51:
赵小蒙's avatar
赵小蒙 committed
286
            delete_chars.remove(delete_chars[0])
许瑞's avatar
许瑞 committed
287
288
289
    if len(delete_chars) > 0:
        ch0_bbox = delete_chars[-1]["bbox"]
        if x_overlap_ratio(eqinfo["bbox"], ch0_bbox) < 0.51:
赵小蒙's avatar
赵小蒙 committed
290
            delete_chars.remove(delete_chars[-1])
许瑞's avatar
许瑞 committed
291

赵小蒙's avatar
赵小蒙 committed
292
293
    # 计算x方向上被删除区间内的char的真实x0, x1
    if len(delete_chars):
许瑞's avatar
许瑞 committed
294
295
296
        x0, x1 = min([b["bbox"][0] for b in delete_chars]), max(
            [b["bbox"][2] for b in delete_chars]
        )
赵小蒙's avatar
赵小蒙 committed
297
298
299
    else:
        logger.debug(f"行内公式替换没有发生,尝试下一行匹配, eqinfo={eqinfo}")
        return False
许瑞's avatar
许瑞 committed
300

赵小蒙's avatar
赵小蒙 committed
301
302
    # 删除位于x0, x1这两个中间的span
    delete_span = []
许瑞's avatar
许瑞 committed
303
304
305
    for span in line["spans"]:
        span_box = span["bbox"]
        if x0 <= span_box[0] and span_box[2] <= x1:
赵小蒙's avatar
赵小蒙 committed
306
307
            delete_span.append(span)
    for span in delete_span:
许瑞's avatar
许瑞 committed
308
        line["spans"].remove(span)
赵小蒙's avatar
赵小蒙 committed
309
310

    equation_span = {
许瑞's avatar
许瑞 committed
311
        "size": 9.962599754333496,
312
        "_type": TYPE_INLINE_EQUATION,
许瑞's avatar
许瑞 committed
313
314
315
316
317
        "flags": 4,
        "font": TYPE_INLINE_EQUATION,
        "color": 0,
        "ascender": 0.9409999847412109,
        "descender": -0.3050000071525574,
318
        "text": "",
许瑞's avatar
许瑞 committed
319
        "origin": [337.1410153102337, 216.0205245153934],
320
        "bbox": eqinfo["bbox"]
许瑞's avatar
许瑞 committed
321
322
    }
    # equation_span = line['spans'][0].copy()
323
    equation_span["text"] = f" ${eqinfo['latex']}$ "
许瑞's avatar
许瑞 committed
324
325
326
    equation_span["bbox"] = [x0, equation_span["bbox"][1], x1, equation_span["bbox"][3]]
    equation_span["origin"] = [equation_span["bbox"][0], equation_span["bbox"][1]]
    equation_span["chars"] = delete_chars
327
    equation_span["_type"] = TYPE_INLINE_EQUATION
许瑞's avatar
许瑞 committed
328
329
    equation_span["_eq_bbox"] = eqinfo["bbox"]
    line["spans"].insert(first_overlap_span_idx + 1, equation_span)  # 放入公式
赵小蒙's avatar
赵小蒙 committed
330
331

    # logger.info(f"==>text is 【{line_txt}】, equation is 【{eqinfo['latex_text']}】")
许瑞's avatar
许瑞 committed
332

赵小蒙's avatar
赵小蒙 committed
333
    # 第一个、和最后一个有overlap的span进行分割,然后插入对应的位置
许瑞's avatar
许瑞 committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    first_span_chars = [
        char
        for char in first_overlap_span["chars"]
        if (char["bbox"][2] + char["bbox"][0]) / 2 < x0
    ]
    tail_span_chars = [
        char
        for char in last_overlap_span["chars"]
        if (char["bbox"][0] + char["bbox"][2]) / 2 > x1
    ]

    if len(first_span_chars) > 0:
        first_overlap_span["chars"] = first_span_chars
        first_overlap_span["text"] = "".join([char["c"] for char in first_span_chars])
        first_overlap_span["bbox"] = (
            first_overlap_span["bbox"][0],
            first_overlap_span["bbox"][1],
            max([chr["bbox"][2] for chr in first_span_chars]),
            first_overlap_span["bbox"][3],
        )
赵小蒙's avatar
赵小蒙 committed
354
355
356
357
        # first_overlap_span['_type'] = "first"
    else:
        # 删掉
        if first_overlap_span not in delete_span:
许瑞's avatar
许瑞 committed
358
359
360
            line["spans"].remove(first_overlap_span)

    if len(tail_span_chars) > 0:
361
362
363
364
365
        min_of_tail_span_x0 = min([chr["bbox"][0] for chr in tail_span_chars])
        min_of_tail_span_y0 = min([chr["bbox"][1] for chr in tail_span_chars])
        max_of_tail_span_x1 = max([chr["bbox"][2] for chr in tail_span_chars])
        max_of_tail_span_y1 = max([chr["bbox"][3] for chr in tail_span_chars])

许瑞's avatar
许瑞 committed
366
367
368
369
370
371
372
        if last_overlap_span == first_overlap_span:  # 这个时候应该插入一个新的
            tail_span_txt = "".join([char["c"] for char in tail_span_chars])
            last_span_to_insert = last_overlap_span.copy()
            last_span_to_insert["chars"] = tail_span_chars
            last_span_to_insert["text"] = "".join(
                [char["c"] for char in tail_span_chars]
            )
373
374
375
376
377
378
379
380
381
382
383
384
385
386
            if equation_span["bbox"][2] >= last_overlap_span["bbox"][2]:
                last_span_to_insert["bbox"] = (
                    min_of_tail_span_x0,
                    min_of_tail_span_y0,
                    max_of_tail_span_x1,
                    max_of_tail_span_y1
                )
            else:
                last_span_to_insert["bbox"] = (
                    min([chr["bbox"][0] for chr in tail_span_chars]),
                    last_overlap_span["bbox"][1],
                    last_overlap_span["bbox"][2],
                    last_overlap_span["bbox"][3],
                )
赵小蒙's avatar
赵小蒙 committed
387
            # 插入到公式对象之后
许瑞's avatar
许瑞 committed
388
389
390
391
392
393
394
395
396
397
398
            equation_idx = line["spans"].index(equation_span)
            line["spans"].insert(equation_idx + 1, last_span_to_insert)  # 放入公式
        else:  # 直接修改原来的span
            last_overlap_span["chars"] = tail_span_chars
            last_overlap_span["text"] = "".join([char["c"] for char in tail_span_chars])
            last_overlap_span["bbox"] = (
                min([chr["bbox"][0] for chr in tail_span_chars]),
                last_overlap_span["bbox"][1],
                last_overlap_span["bbox"][2],
                last_overlap_span["bbox"][3],
            )
赵小蒙's avatar
赵小蒙 committed
399
400
    else:
        # 删掉
许瑞's avatar
许瑞 committed
401
402
403
404
405
406
        if (
            last_overlap_span not in delete_span
            and last_overlap_span != first_overlap_span
        ):
            line["spans"].remove(last_overlap_span)

赵小蒙's avatar
赵小蒙 committed
407
    remain_txt = ""
许瑞's avatar
许瑞 committed
408
    for span in line["spans"]:
赵小蒙's avatar
赵小蒙 committed
409
        span_txt = "<span>"
许瑞's avatar
许瑞 committed
410
411
        for char in span["chars"]:
            span_txt = span_txt + char["c"]
赵小蒙's avatar
赵小蒙 committed
412
413
414
415

        span_txt = span_txt + "</span>"

        remain_txt = remain_txt + span_txt
许瑞's avatar
许瑞 committed
416

赵小蒙's avatar
赵小蒙 committed
417
    # logger.info(f"<== succ replace, text is 【{remain_txt}】, equation is 【{eqinfo['latex_text']}】")
许瑞's avatar
许瑞 committed
418

赵小蒙's avatar
赵小蒙 committed
419
420
421
422
423
    return True


def replace_eq_blk(eqinfo, text_block):
    """替换行内公式"""
许瑞's avatar
许瑞 committed
424
425
426
427
428
429
430
431
432
433
    for line in text_block["lines"]:
        line_bbox = line["bbox"]
        if (
            _is_xin(eqinfo["bbox"], line_bbox)
            or __y_overlap_ratio(eqinfo["bbox"], line_bbox) > 0.6
        ):  # 定位到行, 使用y方向重合率是因为有的时候,一个行的宽度会小于公式位置宽度:行很高,公式很窄,
            replace_succ = replace_line_v2(eqinfo, line)
            if (
                not replace_succ
            ):  # 有的时候,一个pdf的line高度从API里会计算的有问题,因此在行内span级别会替换不成功,这就需要继续重试下一行
赵小蒙's avatar
赵小蒙 committed
434
435
436
437
438
439
440
441
442
443
444
                continue
            else:
                break
    else:
        return False
    return True


def replace_inline_equations(inline_equation_bboxes, raw_text_blocks):
    """替换行内公式"""
    for eqinfo in inline_equation_bboxes:
许瑞's avatar
许瑞 committed
445
        eqbox = eqinfo["bbox"]
赵小蒙's avatar
赵小蒙 committed
446
        for blk in raw_text_blocks:
许瑞's avatar
许瑞 committed
447
            if _is_xin(eqbox, blk["bbox"]):
赵小蒙's avatar
赵小蒙 committed
448
449
450
451
452
453
454
                if not replace_eq_blk(eqinfo, blk):
                    logger.error(f"行内公式没有替换成功:{eqinfo} ")
                else:
                    break

    return raw_text_blocks

许瑞's avatar
许瑞 committed
455

赵小蒙's avatar
赵小蒙 committed
456
457
458
def remove_chars_in_text_blocks(text_blocks):
    """删除text_blocks里的char"""
    for blk in text_blocks:
许瑞's avatar
许瑞 committed
459
460
        for line in blk["lines"]:
            for span in line["spans"]:
赵小蒙's avatar
赵小蒙 committed
461
462
463
464
                _ = span.pop("chars", "no such key")
    return text_blocks


许瑞's avatar
许瑞 committed
465
466
467
def replace_equations_in_textblock(
    raw_text_blocks, inline_equation_bboxes, interline_equation_bboxes
):
赵小蒙's avatar
赵小蒙 committed
468
469
470
    """
    替换行间和和行内公式为latex
    """
471
472
    # debug 
    from magic_pdf.debug_utils import flatten_spans
许瑞's avatar
许瑞 committed
473
474
475
476

    raw_text_blocks = remove_text_block_in_interline_equation_bbox(
        interline_equation_bboxes, raw_text_blocks
    )  # 消除重叠:第一步,在公式内部的
477
478
    flatten_spans(raw_text_blocks)

许瑞's avatar
许瑞 committed
479
480
481
    raw_text_blocks = remove_text_block_overlap_interline_equation_bbox(
        interline_equation_bboxes, raw_text_blocks
    )  # 消重,第二步,和公式覆盖的
482
    flatten_spans(raw_text_blocks)
许瑞's avatar
许瑞 committed
483

484
485
    insert_interline_equations_textblock(interline_equation_bboxes, raw_text_blocks)
    flatten_spans(raw_text_blocks)
赵小蒙's avatar
赵小蒙 committed
486
    raw_text_blocks = replace_inline_equations(inline_equation_bboxes, raw_text_blocks)
487
    flatten_spans(raw_text_blocks)  
赵小蒙's avatar
赵小蒙 committed
488
    return raw_text_blocks
许瑞's avatar
许瑞 committed
489

赵小蒙's avatar
赵小蒙 committed
490
491

def draw_block_on_pdf_with_txt_replace_eq_bbox(json_path, pdf_path):
许瑞's avatar
许瑞 committed
492
    """ """
赵小蒙's avatar
赵小蒙 committed
493
    new_pdf = f"{Path(pdf_path).parent}/{Path(pdf_path).stem}.step3-消除行内公式text_block.pdf"
许瑞's avatar
许瑞 committed
494
    with open(json_path, "r", encoding="utf-8") as f:
赵小蒙's avatar
赵小蒙 committed
495
496
497
498
        obj = json.loads(f.read())

    if os.path.exists(new_pdf):
        os.remove(new_pdf)
许瑞's avatar
许瑞 committed
499
500
    new_doc = fitz.open("")

赵小蒙's avatar
赵小蒙 committed
501
502
503
504
    doc = fitz.open(pdf_path)
    new_doc = fitz.open(pdf_path)
    for i in range(len(new_doc)):
        page = new_doc[i]
许瑞's avatar
许瑞 committed
505
506
507
508
509
510
511
512
513
        inline_equation_bboxes = obj[f"page_{i}"]["inline_equations"]
        interline_equation_bboxes = obj[f"page_{i}"]["interline_equations"]
        raw_text_blocks = obj[f"page_{i}"]["preproc_blocks"]
        raw_text_blocks = remove_text_block_in_interline_equation_bbox(
            interline_equation_bboxes, raw_text_blocks
        )  # 消除重叠:第一步,在公式内部的
        raw_text_blocks = remove_text_block_overlap_interline_equation_bbox(
            interline_equation_bboxes, raw_text_blocks
        )  # 消重,第二步,和公式覆盖的
赵小蒙's avatar
赵小蒙 committed
514
        insert_interline_equations_textblock(interline_equation_bboxes, raw_text_blocks)
许瑞's avatar
许瑞 committed
515
516
517
518
        raw_text_blocks = replace_inline_equations(
            inline_equation_bboxes, raw_text_blocks
        )

赵小蒙's avatar
赵小蒙 committed
519
        # 为了检验公式是否重复,把每一行里,含有公式的span背景改成黄色的
许瑞's avatar
许瑞 committed
520
        color_map = [fitz.pdfcolor["blue"], fitz.pdfcolor["green"]]
赵小蒙's avatar
赵小蒙 committed
521
522
        j = 0
        for blk in raw_text_blocks:
许瑞's avatar
许瑞 committed
523
524
            for i, line in enumerate(blk["lines"]):

赵小蒙's avatar
赵小蒙 committed
525
526
527
528
529
530
                # line_box = line['bbox']
                # shape = page.new_shape()
                # shape.draw_rect(line_box)
                # shape.finish(color=fitz.pdfcolor['red'], fill=color_map[j%2], fill_opacity=0.3)
                # shape.commit()
                # j = j+1
许瑞's avatar
许瑞 committed
531
532

                for i, span in enumerate(line["spans"]):
赵小蒙's avatar
赵小蒙 committed
533
                    shape_page = page.new_shape()
许瑞's avatar
许瑞 committed
534
535
536
537
538
539
540
541
                    span_type = span.get("_type")
                    color = fitz.pdfcolor["blue"]
                    if span_type == "first":
                        color = fitz.pdfcolor["blue"]
                    elif span_type == "tail":
                        color = fitz.pdfcolor["green"]
                    elif span_type == TYPE_INLINE_EQUATION:
                        color = fitz.pdfcolor["black"]
赵小蒙's avatar
赵小蒙 committed
542
543
                    else:
                        color = None
许瑞's avatar
许瑞 committed
544
545

                    b = span["bbox"]
赵小蒙's avatar
赵小蒙 committed
546
                    shape_page.draw_rect(b)
许瑞's avatar
许瑞 committed
547

赵小蒙's avatar
赵小蒙 committed
548
549
550
551
552
                    shape_page.finish(color=None, fill=color, fill_opacity=0.3)
                    shape_page.commit()

    new_doc.save(new_pdf)
    logger.info(f"save ok {new_pdf}")
许瑞's avatar
许瑞 committed
553
    final_json = json.dumps(obj, ensure_ascii=False, indent=2)
赵小蒙's avatar
赵小蒙 committed
554
555
    with open("equations_test/final_json.json", "w") as f:
        f.write(final_json)
许瑞's avatar
许瑞 committed
556

赵小蒙's avatar
赵小蒙 committed
557
558
559
    return new_pdf


许瑞's avatar
许瑞 committed
560
if __name__ == "__main__":
赵小蒙's avatar
赵小蒙 committed
561
562
    # draw_block_on_pdf_with_txt_replace_eq_bbox(new_json_path, equation_color_pdf)
    pass