magic-pdf 0.10.1__py3-none-any.whl → 0.10.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. magic_pdf/dict2md/ocr_mkcontent.py +16 -22
  2. magic_pdf/filter/pdf_meta_scan.py +5 -19
  3. magic_pdf/libs/commons.py +0 -161
  4. magic_pdf/libs/draw_bbox.py +2 -3
  5. magic_pdf/libs/markdown_utils.py +0 -21
  6. magic_pdf/libs/pdf_check.py +52 -25
  7. magic_pdf/libs/pdf_image_tools.py +2 -1
  8. magic_pdf/libs/version.py +1 -1
  9. magic_pdf/model/doc_analyze_by_custom_model.py +2 -2
  10. magic_pdf/model/magic_model.py +0 -30
  11. magic_pdf/model/pp_structure_v2.py +23 -3
  12. magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py +50 -29
  13. magic_pdf/model/sub_modules/ocr/paddleocr/ppocr_273_mod.py +11 -9
  14. magic_pdf/model/sub_modules/table/tablemaster/tablemaster_paddle.py +2 -2
  15. magic_pdf/para/para_split_v3.py +21 -7
  16. magic_pdf/pdf_parse_union_core_v2.py +134 -146
  17. magic_pdf/pre_proc/construct_page_dict.py +0 -55
  18. magic_pdf/pre_proc/cut_image.py +0 -37
  19. magic_pdf/pre_proc/ocr_detect_all_bboxes.py +5 -178
  20. magic_pdf/pre_proc/ocr_dict_merge.py +1 -224
  21. magic_pdf/pre_proc/ocr_span_list_modify.py +2 -252
  22. magic_pdf/rw/S3ReaderWriter.py +1 -1
  23. {magic_pdf-0.10.1.dist-info → magic_pdf-0.10.3.dist-info}/METADATA +3 -78
  24. {magic_pdf-0.10.1.dist-info → magic_pdf-0.10.3.dist-info}/RECORD +28 -79
  25. magic_pdf/dict2md/mkcontent.py +0 -438
  26. magic_pdf/layout/__init__.py +0 -0
  27. magic_pdf/layout/bbox_sort.py +0 -681
  28. magic_pdf/layout/layout_det_utils.py +0 -182
  29. magic_pdf/layout/layout_sort.py +0 -921
  30. magic_pdf/layout/layout_spiler_recog.py +0 -101
  31. magic_pdf/layout/mcol_sort.py +0 -336
  32. magic_pdf/libs/calc_span_stats.py +0 -239
  33. magic_pdf/libs/detect_language_from_model.py +0 -21
  34. magic_pdf/libs/nlp_utils.py +0 -203
  35. magic_pdf/libs/textbase.py +0 -33
  36. magic_pdf/libs/vis_utils.py +0 -308
  37. magic_pdf/para/block_continuation_processor.py +0 -562
  38. magic_pdf/para/block_termination_processor.py +0 -480
  39. magic_pdf/para/commons.py +0 -222
  40. magic_pdf/para/denoise.py +0 -246
  41. magic_pdf/para/draw.py +0 -121
  42. magic_pdf/para/exceptions.py +0 -198
  43. magic_pdf/para/layout_match_processor.py +0 -40
  44. magic_pdf/para/para_split.py +0 -807
  45. magic_pdf/para/para_split_v2.py +0 -959
  46. magic_pdf/para/raw_processor.py +0 -207
  47. magic_pdf/para/stats.py +0 -268
  48. magic_pdf/para/title_processor.py +0 -1014
  49. magic_pdf/pdf_parse_union_core.py +0 -345
  50. magic_pdf/post_proc/__init__.py +0 -0
  51. magic_pdf/post_proc/detect_para.py +0 -3472
  52. magic_pdf/post_proc/pdf_post_filter.py +0 -60
  53. magic_pdf/post_proc/remove_footnote.py +0 -153
  54. magic_pdf/pre_proc/citationmarker_remove.py +0 -161
  55. magic_pdf/pre_proc/detect_equation.py +0 -134
  56. magic_pdf/pre_proc/detect_footer_by_model.py +0 -64
  57. magic_pdf/pre_proc/detect_footer_header_by_statistics.py +0 -284
  58. magic_pdf/pre_proc/detect_footnote.py +0 -170
  59. magic_pdf/pre_proc/detect_header.py +0 -64
  60. magic_pdf/pre_proc/detect_images.py +0 -647
  61. magic_pdf/pre_proc/detect_page_number.py +0 -64
  62. magic_pdf/pre_proc/detect_tables.py +0 -62
  63. magic_pdf/pre_proc/equations_replace.py +0 -550
  64. magic_pdf/pre_proc/fix_image.py +0 -244
  65. magic_pdf/pre_proc/fix_table.py +0 -270
  66. magic_pdf/pre_proc/main_text_font.py +0 -23
  67. magic_pdf/pre_proc/ocr_detect_layout.py +0 -133
  68. magic_pdf/pre_proc/pdf_pre_filter.py +0 -78
  69. magic_pdf/pre_proc/post_layout_split.py +0 -0
  70. magic_pdf/pre_proc/remove_colored_strip_bbox.py +0 -101
  71. magic_pdf/pre_proc/remove_footer_header.py +0 -114
  72. magic_pdf/pre_proc/remove_rotate_bbox.py +0 -236
  73. magic_pdf/pre_proc/resolve_bbox_conflict.py +0 -184
  74. magic_pdf/pre_proc/solve_line_alien.py +0 -29
  75. magic_pdf/pre_proc/statistics.py +0 -12
  76. {magic_pdf-0.10.1.dist-info → magic_pdf-0.10.3.dist-info}/LICENSE.md +0 -0
  77. {magic_pdf-0.10.1.dist-info → magic_pdf-0.10.3.dist-info}/WHEEL +0 -0
  78. {magic_pdf-0.10.1.dist-info → magic_pdf-0.10.3.dist-info}/entry_points.txt +0 -0
  79. {magic_pdf-0.10.1.dist-info → magic_pdf-0.10.3.dist-info}/top_level.txt +0 -0
