para_split_v3.py 15 KB
Newer Older
1
2
import copy

3
4
from loguru import logger

5
from magic_pdf.libs.Constants import LINES_DELETED, CROSS_PAGE
6
from magic_pdf.libs.ocr_content_type import BlockType, ContentType
7

8
LINE_STOP_FLAG = ('.', '!', '?', '。', '!', '?', ')', ')', '"', '”', ':', ':', ';', ';')
9
10
11
12
13
14
LIST_END_FLAG = ('.', '。', ';', ';')


class ListLineTag:
    IS_LIST_START_LINE = "is_list_start_line"
    IS_LIST_END_LINE = "is_list_end_line"
15
16
17


def __process_blocks(blocks):
18
19
20
    # 对所有block预处理
    # 1.通过title和interline_equation将block分组
    # 2.bbox边界根据line信息重置
21
22
23
24
25
26
27
28
29
30

    result = []
    current_group = []

    for i in range(len(blocks)):
        current_block = blocks[i]

        # 如果当前块是 text 类型
        if current_block['type'] == 'text':
            current_block["bbox_fs"] = copy.deepcopy(current_block["bbox"])
31
            if 'lines' in current_block and len(current_block["lines"]) > 0:
32
33
34
35
36
37
                current_block['bbox_fs'] = [min([line['bbox'][0] for line in current_block['lines']]),
                                            min([line['bbox'][1] for line in current_block['lines']]),
                                            max([line['bbox'][2] for line in current_block['lines']]),
                                            max([line['bbox'][3] for line in current_block['lines']])]
            current_group.append(current_block)

38
39
40
41
42
43
44
        # 检查下一个块是否存在
        if i + 1 < len(blocks):
            next_block = blocks[i + 1]
            # 如果下一个块不是 text 类型且是 title 或 interline_equation 类型
            if next_block['type'] in ['title', 'interline_equation']:
                result.append(current_group)
                current_group = []
45
46
47
48
49
50
51
52

    # 处理最后一个 group
    if current_group:
        result.append(current_group)

    return result


53
def __is_list_or_index_block(block):
54
55
56
57
    # 一个block如果是list block 应该同时满足以下特征
    # 1.block内有多个line 2.block 内有多个line左侧顶格写 3.block内有多个line 右侧不顶格(狗牙状)
    # 1.block内有多个line 2.block 内有多个line左侧顶格写 3.多个line以endflag结尾
    # 1.block内有多个line 2.block 内有多个line左侧顶格写 3.block内有多个line 左侧不顶格
58
59
60
61

    # index block 是一种特殊的list block
    # 一个block如果是index block 应该同时满足以下特征
    # 1.block内有多个line 2.block 内有多个line两侧均顶格写 3.line的开头或者结尾均为数字
62
    if len(block['lines']) >= 2:
63
64
65
        first_line = block['lines'][0]
        line_height = first_line['bbox'][3] - first_line['bbox'][1]
        block_weight = block['bbox_fs'][2] - block['bbox_fs'][0]
66
        block_height = block['bbox_fs'][3] - block['bbox_fs'][1]
67
        page_weight, page_height = block['page_size']
68
69
70
71

        left_close_num = 0
        left_not_close_num = 0
        right_not_close_num = 0
72
        right_close_num = 0
73
        lines_text_list = []
74
75
        center_close_num = 0
        external_sides_not_close_num = 0
76
77
        multiple_para_flag = False
        last_line = block['lines'][-1]
78

79
80
81
82
83
84
        if page_weight == 0:
            block_weight_radio = 0
        else:
            block_weight_radio = block_weight / page_weight
        # logger.info(f"block_weight_radio: {block_weight_radio}")

85
86
87
88
89
90
91
        # 如果首行左边不顶格而右边顶格,末行左边顶格而右边不顶格 (第一行可能可以右边不顶格)
        if (first_line['bbox'][0] - block['bbox_fs'][0] > line_height / 2 and
                abs(last_line['bbox'][0] - block['bbox_fs'][0]) < line_height / 2 and
                block['bbox_fs'][2] - last_line['bbox'][2] > line_height
        ):
            multiple_para_flag = True

92
        for line in block['lines']:
93
94
            line_mid_x = (line['bbox'][0] + line['bbox'][2]) / 2
            block_mid_x = (block['bbox_fs'][0] + block['bbox_fs'][2]) / 2
95
96
97
98
            if (
                    line['bbox'][0] - block['bbox_fs'][0] > 0.8 * line_height and
                    block['bbox_fs'][2] - line['bbox'][2] > 0.8 * line_height
            ):
99
                external_sides_not_close_num += 1
100
            if abs(line_mid_x - block_mid_x) < line_height / 2:
101
102
                center_close_num += 1

103
104
105
106
107
108
109
            line_text = ""

            for span in line['spans']:
                span_type = span['type']
                if span_type == ContentType.Text:
                    line_text += span['content'].strip()

hyastar's avatar
hyastar committed
110
            # 添加所有文本,包括空行,保持与block['lines']长度一致
111
112
113
            lines_text_list.append(line_text)

            # 计算line左侧顶格数量是否大于2,是否顶格用abs(block['bbox_fs'][0] - line['bbox'][0]) < line_height/2 来判断
114
            if abs(block['bbox_fs'][0] - line['bbox'][0]) < line_height / 2:
115
116
117
118
                left_close_num += 1
            elif line['bbox'][0] - block['bbox_fs'][0] > line_height:
                left_not_close_num += 1

119
            # 计算右侧是否顶格
120
            if abs(block['bbox_fs'][2] - line['bbox'][2]) < line_height:
121
                right_close_num += 1
122
123
            else:
                # 右侧不顶格情况下是否有一段距离,拍脑袋用0.3block宽度做阈值
124
125
                # 0.26
                closed_area = 0.35 * block_weight
126
127
                if block['bbox_fs'][2] - line['bbox'][2] > closed_area:
                    right_not_close_num += 1
128

129
130
        # 判断lines_text_list中的元素是否有超过80%都以LIST_END_FLAG结尾
        line_end_flag = False
131
132
        # 判断lines_text_list中的元素是否有超过80%都以数字开头或都以数字结尾
        line_num_flag = False
133
134
135
        num_start_count = 0
        num_end_count = 0
        flag_end_count = 0
hyastar's avatar
hyastar committed
136

137
138
139
        if len(lines_text_list) > 0:
            for line_text in lines_text_list:
                if len(line_text) > 0:
140
                    if line_text[-1] in LIST_END_FLAG:
141
                        flag_end_count += 1
142
143
144
145
146
147
148
                    if line_text[0].isdigit():
                        num_start_count += 1
                    if line_text[-1].isdigit():
                        num_end_count += 1

            if num_start_count / len(lines_text_list) >= 0.8 or num_end_count / len(lines_text_list) >= 0.8:
                line_num_flag = True
hyastar's avatar
hyastar committed
149
150
            if flag_end_count / len(lines_text_list) >= 0.8:
                line_end_flag = True
151

152
        # 有的目录右侧不贴边, 目前认为左边或者右边有一边全贴边,且符合数字规则极为index
153
        if ((left_close_num / len(block['lines']) >= 0.8 or right_close_num / len(block['lines']) >= 0.8)
154
155
                and line_num_flag
        ):
156
157
            for line in block['lines']:
                line[ListLineTag.IS_LIST_START_LINE] = True
158
            return BlockType.Index
159

160
        # 全部line都居中的特殊list识别,每行都需要换行,特征是多行,且大多数行都前后not_close,每line中点x坐标接近
161
162
163
164
165
166
167
        # 补充条件block的长宽比有要求
        elif (
                external_sides_not_close_num >= 2 and
                center_close_num == len(block['lines']) and
                external_sides_not_close_num / len(block['lines']) >= 0.5 and
                block_height / block_weight > 0.4
        ):
168
169
170
            for line in block['lines']:
                line[ListLineTag.IS_LIST_START_LINE] = True
            return BlockType.List