@@ -1,438 +0,0 @@
1
- import math
2
-
3
- from loguru import logger
4
-
5
- from magic_pdf.config.ocr_content_type import ContentType
6
- from magic_pdf.libs.boxbase import (find_bottom_nearest_text_bbox,
7
- find_top_nearest_text_bbox)
8
- from magic_pdf.libs.commons import join_path
9
-
10
- TYPE_INLINE_EQUATION = ContentType.InlineEquation
11
- TYPE_INTERLINE_EQUATION = ContentType.InterlineEquation
12
- UNI_FORMAT_TEXT_TYPE = ['text', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
13
-
14
-
15
- @DeprecationWarning
16
- def mk_nlp_markdown_1(para_dict: dict):
17
- """对排序后的bboxes拼接内容."""
18
- content_lst = []
19
- for _, page_info in para_dict.items():
20
- para_blocks = page_info.get('para_blocks')
21
- if not para_blocks:
22
- continue
23
-
24
- for block in para_blocks:
25
- item = block['paras']
26
- for _, p in item.items():
27
- para_text = p['para_text']
28
- is_title = p['is_para_title']
29
- title_level = p['para_title_level']
30
- md_title_prefix = '#' * title_level
31
- if is_title:
32
- content_lst.append(f'{md_title_prefix} {para_text}')
33
- else:
34
- content_lst.append(para_text)
35
-
36
- content_text = '\n\n'.join(content_lst)
37
-
38
- return content_text
39
-
40
-
41
- # 找到目标字符串在段落中的索引
42
- def __find_index(paragraph, target):
43
- index = paragraph.find(target)
44
- if index != -1:
45
- return index
46
- else:
47
- return None
48
-
49
-
50
- def __insert_string(paragraph, target, position):
51
- new_paragraph = paragraph[:position] + target + paragraph[position:]
52
- return new_paragraph
53
-
54
-
55
- def __insert_after(content, image_content, target):
56
- """在content中找到target,将image_content插入到target后面."""
57
- index = content.find(target)
58
- if index != -1:
59
- content = (
60
- content[: index + len(target)]
61
- + '\n\n'
62
- + image_content
63
- + '\n\n'
64
- + content[index + len(target) :]
65
- )
66
- else:
67
- logger.error(
68
- f"Can't find the location of image {image_content} in the markdown file, search target is {target}"
69
- )
70
- return content
71
-
72
-
73
- def __insert_before(content, image_content, target):
74
- """在content中找到target,将image_content插入到target前面."""
75
- index = content.find(target)
76
- if index != -1:
77
- content = content[:index] + '\n\n' + image_content + '\n\n' + content[index:]
78
- else:
79
- logger.error(
80
- f"Can't find the location of image {image_content} in the markdown file, search target is {target}"
81
- )
82
- return content
83
-
84
-
85
- @DeprecationWarning
86
- def mk_mm_markdown_1(para_dict: dict):
87
- """拼装多模态markdown."""
88
- content_lst = []
89
- for _, page_info in para_dict.items():
90
- page_lst = [] # 一个page内的段落列表
91
- para_blocks = page_info.get('para_blocks')
92
- pymu_raw_blocks = page_info.get('preproc_blocks')
93
-
94
- all_page_images = []
95
- all_page_images.extend(page_info.get('images', []))
96
- all_page_images.extend(page_info.get('image_backup', []))
97
- all_page_images.extend(page_info.get('tables', []))
98
- all_page_images.extend(page_info.get('table_backup', []))
99
-
100
- if not para_blocks or not pymu_raw_blocks: # 只有图片的拼接的场景
101
- for img in all_page_images:
102
- page_lst.append(f"![]({img['image_path']})") # TODO 图片顺序
103
- page_md = '\n\n'.join(page_lst)
104
-
105
- else:
106
- for block in para_blocks:
107
- item = block['paras']
108
- for _, p in item.items():
109
- para_text = p['para_text']
110
- is_title = p['is_para_title']
111
- title_level = p['para_title_level']
112
- md_title_prefix = '#' * title_level
113
- if is_title:
114
- page_lst.append(f'{md_title_prefix} {para_text}')
115
- else:
116
- page_lst.append(para_text)
117
-
118
- """拼装成一个页面的文本"""
119
- page_md = '\n\n'.join(page_lst)
120
- """插入图片"""
121
- for img in all_page_images:
122
- imgbox = img['bbox']
123
- img_content = f"![]({img['image_path']})"
124
- # 先看在哪个block内
125
- for block in pymu_raw_blocks:
126
- bbox = block['bbox']
127
- if (
128
- bbox[0] - 1 <= imgbox[0] < bbox[2] + 1
129
- and bbox[1] - 1 <= imgbox[1] < bbox[3] + 1
130
- ): # 确定在block内
131
- for l in block['lines']: # noqa: E741
132
- line_box = l['bbox']
133
- if (
134
- line_box[0] - 1 <= imgbox[0] < line_box[2] + 1
135
- and line_box[1] - 1 <= imgbox[1] < line_box[3] + 1
136
- ): # 在line内的,插入line前面
137
- line_txt = ''.join([s['text'] for s in l['spans']])
138
- page_md = __insert_before(
139
- page_md, img_content, line_txt
140
- )
141
- break
142
- break
143
- else: # 在行与行之间
144
- # 找到图片x0,y0与line的x0,y0最近的line
145
- min_distance = 100000
146
- min_line = None
147
- for l in block['lines']: # noqa: E741
148
- line_box = l['bbox']
149
- distance = math.sqrt(
150
- (line_box[0] - imgbox[0]) ** 2
151
- + (line_box[1] - imgbox[1]) ** 2
152
- )
153
- if distance < min_distance:
154
- min_distance = distance
155
- min_line = l
156
- if min_line:
157
- line_txt = ''.join(
158
- [s['text'] for s in min_line['spans']]
159
- )
160
- img_h = imgbox[3] - imgbox[1]
161
- if min_distance < img_h: # 文字在图片前面
162
- page_md = __insert_after(
163
- page_md, img_content, line_txt
164
- )
165
- else:
166
- page_md = __insert_before(
167
- page_md, img_content, line_txt
168
- )
169
- else:
170
- logger.error(
171
- f"Can't find the location of image {img['image_path']} in the markdown file #1"
172
- )
173
- else: # 应当在两个block之间
174
- # 找到上方最近的block,如果上方没有就找大下方最近的block
175
- top_txt_block = find_top_nearest_text_bbox(pymu_raw_blocks, imgbox)
176
- if top_txt_block:
177
- line_txt = ''.join(
178
- [s['text'] for s in top_txt_block['lines'][-1]['spans']]
179
- )
180
- page_md = __insert_after(page_md, img_content, line_txt)
181
- else:
182
- bottom_txt_block = find_bottom_nearest_text_bbox(
183
- pymu_raw_blocks, imgbox
184
- )
185
- if bottom_txt_block:
186
- line_txt = ''.join(
187
- [
188
- s['text']
189
- for s in bottom_txt_block['lines'][0]['spans']
190
- ]
191
- )
192
- page_md = __insert_before(page_md, img_content, line_txt)
193
- else:
194
- logger.error(
195
- f"Can't find the location of image {img['image_path']} in the markdown file #2"
196
- )
197
-
198
- content_lst.append(page_md)
199
-
200
- """拼装成全部页面的文本"""
201
- content_text = '\n\n'.join(content_lst)
202
-
203
- return content_text
204
-
205
-
206
- def __insert_after_para(text, type, element, content_list):
207
- """在content_list中找到text,将image_path作为一个新的node插入到text后面."""
208
- for i, c in enumerate(content_list):
209
- content_type = c.get('type')
210
- if content_type in UNI_FORMAT_TEXT_TYPE and text in c.get('text', ''):
211
- if type == 'image':
212
- content_node = {
213
- 'type': 'image',
214
- 'img_path': element.get('image_path'),
215
- 'img_alt': '',
216
- 'img_title': '',
217
- 'img_caption': '',
218
- }
219
- elif type == 'table':
220
- content_node = {
221
- 'type': 'table',
222
- 'img_path': element.get('image_path'),
223
- 'table_latex': element.get('text'),
224
- 'table_title': '',
225
- 'table_caption': '',
226
- 'table_quality': element.get('quality'),
227
- }
228
- content_list.insert(i + 1, content_node)
229
- break
230
- else:
231
- logger.error(
232
- f"Can't find the location of image {element.get('image_path')} in the markdown file, search target is {text}"
233
- )
234
-
235
-
236
- def __insert_before_para(text, type, element, content_list):
237
- """在content_list中找到text,将image_path作为一个新的node插入到text前面."""
238
- for i, c in enumerate(content_list):
239
- content_type = c.get('type')
240
- if content_type in UNI_FORMAT_TEXT_TYPE and text in c.get('text', ''):
241
- if type == 'image':
242
- content_node = {
243
- 'type': 'image',
244
- 'img_path': element.get('image_path'),
245
- 'img_alt': '',
246
- 'img_title': '',
247
- 'img_caption': '',
248
- }
249
- elif type == 'table':
250
- content_node = {
251
- 'type': 'table',
252
- 'img_path': element.get('image_path'),
253
- 'table_latex': element.get('text'),
254
- 'table_title': '',
255
- 'table_caption': '',
256
- 'table_quality': element.get('quality'),
257
- }
258
- content_list.insert(i, content_node)
259
- break
260
- else:
261
- logger.error(
262
- f"Can't find the location of image {element.get('image_path')} in the markdown file, search target is {text}"
263
- )
264
-
265
-
266
- def mk_universal_format(pdf_info_list: list, img_buket_path):
267
- """构造统一格式 https://aicarrier.feishu.cn/wiki/FqmMwcH69iIdCWkkyjvcDwNUnTY."""
268
- content_lst = []
269
- for page_info in pdf_info_list:
270
- page_lst = [] # 一个page内的段落列表
271
- para_blocks = page_info.get('para_blocks')
272
- pymu_raw_blocks = page_info.get('preproc_blocks')
273
-
274
- all_page_images = []
275
- all_page_images.extend(page_info.get('images', []))
276
- all_page_images.extend(page_info.get('image_backup', []))
277
- # all_page_images.extend(page_info.get("tables",[]))
278
- # all_page_images.extend(page_info.get("table_backup",[]) )
279
- all_page_tables = []
280
- all_page_tables.extend(page_info.get('tables', []))
281
-
282
- if not para_blocks or not pymu_raw_blocks: # 只有图片的拼接的场景
283
- for img in all_page_images:
284
- content_node = {
285
- 'type': 'image',
286
- 'img_path': join_path(img_buket_path, img['image_path']),
287
- 'img_alt': '',
288
- 'img_title': '',
289
- 'img_caption': '',
290
- }
291
- page_lst.append(content_node) # TODO 图片顺序
292
- for table in all_page_tables:
293
- content_node = {
294
- 'type': 'table',
295
- 'img_path': join_path(img_buket_path, table['image_path']),
296
- 'table_latex': table.get('text'),
297
- 'table_title': '',
298
- 'table_caption': '',
299
- 'table_quality': table.get('quality'),
300
- }
301
- page_lst.append(content_node) # TODO 图片顺序
302
- else:
303
- for block in para_blocks:
304
- item = block['paras']
305
- for _, p in item.items():
306
- font_type = p[
307
- 'para_font_type'
308
- ] # 对于文本来说,要么是普通文本,要么是个行间公式
309
- if font_type == TYPE_INTERLINE_EQUATION:
310
- content_node = {'type': 'equation', 'latex': p['para_text']}
311
- page_lst.append(content_node)
312
- else:
313
- para_text = p['para_text']
314
- is_title = p['is_para_title']
315
- title_level = p['para_title_level']
316
-
317
- if is_title:
318
- content_node = {
319
- 'type': f'h{title_level}',
320
- 'text': para_text,
321
- }
322
- page_lst.append(content_node)
323
- else:
324
- content_node = {'type': 'text', 'text': para_text}
325
- page_lst.append(content_node)
326
-
327
- content_lst.extend(page_lst)
328
-
329
- """插入图片"""
330
- for img in all_page_images:
331
- insert_img_or_table('image', img, pymu_raw_blocks, content_lst)
332
-
333
- """插入表格"""
334
- for table in all_page_tables:
335
- insert_img_or_table('table', table, pymu_raw_blocks, content_lst)
336
- # end for
337
- return content_lst
338
-
339
-
340
- def insert_img_or_table(type, element, pymu_raw_blocks, content_lst):
341
- element_bbox = element['bbox']
342
- # 先看在哪个block内
343
- for block in pymu_raw_blocks:
344
- bbox = block['bbox']
345
- if (
346
- bbox[0] - 1 <= element_bbox[0] < bbox[2] + 1
347
- and bbox[1] - 1 <= element_bbox[1] < bbox[3] + 1
348
- ): # 确定在这个大的block内,然后进入逐行比较距离
349
- for l in block['lines']: # noqa: E741
350
- line_box = l['bbox']
351
- if (
352
- line_box[0] - 1 <= element_bbox[0] < line_box[2] + 1
353
- and line_box[1] - 1 <= element_bbox[1] < line_box[3] + 1
354
- ): # 在line内的,插入line前面
355
- line_txt = ''.join([s['text'] for s in l['spans']])
356
- __insert_before_para(line_txt, type, element, content_lst)
357
- break
358
- break
359
- else: # 在行与行之间
360
- # 找到图片x0,y0与line的x0,y0最近的line
361
- min_distance = 100000
362
- min_line = None
363
- for l in block['lines']: # noqa: E741
364
- line_box = l['bbox']
365
- distance = math.sqrt(
366
- (line_box[0] - element_bbox[0]) ** 2
367
- + (line_box[1] - element_bbox[1]) ** 2
368
- )
369
- if distance < min_distance:
370
- min_distance = distance
371
- min_line = l
372
- if min_line:
373
- line_txt = ''.join([s['text'] for s in min_line['spans']])
374
- img_h = element_bbox[3] - element_bbox[1]
375
- if min_distance < img_h: # 文字在图片前面
376
- __insert_after_para(line_txt, type, element, content_lst)
377
- else:
378
- __insert_before_para(line_txt, type, element, content_lst)
379
- break
380
- else:
381
- logger.error(
382
- f"Can't find the location of image {element.get('image_path')} in the markdown file #1"
383
- )
384
- else: # 应当在两个block之间
385
- # 找到上方最近的block,如果上方没有就找大下方最近的block
386
- top_txt_block = find_top_nearest_text_bbox(pymu_raw_blocks, element_bbox)
387
- if top_txt_block:
388
- line_txt = ''.join([s['text'] for s in top_txt_block['lines'][-1]['spans']])
389
- __insert_after_para(line_txt, type, element, content_lst)
390
- else:
391
- bottom_txt_block = find_bottom_nearest_text_bbox(
392
- pymu_raw_blocks, element_bbox
393
- )
394
- if bottom_txt_block:
395
- line_txt = ''.join(
396
- [s['text'] for s in bottom_txt_block['lines'][0]['spans']]
397
- )
398
- __insert_before_para(line_txt, type, element, content_lst)
399
- else: # TODO ,图片可能独占一列,这种情况上下是没有图片的
400
- logger.error(
401
- f"Can't find the location of image {element.get('image_path')} in the markdown file #2"
402
- )
403
-
404
-
405
- def mk_mm_markdown(content_list):
406
- """基于同一格式的内容列表,构造markdown,含图片."""
407
- content_md = []
408
- for c in content_list:
409
- content_type = c.get('type')
410
- if content_type == 'text':
411
- content_md.append(c.get('text'))
412
- elif content_type == 'equation':
413
- content = c.get('latex')
414
- if content.startswith('$$') and content.endswith('$$'):
415
- content_md.append(content)
416
- else:
417
- content_md.append(f"\n$$\n{c.get('latex')}\n$$\n")
418
- elif content_type in UNI_FORMAT_TEXT_TYPE:
419
- content_md.append(f"{'#'*int(content_type[1])} {c.get('text')}")
420
- elif content_type == 'image':
421
- content_md.append(f"![]({c.get('img_path')})")
422
- return '\n\n'.join(content_md)
423
-
424
-
425
- def mk_nlp_markdown(content_list):
426
- """基于同一格式的内容列表,构造markdown,不含图片."""
427
- content_md = []
428
- for c in content_list:
429
- content_type = c.get('type')
430
- if content_type == 'text':
431
- content_md.append(c.get('text'))
432
- elif content_type == 'equation':
433
- content_md.append(f"$$\n{c.get('latex')}\n$$")
434
- elif content_type == 'table':
435
- content_md.append(f"$$$\n{c.get('table_latex')}\n$$$")
436
- elif content_type in UNI_FORMAT_TEXT_TYPE:
437
- content_md.append(f"{'#'*int(content_type[1])} {c.get('text')}")
438
- return '\n\n'.join(content_md)
File without changes