171

172
173
174
175
176
177
        elif (
                left_close_num >= 2
                and (right_not_close_num >= 2 or line_end_flag or left_not_close_num >= 2)
                and not multiple_para_flag
                # and block_weight_radio > 0.27
        ):
178
            # 处理一种特殊的没有缩进的list,所有行都贴左边,通过右边的空隙判断是否是item尾
179
            if left_close_num / len(block['lines']) > 0.8:
180
181
182
183
184
185
186
187
                # 这种是每个item只有一行,且左边都贴边的短item list
                if flag_end_count == 0 and right_close_num / len(block['lines']) < 0.5:
                    for line in block['lines']:
                        if abs(block['bbox_fs'][0] - line['bbox'][0]) < line_height / 2:
                            line[ListLineTag.IS_LIST_START_LINE] = True
                # 这种是大部分line item 都有结束标识符的情况,按结束标识符区分不同item
                elif line_end_flag:
                    for i, line in enumerate(block['lines']):
hyastar's avatar
hyastar committed
188
                        if len(lines_text_list[i]) > 0 and lines_text_list[i][-1] in LIST_END_FLAG:
189
190
                            line[ListLineTag.IS_LIST_END_LINE] = True
                            if i + 1 < len(block['lines']):
191
                                block['lines'][i + 1][ListLineTag.IS_LIST_START_LINE] = True
192
193
194
195
196
197
198
                # line item基本没有结束标识符,而且也没有缩进,按右侧空隙判断哪些是item end
                else:
                    line_start_flag = False
                    for i, line in enumerate(block['lines']):
                        if line_start_flag:
                            line[ListLineTag.IS_LIST_START_LINE] = True
                            line_start_flag = False
hyastar's avatar
hyastar committed
199

200
                        if abs(block['bbox_fs'][2] - line['bbox'][2]) > 0.1 * block_weight:
201
202
                            line[ListLineTag.IS_LIST_END_LINE] = True
                            line_start_flag = True
hyastar's avatar
hyastar committed
203
204
            # 一种有缩进的特殊有序list,start line 左侧不贴边且以数字开头,end line 以 IS_LIST_END_FLAG 结尾且数量和start line 一致
            elif num_start_count >= 2 and num_start_count == flag_end_count:
205
                for i, line in enumerate(block['lines']):
hyastar's avatar
hyastar committed
206
207
208
209
210
                    if len(lines_text_list[i]) > 0:
                        if lines_text_list[i][0].isdigit():
                            line[ListLineTag.IS_LIST_START_LINE] = True
                        if lines_text_list[i][-1] in LIST_END_FLAG:
                            line[ListLineTag.IS_LIST_END_LINE] = True
211
212
213
214
215
216
217
            else:
                # 正常有缩进的list处理
                for line in block['lines']:
                    if abs(block['bbox_fs'][0] - line['bbox'][0]) < line_height / 2:
                        line[ListLineTag.IS_LIST_START_LINE] = True
                    if abs(block['bbox_fs'][2] - line['bbox'][2]) > line_height:
                        line[ListLineTag.IS_LIST_END_LINE] = True
218
219

            return BlockType.List
220
        else:
221
            return BlockType.Text
222
    else:
223
        return BlockType.Text
224
225
226


def __merge_2_text_blocks(block1, block2):
227
228
229
    if len(block1['lines']) > 0:
        first_line = block1['lines'][0]
        line_height = first_line['bbox'][3] - first_line['bbox'][1]
230
231
232
233
        block1_weight = block1['bbox'][2] - block1['bbox'][0]
        block2_weight = block2['bbox'][2] - block2['bbox'][0]
        min_block_weight = min(block1_weight, block2_weight)
        if abs(block1['bbox_fs'][0] - first_line['bbox'][0]) < line_height / 2:
234
235
236
237
            last_line = block2['lines'][-1]
            if len(last_line['spans']) > 0:
                last_span = last_line['spans'][-1]
                line_height = last_line['bbox'][3] - last_line['bbox'][1]
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
                if len(first_line['spans']) > 0:
                    first_span = first_line['spans'][0]
                    if len(first_span['content']) > 0:
                        span_start_with_num = first_span['content'][0].isdigit()
                        if (abs(block2['bbox_fs'][2] - last_line['bbox'][2]) < line_height
                                and not last_span['content'].endswith(LINE_STOP_FLAG)
                                # 两个block宽度差距超过2倍也不合并
                                and abs(block1_weight - block2_weight) < min_block_weight
                                and not span_start_with_num
                        ):
                            if block1['page_num'] != block2['page_num']:
                                for line in block1['lines']:
                                    for span in line['spans']:
                                        span[CROSS_PAGE] = True
                            block2['lines'].extend(block1['lines'])
                            block1['lines'] = []
                            block1[LINES_DELETED] = True
255
256
257
258

    return block1, block2


259
260
261
262
263
264
265
266
267
268
269
270
def __merge_2_list_blocks(block1, block2):
    if block1['page_num'] != block2['page_num']:
        for line in block1['lines']:
            for span in line['spans']:
                span[CROSS_PAGE] = True
    block2['lines'].extend(block1['lines'])
    block1['lines'] = []
    block1[LINES_DELETED] = True

    return block1, block2


271
272
273
274
275
276
277
278
279
def __is_list_group(text_blocks_group):
    # list group的特征是一个group内的所有block都满足以下条件
    # 1.每个block都不超过3行 2. 每个block 的左边界都比较接近(逻辑简单点先不加这个规则)
    for block in text_blocks_group:
        if len(block['lines']) > 3:
            return False
    return True


280
281
282
def __para_merge_page(blocks):
    page_text_blocks_groups = __process_blocks(blocks)
    for text_blocks_group in page_text_blocks_groups:
283
284

        if len(text_blocks_group) > 0:
285
            # 需要先在合并前对所有block判断是否为list or index block
286
            for block in text_blocks_group:
287
288
289
                block_type = __is_list_or_index_block(block)
                block['type'] = block_type
                # logger.info(f"{block['type']}:{block}")
290

291
        if len(text_blocks_group) > 1:
292
293
294
295

            # 在合并前判断这个group 是否是一个 list group
            is_list_group = __is_list_group(text_blocks_group)

296
            # 倒序遍历
297
            for i in range(len(text_blocks_group) - 1, -1, -1):
298
                current_block = text_blocks_group[i]
299

300
301
302
                # 检查是否有前一个块
                if i - 1 >= 0:
                    prev_block = text_blocks_group[i - 1]
303

304
                    if current_block['type'] == 'text' and prev_block['type'] == 'text' and not is_list_group:
305
                        __merge_2_text_blocks(current_block, prev_block)
306
307
308
309
                    elif (
                            (current_block['type'] == BlockType.List and prev_block['type'] == BlockType.List) or
                            (current_block['type'] == BlockType.Index and prev_block['type'] == BlockType.Index)
                    ):
310
                        __merge_2_list_blocks(current_block, prev_block)
311

312
313
314
315
316
317
318
319
320
321
        else:
            continue


def para_split(pdf_info_dict, debug_mode=False):
    all_blocks = []
    for page_num, page in pdf_info_dict.items():
        blocks = copy.deepcopy(page['preproc_blocks'])
        for block in blocks:
            block['page_num'] = page_num
322
            block['page_size'] = page['page_size']
323
324
325
326
327
328
329
330
331
332
333
        all_blocks.extend(blocks)

    __para_merge_page(all_blocks)
    for page_num, page in pdf_info_dict.items():
        page['para_blocks'] = []
        for block in all_blocks:
            if block['page_num'] == page_num:
                page['para_blocks'].append(block)


if __name__ == '__main__':
334
    input_blocks = []
335
    # 调用函数
336
337
    groups = __process_blocks(input_blocks)
    for group_index, group in enumerate(groups):
338
        print(f"Group {group_index}: {group}")