magic-pdf 0.10.0__py3-none-any.whl → 0.10.2__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 (76) hide show
  1. magic_pdf/data/data_reader_writer/filebase.py +3 -0
  2. magic_pdf/filter/pdf_meta_scan.py +3 -17
  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_image_tools.py +2 -1
  7. magic_pdf/libs/version.py +1 -1
  8. magic_pdf/model/doc_analyze_by_custom_model.py +2 -2
  9. magic_pdf/model/magic_model.py +0 -30
  10. magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py +3 -28
  11. magic_pdf/model/sub_modules/ocr/paddleocr/ppocr_273_mod.py +3 -3
  12. magic_pdf/para/para_split_v3.py +7 -2
  13. magic_pdf/pdf_parse_union_core_v2.py +97 -124
  14. magic_pdf/pre_proc/construct_page_dict.py +0 -55
  15. magic_pdf/pre_proc/cut_image.py +0 -37
  16. magic_pdf/pre_proc/ocr_detect_all_bboxes.py +5 -178
  17. magic_pdf/pre_proc/ocr_dict_merge.py +1 -224
  18. magic_pdf/pre_proc/ocr_span_list_modify.py +2 -252
  19. magic_pdf/rw/S3ReaderWriter.py +1 -1
  20. {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/METADATA +3 -77
  21. {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/RECORD +25 -76
  22. {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/WHEEL +1 -1
  23. magic_pdf/dict2md/mkcontent.py +0 -438
  24. magic_pdf/layout/__init__.py +0 -0
  25. magic_pdf/layout/bbox_sort.py +0 -681
  26. magic_pdf/layout/layout_det_utils.py +0 -182
  27. magic_pdf/layout/layout_sort.py +0 -921
  28. magic_pdf/layout/layout_spiler_recog.py +0 -101
  29. magic_pdf/layout/mcol_sort.py +0 -336
  30. magic_pdf/libs/calc_span_stats.py +0 -239
  31. magic_pdf/libs/detect_language_from_model.py +0 -21
  32. magic_pdf/libs/nlp_utils.py +0 -203
  33. magic_pdf/libs/textbase.py +0 -33
  34. magic_pdf/libs/vis_utils.py +0 -308
  35. magic_pdf/para/block_continuation_processor.py +0 -562
  36. magic_pdf/para/block_termination_processor.py +0 -480
  37. magic_pdf/para/commons.py +0 -222
  38. magic_pdf/para/denoise.py +0 -246
  39. magic_pdf/para/draw.py +0 -121
  40. magic_pdf/para/exceptions.py +0 -198
  41. magic_pdf/para/layout_match_processor.py +0 -40
  42. magic_pdf/para/para_split.py +0 -807
  43. magic_pdf/para/para_split_v2.py +0 -959
  44. magic_pdf/para/raw_processor.py +0 -207
  45. magic_pdf/para/stats.py +0 -268
  46. magic_pdf/para/title_processor.py +0 -1014
  47. magic_pdf/pdf_parse_union_core.py +0 -345
  48. magic_pdf/post_proc/__init__.py +0 -0
  49. magic_pdf/post_proc/detect_para.py +0 -3472
  50. magic_pdf/post_proc/pdf_post_filter.py +0 -60
  51. magic_pdf/post_proc/remove_footnote.py +0 -153
  52. magic_pdf/pre_proc/citationmarker_remove.py +0 -161
  53. magic_pdf/pre_proc/detect_equation.py +0 -134
  54. magic_pdf/pre_proc/detect_footer_by_model.py +0 -64
  55. magic_pdf/pre_proc/detect_footer_header_by_statistics.py +0 -284
  56. magic_pdf/pre_proc/detect_footnote.py +0 -170
  57. magic_pdf/pre_proc/detect_header.py +0 -64
  58. magic_pdf/pre_proc/detect_images.py +0 -647
  59. magic_pdf/pre_proc/detect_page_number.py +0 -64
  60. magic_pdf/pre_proc/detect_tables.py +0 -62
  61. magic_pdf/pre_proc/equations_replace.py +0 -550
  62. magic_pdf/pre_proc/fix_image.py +0 -244
  63. magic_pdf/pre_proc/fix_table.py +0 -270
  64. magic_pdf/pre_proc/main_text_font.py +0 -23
  65. magic_pdf/pre_proc/ocr_detect_layout.py +0 -133
  66. magic_pdf/pre_proc/pdf_pre_filter.py +0 -78
  67. magic_pdf/pre_proc/post_layout_split.py +0 -0
  68. magic_pdf/pre_proc/remove_colored_strip_bbox.py +0 -101
  69. magic_pdf/pre_proc/remove_footer_header.py +0 -114
  70. magic_pdf/pre_proc/remove_rotate_bbox.py +0 -236
  71. magic_pdf/pre_proc/resolve_bbox_conflict.py +0 -184
  72. magic_pdf/pre_proc/solve_line_alien.py +0 -29
  73. magic_pdf/pre_proc/statistics.py +0 -12
  74. {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/LICENSE.md +0 -0
  75. {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/entry_points.txt +0 -0
  76. {magic_pdf-0.10.0.dist-info → magic_pdf-0.10.2.dist-info}/top_level.txt +0 -0
@@ -1,3472 +0,0 @@
1
- import os
2
- import sys
3
- import json
4
- import re
5
- import math
6
- import unicodedata
7
- from collections import Counter
8
-
9
-
10
- import numpy as np
11
- from termcolor import cprint
12
-
13
-
14
- from magic_pdf.libs.commons import fitz
15
- from magic_pdf.libs.nlp_utils import NLPModels
16
-
17
-
18
- if sys.version_info[0] >= 3:
19
- sys.stdout.reconfigure(encoding="utf-8") # type: ignore
20
-
21
-
22
- def open_pdf(pdf_path):
23
- try:
24
- pdf_document = fitz.open(pdf_path) # type: ignore
25
- return pdf_document
26
- except Exception as e:
27
- print(f"无法打开PDF文件:{pdf_path}。原因是:{e}")
28
- raise e
29
-
30
-
31
- def print_green_on_red(text):
32
- cprint(text, "green", "on_red", attrs=["bold"], end="\n\n")
33
-
34
-
35
- def print_green(text):
36
- print()
37
- cprint(text, "green", attrs=["bold"], end="\n\n")
38
-
39
-
40
- def print_red(text):
41
- print()
42
- cprint(text, "red", attrs=["bold"], end="\n\n")
43
-
44
-
45
- def print_yellow(text):
46
- print()
47
- cprint(text, "yellow", attrs=["bold"], end="\n\n")
48
-
49
-
50
- def safe_get(dict_obj, key, default):
51
- val = dict_obj.get(key)
52
- if val is None:
53
- return default
54
- else:
55
- return val
56
-
57
-
58
- def is_bbox_overlap(bbox1, bbox2):
59
- """
60
- This function checks if bbox1 and bbox2 overlap or not
61
-
62
- Parameters
63
- ----------
64
- bbox1 : list
65
- bbox1
66
- bbox2 : list
67
- bbox2
68
-
69
- Returns
70
- -------
71
- bool
72
- True if bbox1 and bbox2 overlap, else False
73
- """
74
- x0_1, y0_1, x1_1, y1_1 = bbox1
75
- x0_2, y0_2, x1_2, y1_2 = bbox2
76
-
77
- if x0_1 > x1_2 or x0_2 > x1_1:
78
- return False
79
- if y0_1 > y1_2 or y0_2 > y1_1:
80
- return False
81
-
82
- return True
83
-
84
-
85
- def is_in_bbox(bbox1, bbox2):
86
- """
87
- This function checks if bbox1 is in bbox2
88
-
89
- Parameters
90
- ----------
91
- bbox1 : list
92
- bbox1
93
- bbox2 : list
94
- bbox2
95
-
96
- Returns
97
- -------
98
- bool
99
- True if bbox1 is in bbox2, else False
100
- """
101
- x0_1, y0_1, x1_1, y1_1 = bbox1
102
- x0_2, y0_2, x1_2, y1_2 = bbox2
103
-
104
- if x0_1 >= x0_2 and y0_1 >= y0_2 and x1_1 <= x1_2 and y1_1 <= y1_2:
105
- return True
106
- else:
107
- return False
108
-
109
-
110
- def calculate_para_bbox(lines):
111
- """
112
- This function calculates the minimum bbox of the paragraph
113
-
114
- Parameters
115
- ----------
116
- lines : list
117
- lines
118
-
119
- Returns
120
- -------
121
- para_bbox : list
122
- bbox of the paragraph
123
- """
124
- x0 = min(line["bbox"][0] for line in lines)
125
- y0 = min(line["bbox"][1] for line in lines)
126
- x1 = max(line["bbox"][2] for line in lines)
127
- y1 = max(line["bbox"][3] for line in lines)
128
- return [x0, y0, x1, y1]
129
-
130
-
131
- def is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction=2):
132
- """
133
- This function checks if the line is right aligned from its neighbors
134
-
135
- Parameters
136
- ----------
137
- curr_line_bbox : list
138
- bbox of the current line
139
- prev_line_bbox : list
140
- bbox of the previous line
141
- next_line_bbox : list
142
- bbox of the next line
143
- avg_char_width : float
144
- average of char widths
145
- direction : int
146
- 0 for prev, 1 for next, 2 for both
147
-
148
- Returns
149
- -------
150
- bool
151
- True if the line is right aligned from its neighbors, False otherwise.
152
- """
153
- horizontal_ratio = 0.5
154
- horizontal_thres = horizontal_ratio * avg_char_width
155
-
156
- _, _, x1, _ = curr_line_bbox
157
- _, _, prev_x1, _ = prev_line_bbox if prev_line_bbox else (0, 0, 0, 0)
158
- _, _, next_x1, _ = next_line_bbox if next_line_bbox else (0, 0, 0, 0)
159
-
160
- if direction == 0:
161
- return abs(x1 - prev_x1) < horizontal_thres
162
- elif direction == 1:
163
- return abs(x1 - next_x1) < horizontal_thres
164
- elif direction == 2:
165
- return abs(x1 - prev_x1) < horizontal_thres and abs(x1 - next_x1) < horizontal_thres
166
- else:
167
- return False
168
-
169
-
170
- def is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction=2):
171
- """
172
- This function checks if the line is left aligned from its neighbors
173
-
174
- Parameters
175
- ----------
176
- curr_line_bbox : list
177
- bbox of the current line
178
- prev_line_bbox : list
179
- bbox of the previous line
180
- next_line_bbox : list
181
- bbox of the next line
182
- avg_char_width : float
183
- average of char widths
184
- direction : int
185
- 0 for prev, 1 for next, 2 for both
186
-
187
- Returns
188
- -------
189
- bool
190
- True if the line is left aligned from its neighbors, False otherwise.
191
- """
192
- horizontal_ratio = 0.5
193
- horizontal_thres = horizontal_ratio * avg_char_width
194
-
195
- x0, _, _, _ = curr_line_bbox
196
- prev_x0, _, _, _ = prev_line_bbox if prev_line_bbox else (0, 0, 0, 0)
197
- next_x0, _, _, _ = next_line_bbox if next_line_bbox else (0, 0, 0, 0)
198
-
199
- if direction == 0:
200
- return abs(x0 - prev_x0) < horizontal_thres
201
- elif direction == 1:
202
- return abs(x0 - next_x0) < horizontal_thres
203
- elif direction == 2:
204
- return abs(x0 - prev_x0) < horizontal_thres and abs(x0 - next_x0) < horizontal_thres
205
- else:
206
- return False
207
-
208
-
209
- def end_with_punctuation(line_text):
210
- """
211
- This function checks if the line ends with punctuation marks
212
- """
213
-
214
- english_end_puncs = [".", "?", "!"]
215
- chinese_end_puncs = ["。", "?", "!"]
216
- end_puncs = english_end_puncs + chinese_end_puncs
217
-
218
- last_non_space_char = None
219
- for ch in line_text[::-1]:
220
- if not ch.isspace():
221
- last_non_space_char = ch
222
- break
223
-
224
- if last_non_space_char is None:
225
- return False
226
-
227
- return last_non_space_char in end_puncs
228
-
229
-
230
- def is_nested_list(lst):
231
- if isinstance(lst, list):
232
- return any(isinstance(sub, list) for sub in lst)
233
- return False
234
-
235
-
236
- class DenseSingleLineBlockException(Exception):
237
- """
238
- This class defines the exception type for dense single line-block.
239
- """
240
-
241
- def __init__(self, message="DenseSingleLineBlockException"):
242
- self.message = message
243
- super().__init__(self.message)
244
-
245
- def __str__(self):
246
- return f"{self.message}"
247
-
248
- def __repr__(self):
249
- return f"{self.message}"
250
-
251
-
252
- class TitleDetectionException(Exception):
253
- """
254
- This class defines the exception type for title detection.
255
- """
256
-
257
- def __init__(self, message="TitleDetectionException"):
258
- self.message = message
259
- super().__init__(self.message)
260
-
261
- def __str__(self):
262
- return f"{self.message}"
263
-
264
- def __repr__(self):
265
- return f"{self.message}"
266
-
267
-
268
- class TitleLevelException(Exception):
269
- """
270
- This class defines the exception type for title level.
271
- """
272
-
273
- def __init__(self, message="TitleLevelException"):
274
- self.message = message
275
- super().__init__(self.message)
276
-
277
- def __str__(self):
278
- return f"{self.message}"
279
-
280
- def __repr__(self):
281
- return f"{self.message}"
282
-
283
-
284
- class ParaSplitException(Exception):
285
- """
286
- This class defines the exception type for paragraph splitting.
287
- """
288
-
289
- def __init__(self, message="ParaSplitException"):
290
- self.message = message
291
- super().__init__(self.message)
292
-
293
- def __str__(self):
294
- return f"{self.message}"
295
-
296
- def __repr__(self):
297
- return f"{self.message}"
298
-
299
-
300
- class ParaMergeException(Exception):
301
- """
302
- This class defines the exception type for paragraph merging.
303
- """
304
-
305
- def __init__(self, message="ParaMergeException"):
306
- self.message = message
307
- super().__init__(self.message)
308
-
309
- def __str__(self):
310
- return f"{self.message}"
311
-
312
- def __repr__(self):
313
- return f"{self.message}"
314
-
315
-
316
- class DiscardByException:
317
- """
318
- This class discards pdf files by exception
319
- """
320
-
321
- def __init__(self) -> None:
322
- pass
323
-
324
- def discard_by_single_line_block(self, pdf_dic, exception: DenseSingleLineBlockException):
325
- """
326
- This function discards pdf files by single line block exception
327
-
328
- Parameters
329
- ----------
330
- pdf_dic : dict
331
- pdf dictionary
332
- exception : str
333
- exception message
334
-
335
- Returns
336
- -------
337
- error_message : str
338
- """
339
- exception_page_nums = 0
340
- page_num = 0
341
- for page_id, page in pdf_dic.items():
342
- if page_id.startswith("page_"):
343
- page_num += 1
344
- if "preproc_blocks" in page.keys():
345
- preproc_blocks = page["preproc_blocks"]
346
-
347
- all_single_line_blocks = []
348
- for block in preproc_blocks:
349
- if len(block["lines"]) == 1:
350
- all_single_line_blocks.append(block)
351
-
352
- if len(preproc_blocks) > 0 and len(all_single_line_blocks) / len(preproc_blocks) > 0.9:
353
- exception_page_nums += 1
354
-
355
- if page_num == 0:
356
- return None
357
-
358
- if exception_page_nums / page_num > 0.1: # Low ratio means basically, whenever this is the case, it is discarded
359
- return exception.message
360
-
361
- return None
362
-
363
- def discard_by_title_detection(self, pdf_dic, exception: TitleDetectionException):
364
- """
365
- This function discards pdf files by title detection exception
366
-
367
- Parameters
368
- ----------
369
- pdf_dic : dict
370
- pdf dictionary
371
- exception : str
372
- exception message
373
-
374
- Returns
375
- -------
376
- error_message : str
377
- """
378
- # return exception.message
379
- return None
380
-
381
- def discard_by_title_level(self, pdf_dic, exception: TitleLevelException):
382
- """
383
- This function discards pdf files by title level exception
384
-
385
- Parameters
386
- ----------
387
- pdf_dic : dict
388
- pdf dictionary
389
- exception : str
390
- exception message
391
-
392
- Returns
393
- -------
394
- error_message : str
395
- """
396
- # return exception.message
397
- return None
398
-
399
- def discard_by_split_para(self, pdf_dic, exception: ParaSplitException):
400
- """
401
- This function discards pdf files by split para exception
402
-
403
- Parameters
404
- ----------
405
- pdf_dic : dict
406
- pdf dictionary
407
- exception : str
408
- exception message
409
-
410
- Returns
411
- -------
412
- error_message : str
413
- """
414
- # return exception.message
415
- return None
416
-
417
- def discard_by_merge_para(self, pdf_dic, exception: ParaMergeException):
418
- """
419
- This function discards pdf files by merge para exception
420
-
421
- Parameters
422
- ----------
423
- pdf_dic : dict
424
- pdf dictionary
425
- exception : str
426
- exception message
427
-
428
- Returns
429
- -------
430
- error_message : str
431
- """
432
- # return exception.message
433
- return None
434
-
435
-
436
- class LayoutFilterProcessor:
437
- def __init__(self) -> None:
438
- pass
439
-
440
- def batch_process_blocks(self, pdf_dict):
441
- """
442
- This function processes the blocks in batch.
443
-
444
- Parameters
445
- ----------
446
- self : object
447
- The instance of the class.
448
-
449
- pdf_dict : dict
450
- pdf dictionary
451
-
452
- Returns
453
- -------
454
- pdf_dict : dict
455
- pdf dictionary
456
- """
457
- for page_id, blocks in pdf_dict.items():
458
- if page_id.startswith("page_"):
459
- if "layout_bboxes" in blocks.keys() and "para_blocks" in blocks.keys():
460
- layout_bbox_objs = blocks["layout_bboxes"]
461
- if layout_bbox_objs is None:
462
- continue
463
- layout_bboxes = [bbox_obj["layout_bbox"] for bbox_obj in layout_bbox_objs]
464
-
465
- # Enlarge each value of x0, y0, x1, y1 for each layout_bbox to prevent loss of text.
466
- layout_bboxes = [
467
- [math.ceil(x0), math.ceil(y0), math.ceil(x1), math.ceil(y1)] for x0, y0, x1, y1 in layout_bboxes
468
- ]
469
-
470
- para_blocks = blocks["para_blocks"]
471
- if para_blocks is None:
472
- continue
473
-
474
- for lb_bbox in layout_bboxes:
475
- for i, para_block in enumerate(para_blocks):
476
- para_bbox = para_block["bbox"]
477
- para_blocks[i]["in_layout"] = 0
478
- if is_in_bbox(para_bbox, lb_bbox):
479
- para_blocks[i]["in_layout"] = 1
480
-
481
- blocks["para_blocks"] = para_blocks
482
-
483
- return pdf_dict
484
-
485
-
486
- class RawBlockProcessor:
487
- def __init__(self) -> None:
488
- self.y_tolerance = 2
489
- self.pdf_dic = {}
490
-
491
- def __span_flags_decomposer(self, span_flags):
492
- """
493
- Make font flags human readable.
494
-
495
- Parameters
496
- ----------
497
- self : object
498
- The instance of the class.
499
-
500
- span_flags : int
501
- span flags
502
-
503
- Returns
504
- -------
505
- l : dict
506
- decomposed flags
507
- """
508
-
509
- l = {
510
- "is_superscript": False,
511
- "is_italic": False,
512
- "is_serifed": False,
513
- "is_sans_serifed": False,
514
- "is_monospaced": False,
515
- "is_proportional": False,
516
- "is_bold": False,
517
- }
518
-
519
- if span_flags & 2**0:
520
- l["is_superscript"] = True # 表示上标
521
-
522
- if span_flags & 2**1:
523
- l["is_italic"] = True # 表示斜体
524
-
525
- if span_flags & 2**2:
526
- l["is_serifed"] = True # 表示衬线字体
527
- else:
528
- l["is_sans_serifed"] = True # 表示非衬线字体
529
-
530
- if span_flags & 2**3:
531
- l["is_monospaced"] = True # 表示等宽字体
532
- else:
533
- l["is_proportional"] = True # 表示比例字体
534
-
535
- if span_flags & 2**4:
536
- l["is_bold"] = True # 表示粗体
537
-
538
- return l
539
-
540
- def __make_new_lines(self, raw_lines):
541
- """
542
- This function makes new lines.
543
-
544
- Parameters
545
- ----------
546
- self : object
547
- The instance of the class.
548
-
549
- raw_lines : list
550
- raw lines
551
-
552
- Returns
553
- -------
554
- new_lines : list
555
- new lines
556
- """
557
- new_lines = []
558
- new_line = None
559
-
560
- for raw_line in raw_lines:
561
- raw_line_bbox = raw_line["bbox"]
562
- raw_line_spans = raw_line["spans"]
563
- raw_line_text = "".join([span["text"] for span in raw_line_spans])
564
- raw_line_dir = raw_line.get("dir", None)
565
-
566
- decomposed_line_spans = []
567
- for span in raw_line_spans:
568
- raw_flags = span["flags"]
569
- decomposed_flags = self.__span_flags_decomposer(raw_flags)
570
- span["decomposed_flags"] = decomposed_flags
571
- decomposed_line_spans.append(span)
572
-
573
- if new_line is None: # Handle the first line
574
- new_line = {
575
- "bbox": raw_line_bbox,
576
- "text": raw_line_text,
577
- "dir": raw_line_dir if raw_line_dir else (0, 0),
578
- "spans": decomposed_line_spans,
579
- }
580
- else: # Handle the rest lines
581
- if (
582
- abs(raw_line_bbox[1] - new_line["bbox"][1]) <= self.y_tolerance
583
- and abs(raw_line_bbox[3] - new_line["bbox"][3]) <= self.y_tolerance
584
- ):
585
- new_line["bbox"] = (
586
- min(new_line["bbox"][0], raw_line_bbox[0]), # left
587
- new_line["bbox"][1], # top
588
- max(new_line["bbox"][2], raw_line_bbox[2]), # right
589
- raw_line_bbox[3], # bottom
590
- )
591
- new_line["text"] += raw_line_text
592
- new_line["spans"].extend(raw_line_spans)
593
- new_line["dir"] = (
594
- new_line["dir"][0] + raw_line_dir[0],
595
- new_line["dir"][1] + raw_line_dir[1],
596
- )
597
- else:
598
- new_lines.append(new_line)
599
- new_line = {
600
- "bbox": raw_line_bbox,
601
- "text": raw_line_text,
602
- "dir": raw_line_dir if raw_line_dir else (0, 0),
603
- "spans": raw_line_spans,
604
- }
605
- if new_line:
606
- new_lines.append(new_line)
607
-
608
- return new_lines
609
-
610
- def __make_new_block(self, raw_block):
611
- """
612
- This function makes a new block.
613
-
614
- Parameters
615
- ----------
616
- self : object
617
- The instance of the class.
618
- ----------
619
- raw_block : dict
620
- a raw block
621
-
622
- Returns
623
- -------
624
- new_block : dict
625
- """
626
- new_block = {}
627
-
628
- block_id = raw_block["number"]
629
- block_bbox = raw_block["bbox"]
630
- block_text = "".join(span["text"] for line in raw_block["lines"] for span in line["spans"])
631
- raw_lines = raw_block["lines"]
632
- block_lines = self.__make_new_lines(raw_lines)
633
-
634
- new_block["block_id"] = block_id
635
- new_block["bbox"] = block_bbox
636
- new_block["text"] = block_text
637
- new_block["lines"] = block_lines
638
-
639
- return new_block
640
-
641
- def batch_process_blocks(self, pdf_dic):
642
- """
643
- This function processes the blocks in batch.
644
-
645
- Parameters
646
- ----------
647
- self : object
648
- The instance of the class.
649
- ----------
650
- blocks : list
651
- Input block is a list of raw blocks.
652
-
653
- Returns
654
- -------
655
- result_dict : dict
656
- result dictionary
657
- """
658
-
659
- for page_id, blocks in pdf_dic.items():
660
- if page_id.startswith("page_"):
661
- para_blocks = []
662
- if "preproc_blocks" in blocks.keys():
663
- input_blocks = blocks["preproc_blocks"]
664
- for raw_block in input_blocks:
665
- new_block = self.__make_new_block(raw_block)
666
- para_blocks.append(new_block)
667
-
668
- blocks["para_blocks"] = para_blocks
669
-
670
- return pdf_dic
671
-
672
-
673
- class BlockStatisticsCalculator:
674
- """
675
- This class calculates the statistics of the block.
676
- """
677
-
678
- def __init__(self) -> None:
679
- pass
680
-
681
- def __calc_stats_of_new_lines(self, new_lines):
682
- """
683
- This function calculates the paragraph metrics
684
-
685
- Parameters
686
- ----------
687
- combined_lines : list
688
- combined lines
689
-
690
- Returns
691
- -------
692
- X0 : float
693
- Median of x0 values, which represents the left average boundary of the block
694
- X1 : float
695
- Median of x1 values, which represents the right average boundary of the block
696
- avg_char_width : float
697
- Average of char widths, which represents the average char width of the block
698
- avg_char_height : float
699
- Average of line heights, which represents the average line height of the block
700
-
701
- """
702
- x0_values = []
703
- x1_values = []
704
- char_widths = []
705
- char_heights = []
706
-
707
- block_font_types = []
708
- block_font_sizes = []
709
- block_directions = []
710
-
711
- if len(new_lines) > 0:
712
- for i, line in enumerate(new_lines):
713
- line_bbox = line["bbox"]
714
- line_text = line["text"]
715
- line_spans = line["spans"]
716
-
717
- num_chars = len([ch for ch in line_text if not ch.isspace()])
718
-
719
- x0_values.append(line_bbox[0])
720
- x1_values.append(line_bbox[2])
721
-
722
- if num_chars > 0:
723
- char_width = (line_bbox[2] - line_bbox[0]) / num_chars
724
- char_widths.append(char_width)
725
-
726
- for span in line_spans:
727
- block_font_types.append(span["font"])
728
- block_font_sizes.append(span["size"])
729
-
730
- if "dir" in line:
731
- block_directions.append(line["dir"])
732
-
733
- # line_font_types = [span["font"] for span in line_spans]
734
- char_heights = [span["size"] for span in line_spans]
735
-
736
- X0 = np.median(x0_values) if x0_values else 0
737
- X1 = np.median(x1_values) if x1_values else 0
738
- avg_char_width = sum(char_widths) / len(char_widths) if char_widths else 0
739
- avg_char_height = sum(char_heights) / len(char_heights) if char_heights else 0
740
-
741
- # max_freq_font_type = max(set(block_font_types), key=block_font_types.count) if block_font_types else None
742
-
743
- max_span_length = 0
744
- max_span_font_type = None
745
- for line in new_lines:
746
- line_spans = line["spans"]
747
- for span in line_spans:
748
- span_length = span["bbox"][2] - span["bbox"][0]
749
- if span_length > max_span_length:
750
- max_span_length = span_length
751
- max_span_font_type = span["font"]
752
-
753
- max_freq_font_type = max_span_font_type
754
-
755
- avg_font_size = sum(block_font_sizes) / len(block_font_sizes) if block_font_sizes else None
756
-
757
- avg_dir_horizontal = sum([dir[0] for dir in block_directions]) / len(block_directions) if block_directions else 0
758
- avg_dir_vertical = sum([dir[1] for dir in block_directions]) / len(block_directions) if block_directions else 0
759
-
760
- median_font_size = float(np.median(block_font_sizes)) if block_font_sizes else None
761
-
762
- return (
763
- X0,
764
- X1,
765
- avg_char_width,
766
- avg_char_height,
767
- max_freq_font_type,
768
- avg_font_size,
769
- (avg_dir_horizontal, avg_dir_vertical),
770
- median_font_size,
771
- )
772
-
773
- def __make_new_block(self, input_block):
774
- new_block = {}
775
-
776
- raw_lines = input_block["lines"]
777
- stats = self.__calc_stats_of_new_lines(raw_lines)
778
-
779
- block_id = input_block["block_id"]
780
- block_bbox = input_block["bbox"]
781
- block_text = input_block["text"]
782
- block_lines = raw_lines
783
- block_avg_left_boundary = stats[0]
784
- block_avg_right_boundary = stats[1]
785
- block_avg_char_width = stats[2]
786
- block_avg_char_height = stats[3]
787
- block_font_type = stats[4]
788
- block_font_size = stats[5]
789
- block_direction = stats[6]
790
- block_median_font_size = stats[7]
791
-
792
- new_block["block_id"] = block_id
793
- new_block["bbox"] = block_bbox
794
- new_block["text"] = block_text
795
- new_block["dir"] = block_direction
796
- new_block["X0"] = block_avg_left_boundary
797
- new_block["X1"] = block_avg_right_boundary
798
- new_block["avg_char_width"] = block_avg_char_width
799
- new_block["avg_char_height"] = block_avg_char_height
800
- new_block["block_font_type"] = block_font_type
801
- new_block["block_font_size"] = block_font_size
802
- new_block["lines"] = block_lines
803
- new_block["median_font_size"] = block_median_font_size
804
-
805
- return new_block
806
-
807
- def batch_process_blocks(self, pdf_dic):
808
- """
809
- This function processes the blocks in batch.
810
-
811
- Parameters
812
- ----------
813
- self : object
814
- The instance of the class.
815
- ----------
816
- blocks : list
817
- Input block is a list of raw blocks.
818
- Schema can refer to the value of key ""preproc_blocks".
819
-
820
- Returns
821
- -------
822
- result_dict : dict
823
- result dictionary
824
- """
825
-
826
- for page_id, blocks in pdf_dic.items():
827
- if page_id.startswith("page_"):
828
- para_blocks = []
829
- if "para_blocks" in blocks.keys():
830
- input_blocks = blocks["para_blocks"]
831
- for input_block in input_blocks:
832
- new_block = self.__make_new_block(input_block)
833
- para_blocks.append(new_block)
834
-
835
- blocks["para_blocks"] = para_blocks
836
-
837
- return pdf_dic
838
-
839
-
840
- class DocStatisticsCalculator:
841
- """
842
- This class calculates the statistics of the document.
843
- """
844
-
845
- def __init__(self) -> None:
846
- pass
847
-
848
- def calc_stats_of_doc(self, pdf_dict):
849
- """
850
- This function computes the statistics of the document
851
-
852
- Parameters
853
- ----------
854
- result_dict : dict
855
- result dictionary
856
-
857
- Returns
858
- -------
859
- statistics : dict
860
- statistics of the document
861
- """
862
-
863
- total_text_length = 0
864
- total_num_blocks = 0
865
-
866
- for page_id, blocks in pdf_dict.items():
867
- if page_id.startswith("page_"):
868
- if "para_blocks" in blocks.keys():
869
- para_blocks = blocks["para_blocks"]
870
- for para_block in para_blocks:
871
- total_text_length += len(para_block["text"])
872
- total_num_blocks += 1
873
-
874
- avg_text_length = total_text_length / total_num_blocks if total_num_blocks else 0
875
-
876
- font_list = []
877
-
878
- for page_id, blocks in pdf_dict.items():
879
- if page_id.startswith("page_"):
880
- if "para_blocks" in blocks.keys():
881
- input_blocks = blocks["para_blocks"]
882
- for input_block in input_blocks:
883
- block_text_length = len(input_block.get("text", ""))
884
- if block_text_length < avg_text_length * 0.5:
885
- continue
886
- block_font_type = safe_get(input_block, "block_font_type", "")
887
- block_font_size = safe_get(input_block, "block_font_size", 0)
888
- font_list.append((block_font_type, block_font_size))
889
-
890
- font_counter = Counter(font_list)
891
- most_common_font = font_counter.most_common(1)[0] if font_list else (("", 0), 0)
892
- second_most_common_font = font_counter.most_common(2)[1] if len(font_counter) > 1 else (("", 0), 0)
893
-
894
- statistics = {
895
- "num_pages": 0,
896
- "num_blocks": 0,
897
- "num_paras": 0,
898
- "num_titles": 0,
899
- "num_header_blocks": 0,
900
- "num_footer_blocks": 0,
901
- "num_watermark_blocks": 0,
902
- "num_vertical_margin_note_blocks": 0,
903
- "most_common_font_type": most_common_font[0][0],
904
- "most_common_font_size": most_common_font[0][1],
905
- "number_of_most_common_font": most_common_font[1],
906
- "second_most_common_font_type": second_most_common_font[0][0],
907
- "second_most_common_font_size": second_most_common_font[0][1],
908
- "number_of_second_most_common_font": second_most_common_font[1],
909
- "avg_text_length": avg_text_length,
910
- }
911
-
912
- for page_id, blocks in pdf_dict.items():
913
- if page_id.startswith("page_"):
914
- blocks = pdf_dict[page_id]["para_blocks"]
915
- statistics["num_pages"] += 1
916
- for block_id, block_data in enumerate(blocks):
917
- statistics["num_blocks"] += 1
918
-
919
- if "paras" in block_data.keys():
920
- statistics["num_paras"] += len(block_data["paras"])
921
-
922
- for line in block_data["lines"]:
923
- if line.get("is_title", 0):
924
- statistics["num_titles"] += 1
925
-
926
- if block_data.get("is_header", 0):
927
- statistics["num_header_blocks"] += 1
928
- if block_data.get("is_footer", 0):
929
- statistics["num_footer_blocks"] += 1
930
- if block_data.get("is_watermark", 0):
931
- statistics["num_watermark_blocks"] += 1
932
- if block_data.get("is_vertical_margin_note", 0):
933
- statistics["num_vertical_margin_note_blocks"] += 1
934
-
935
- pdf_dict["statistics"] = statistics
936
-
937
- return pdf_dict
938
-
939
-
940
- class TitleProcessor:
941
- """
942
- This class processes the title.
943
- """
944
-
945
- def __init__(self, *doc_statistics) -> None:
946
- if len(doc_statistics) > 0:
947
- self.doc_statistics = doc_statistics[0]
948
-
949
- self.nlp_model = NLPModels()
950
- self.MAX_TITLE_LEVEL = 3
951
- self.numbered_title_pattern = r"""
952
- ^ # 行首
953
- ( # 开始捕获组
954
- [\(\(]\d+[\)\)] # 括号内数字,支持中文和英文括号,例如:(1) 或 (1)
955
- |\d+[\)\)]\s # 数字后跟右括号和空格,支持中文和英文括号,例如:2) 或 2)
956
- |[\(\(][A-Z][\)\)] # 括号内大写字母,支持中文和英文括号,例如:(A) 或 (A)
957
- |[A-Z][\)\)]\s # 大写字母后跟右括号和空格,例如:A) 或 A)
958
- |[\(\(][IVXLCDM]+[\)\)] # 括号内罗马数字,支持中文和英文括号,例如:(I) 或 (I)
959
- |[IVXLCDM]+[\)\)]\s # 罗马数字后跟右括号和空格,例如:I) 或 I)
960
- |\d+(\.\d+)*\s # 数字或复合数字编号后跟空格,例如:1. 或 3.2.1
961
- |[一二三四五六七八九十百千]+[、\s] # 中文序号后跟顿号和空格,例如:一、
962
- |[\(|\(][一二三四五六七八九十百千]+[\)|\)]\s* # 中文括号内中文序号后跟空格,例如:(一)
963
- |[A-Z]\.\d+(\.\d+)?\s # 大写字母后跟点和数字,例如:A.1 或 A.1.1
964
- |[\(\(][a-z][\)\)] # 括号内小写字母,支持中文和英文括号,例如:(a) 或 (a)
965
- |[a-z]\)\s # 小写字母后跟右括号和空格,例如:a)
966
- |[A-Z]-\s # 大写字母后跟短横线和空格,例如:A-
967
- |\w+:\s # 英文序号词后跟冒号和空格,例如:First:
968
- |第[一二三四五六七八九十百千]+[章节部分条款]\s # 以“第”开头的中文标题后跟空格
969
- |[IVXLCDM]+\. # 罗马数字后跟点,例如:I.
970
- |\d+\.\s # 单个数字后跟点和空格,例如:1.
971
- ) # 结束捕获组
972
- .+ # 标题的其余部分
973
- """
974
-
975
- def _is_potential_title(
976
- self,
977
- curr_line,
978
- prev_line,
979
- prev_line_is_title,
980
- next_line,
981
- avg_char_width,
982
- avg_char_height,
983
- median_font_size,
984
- ):
985
- """
986
- This function checks if the line is a potential title.
987
-
988
- Parameters
989
- ----------
990
- curr_line : dict
991
- current line
992
- prev_line : dict
993
- previous line
994
- next_line : dict
995
- next line
996
- avg_char_width : float
997
- average of char widths
998
- avg_char_height : float
999
- average of line heights
1000
-
1001
- Returns
1002
- -------
1003
- bool
1004
- True if the line is a potential title, False otherwise.
1005
- """
1006
-
1007
- def __is_line_centered(line_bbox, page_bbox, avg_char_width):
1008
- """
1009
- This function checks if the line is centered on the page
1010
-
1011
- Parameters
1012
- ----------
1013
- line_bbox : list
1014
- bbox of the line
1015
- page_bbox : list
1016
- bbox of the page
1017
- avg_char_width : float
1018
- average of char widths
1019
-
1020
- Returns
1021
- -------
1022
- bool
1023
- True if the line is centered on the page, False otherwise.
1024
- """
1025
- horizontal_ratio = 0.5
1026
- horizontal_thres = horizontal_ratio * avg_char_width
1027
-
1028
- x0, _, x1, _ = line_bbox
1029
- _, _, page_x1, _ = page_bbox
1030
-
1031
- return abs((x0 + x1) / 2 - page_x1 / 2) < horizontal_thres
1032
-
1033
- def __is_bold_font_line(line):
1034
- """
1035
- Check if a line contains any bold font style.
1036
- """
1037
-
1038
- def _is_bold_span(span):
1039
- # if span text is empty or only contains space, return False
1040
- if not span["text"].strip():
1041
- return False
1042
-
1043
- return bool(span["flags"] & 2**4) # Check if the font is bold
1044
-
1045
- for span in line["spans"]:
1046
- if not _is_bold_span(span):
1047
- return False
1048
-
1049
- return True
1050
-
1051
- def __is_italic_font_line(line):
1052
- """
1053
- Check if a line contains any italic font style.
1054
- """
1055
-
1056
- def __is_italic_span(span):
1057
- return bool(span["flags"] & 2**1) # Check if the font is italic
1058
-
1059
- for span in line["spans"]:
1060
- if not __is_italic_span(span):
1061
- return False
1062
-
1063
- return True
1064
-
1065
- def __is_punctuation_heavy(line_text):
1066
- """
1067
- Check if the line contains a high ratio of punctuation marks, which may indicate
1068
- that the line is not a title.
1069
-
1070
- Parameters:
1071
- line_text (str): Text of the line.
1072
-
1073
- Returns:
1074
- bool: True if the line is heavy with punctuation, False otherwise.
1075
- """
1076
- # Pattern for common title format like "X.Y. Title"
1077
- pattern = r"\b\d+\.\d+\..*\b"
1078
-
1079
- # If the line matches the title format, return False
1080
- if re.match(pattern, line_text.strip()):
1081
- return False
1082
-
1083
- # Find all punctuation marks in the line
1084
- punctuation_marks = re.findall(r"[^\w\s]", line_text)
1085
- number_of_punctuation_marks = len(punctuation_marks)
1086
-
1087
- text_length = len(line_text)
1088
-
1089
- if text_length == 0:
1090
- return False
1091
-
1092
- punctuation_ratio = number_of_punctuation_marks / text_length
1093
- if punctuation_ratio >= 0.1:
1094
- return True
1095
-
1096
- return False
1097
-
1098
- def __has_mixed_font_styles(spans, strict_mode=False):
1099
- """
1100
- This function checks if the line has mixed font styles, the strict mode will compare the font types
1101
-
1102
- Parameters
1103
- ----------
1104
- spans : list
1105
- spans of the line
1106
- strict_mode : bool
1107
- True for strict mode, the font types will be fully compared
1108
- False for non-strict mode, the font types will be compared by the most longest common prefix
1109
-
1110
- Returns
1111
- -------
1112
- bool
1113
- True if the line has mixed font styles, False otherwise.
1114
- """
1115
- if strict_mode:
1116
- font_styles = set()
1117
- for span in spans:
1118
- font_style = span["font"].lower()
1119
- font_styles.add(font_style)
1120
-
1121
- return len(font_styles) > 1
1122
-
1123
- else: # non-strict mode
1124
- font_styles = []
1125
- for span in spans:
1126
- font_style = span["font"].lower()
1127
- font_styles.append(font_style)
1128
-
1129
- if len(font_styles) > 1:
1130
- longest_common_prefix = os.path.commonprefix(font_styles)
1131
- if len(longest_common_prefix) > 0:
1132
- return False
1133
- else:
1134
- return True
1135
- else:
1136
- return False
1137
-
1138
- def __is_different_font_type_from_neighbors(curr_line_font_type, prev_line_font_type, next_line_font_type):
1139
- """
1140
- This function checks if the current line has a different font type from the previous and next lines
1141
-
1142
- Parameters
1143
- ----------
1144
- curr_line_font_type : str
1145
- font type of the current line
1146
- prev_line_font_type : str
1147
- font type of the previous line
1148
- next_line_font_type : str
1149
- font type of the next line
1150
-
1151
- Returns
1152
- -------
1153
- bool
1154
- True if the current line has a different font type from the previous and next lines, False otherwise.
1155
- """
1156
- return all(
1157
- curr_line_font_type != other_font_type.lower()
1158
- for other_font_type in [prev_line_font_type, next_line_font_type]
1159
- if other_font_type is not None
1160
- )
1161
-
1162
- def __is_larger_font_size_from_neighbors(curr_line_font_size, prev_line_font_size, next_line_font_size):
1163
- """
1164
- This function checks if the current line has a larger font size than the previous and next lines
1165
-
1166
- Parameters
1167
- ----------
1168
- curr_line_font_size : float
1169
- font size of the current line
1170
- prev_line_font_size : float
1171
- font size of the previous line
1172
- next_line_font_size : float
1173
- font size of the next line
1174
-
1175
- Returns
1176
- -------
1177
- bool
1178
- True if the current line has a larger font size than the previous and next lines, False otherwise.
1179
- """
1180
- return all(
1181
- curr_line_font_size > other_font_size * 1.2
1182
- for other_font_size in [prev_line_font_size, next_line_font_size]
1183
- if other_font_size is not None
1184
- )
1185
-
1186
- def __is_similar_to_pre_line(curr_line_font_type, prev_line_font_type, curr_line_font_size, prev_line_font_size):
1187
- """
1188
- This function checks if the current line is similar to the previous line
1189
-
1190
- Parameters
1191
- ----------
1192
- curr_line : dict
1193
- current line
1194
- prev_line : dict
1195
- previous line
1196
-
1197
- Returns
1198
- -------
1199
- bool
1200
- True if the current line is similar to the previous line, False otherwise.
1201
- """
1202
-
1203
- if curr_line_font_type == prev_line_font_type and curr_line_font_size == prev_line_font_size:
1204
- return True
1205
- else:
1206
- return False
1207
-
1208
- def __is_same_font_type_of_docAvg(curr_line_font_type):
1209
- """
1210
- This function checks if the current line has the same font type as the document average font type
1211
-
1212
- Parameters
1213
- ----------
1214
- curr_line_font_type : str
1215
- font type of the current line
1216
-
1217
- Returns
1218
- -------
1219
- bool
1220
- True if the current line has the same font type as the document average font type, False otherwise.
1221
- """
1222
- doc_most_common_font_type = safe_get(self.doc_statistics, "most_common_font_type", "").lower()
1223
- doc_second_most_common_font_type = safe_get(self.doc_statistics, "second_most_common_font_type", "").lower()
1224
-
1225
- return curr_line_font_type.lower() in [doc_most_common_font_type, doc_second_most_common_font_type]
1226
-
1227
- def __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio: float = 1):
1228
- """
1229
- This function checks if the current line has a large enough font size
1230
-
1231
- Parameters
1232
- ----------
1233
- curr_line_font_size : float
1234
- font size of the current line
1235
- ratio : float
1236
- ratio of the current line font size to the document average font size
1237
-
1238
- Returns
1239
- -------
1240
- bool
1241
- True if the current line has a large enough font size, False otherwise.
1242
- """
1243
- doc_most_common_font_size = safe_get(self.doc_statistics, "most_common_font_size", 0)
1244
- doc_second_most_common_font_size = safe_get(self.doc_statistics, "second_most_common_font_size", 0)
1245
- doc_avg_font_size = min(doc_most_common_font_size, doc_second_most_common_font_size)
1246
-
1247
- return curr_line_font_size >= doc_avg_font_size * ratio
1248
-
1249
- def __is_sufficient_spacing_above_and_below(
1250
- curr_line_bbox,
1251
- prev_line_bbox,
1252
- next_line_bbox,
1253
- avg_char_height,
1254
- median_font_size,
1255
- ):
1256
- """
1257
- This function checks if the current line has sufficient spacing above and below
1258
-
1259
- Parameters
1260
- ----------
1261
- curr_line_bbox : list
1262
- bbox of the current line
1263
- prev_line_bbox : list
1264
- bbox of the previous line
1265
- next_line_bbox : list
1266
- bbox of the next line
1267
- avg_char_width : float
1268
- average of char widths
1269
- avg_char_height : float
1270
- average of line heights
1271
-
1272
- Returns
1273
- -------
1274
- bool
1275
- True if the current line has sufficient spacing above and below, False otherwise.
1276
- """
1277
- vertical_ratio = 1.25
1278
- vertical_thres = vertical_ratio * median_font_size
1279
-
1280
- _, y0, _, y1 = curr_line_bbox
1281
-
1282
- sufficient_spacing_above = False
1283
- if prev_line_bbox:
1284
- vertical_spacing_above = min(y0 - prev_line_bbox[1], y1 - prev_line_bbox[3])
1285
- sufficient_spacing_above = vertical_spacing_above > vertical_thres
1286
- else:
1287
- sufficient_spacing_above = True
1288
-
1289
- sufficient_spacing_below = False
1290
- if next_line_bbox:
1291
- vertical_spacing_below = min(next_line_bbox[1] - y0, next_line_bbox[3] - y1)
1292
- sufficient_spacing_below = vertical_spacing_below > vertical_thres
1293
- else:
1294
- sufficient_spacing_below = True
1295
-
1296
- return (sufficient_spacing_above, sufficient_spacing_below)
1297
-
1298
- def __is_word_list_line_by_rules(curr_line_text):
1299
- """
1300
- This function checks if the current line is a word list
1301
-
1302
- Parameters
1303
- ----------
1304
- curr_line_text : str
1305
- text of the current line
1306
-
1307
- Returns
1308
- -------
1309
- bool
1310
- True if the current line is a name list, False otherwise.
1311
- """
1312
- # name_list_pattern = r"([a-zA-Z][a-zA-Z\s]{0,20}[a-zA-Z]|[\u4e00-\u9fa5·]{2,16})(?=[,,;;\s]|$)"
1313
- name_list_pattern = r"(?<![\u4e00-\u9fa5])([A-Z][a-z]{0,19}\s[A-Z][a-z]{0,19}|[\u4e00-\u9fa5]{2,6})(?=[,,;;\s]|$)"
1314
-
1315
- compiled_pattern = re.compile(name_list_pattern)
1316
-
1317
- if compiled_pattern.search(curr_line_text):
1318
- return True
1319
- else:
1320
- return False
1321
-
1322
- def __get_text_catgr_by_nlp(curr_line_text):
1323
- """
1324
- This function checks if the current line is a name list using nlp model, such as spacy
1325
-
1326
- Parameters
1327
- ----------
1328
- curr_line_text : str
1329
- text of the current line
1330
-
1331
- Returns
1332
- -------
1333
- bool
1334
- True if the current line is a name list, False otherwise.
1335
- """
1336
-
1337
- result = self.nlp_model.detect_entity_catgr_using_nlp(curr_line_text)
1338
-
1339
- return result
1340
-
1341
- def __is_numbered_title(curr_line_text):
1342
- """
1343
- This function checks if the current line is a numbered list
1344
-
1345
- Parameters
1346
- ----------
1347
- curr_line_text : str
1348
- text of the current line
1349
-
1350
- Returns
1351
- -------
1352
- bool
1353
- True if the current line is a numbered list, False otherwise.
1354
- """
1355
-
1356
- compiled_pattern = re.compile(self.numbered_title_pattern, re.VERBOSE)
1357
-
1358
- if compiled_pattern.search(curr_line_text):
1359
- return True
1360
- else:
1361
- return False
1362
-
1363
- def __is_end_with_ending_puncs(line_text):
1364
- """
1365
- This function checks if the current line ends with a ending punctuation mark
1366
-
1367
- Parameters
1368
- ----------
1369
- line_text : str
1370
- text of the current line
1371
-
1372
- Returns
1373
- -------
1374
- bool
1375
- True if the current line ends with a punctuation mark, False otherwise.
1376
- """
1377
- end_puncs = [".", "?", "!", "。", "?", "!", "…"]
1378
-
1379
- line_text = line_text.rstrip()
1380
- if line_text[-1] in end_puncs:
1381
- return True
1382
-
1383
- return False
1384
-
1385
- def __contains_only_no_meaning_symbols(line_text):
1386
- """
1387
- This function checks if the current line contains only symbols that have no meaning, if so, it is not a title.
1388
- Situation contains:
1389
- 1. Only have punctuation marks
1390
- 2. Only have other non-meaning symbols
1391
-
1392
- Parameters
1393
- ----------
1394
- line_text : str
1395
- text of the current line
1396
-
1397
- Returns
1398
- -------
1399
- bool
1400
- True if the current line contains only symbols that have no meaning, False otherwise.
1401
- """
1402
-
1403
- punctuation_marks = re.findall(r"[^\w\s]", line_text) # find all punctuation marks
1404
- number_of_punctuation_marks = len(punctuation_marks)
1405
-
1406
- text_length = len(line_text)
1407
-
1408
- if text_length == 0:
1409
- return False
1410
-
1411
- punctuation_ratio = number_of_punctuation_marks / text_length
1412
- if punctuation_ratio >= 0.9:
1413
- return True
1414
-
1415
- return False
1416
-
1417
- def __is_equation(line_text):
1418
- """
1419
- This function checks if the current line is an equation.
1420
-
1421
- Parameters
1422
- ----------
1423
- line_text : str
1424
-
1425
- Returns
1426
- -------
1427
- bool
1428
- True if the current line is an equation, False otherwise.
1429
- """
1430
- equation_reg = r"\$.*?\\overline.*?\$" # to match interline equations
1431
-
1432
- if re.search(equation_reg, line_text):
1433
- return True
1434
- else:
1435
- return False
1436
-
1437
- def __is_title_by_len(text, max_length=200):
1438
- """
1439
- This function checks if the current line is a title by length.
1440
-
1441
- Parameters
1442
- ----------
1443
- text : str
1444
- text of the current line
1445
-
1446
- max_length : int
1447
- max length of the title
1448
-
1449
- Returns
1450
- -------
1451
- bool
1452
- True if the current line is a title, False otherwise.
1453
-
1454
- """
1455
- text = text.strip()
1456
- return len(text) <= max_length
1457
-
1458
- def __compute_line_font_type_and_size(curr_line):
1459
- """
1460
- This function computes the font type and font size of the line.
1461
-
1462
- Parameters
1463
- ----------
1464
- line : dict
1465
- line
1466
-
1467
- Returns
1468
- -------
1469
- font_type : str
1470
- font type of the line
1471
- font_size : float
1472
- font size of the line
1473
- """
1474
- spans = curr_line["spans"]
1475
- max_accumulated_length = 0
1476
- max_span_font_size = curr_line["spans"][0]["size"] # default value, float type
1477
- max_span_font_type = curr_line["spans"][0]["font"].lower() # default value, string type
1478
- for span in spans:
1479
- if span["text"].isspace():
1480
- continue
1481
- span_length = span["bbox"][2] - span["bbox"][0]
1482
- if span_length > max_accumulated_length:
1483
- max_accumulated_length = span_length
1484
- max_span_font_size = span["size"]
1485
- max_span_font_type = span["font"].lower()
1486
-
1487
- return max_span_font_type, max_span_font_size
1488
-
1489
- def __is_a_consistent_sub_title(pre_line, curr_line):
1490
- """
1491
- This function checks if the current line is a consistent sub title.
1492
-
1493
- Parameters
1494
- ----------
1495
- pre_line : dict
1496
- previous line
1497
- curr_line : dict
1498
- current line
1499
-
1500
- Returns
1501
- -------
1502
- bool
1503
- True if the current line is a consistent sub title, False otherwise.
1504
- """
1505
- if pre_line is None:
1506
- return False
1507
-
1508
- start_letter_of_pre_line = pre_line["text"][0]
1509
- start_letter_of_curr_line = curr_line["text"][0]
1510
-
1511
- has_same_prefix_digit = (
1512
- start_letter_of_pre_line.isdigit()
1513
- and start_letter_of_curr_line.isdigit()
1514
- and start_letter_of_pre_line == start_letter_of_curr_line
1515
- )
1516
-
1517
- # prefix text of curr_line satisfies the following title format: x.x
1518
- prefix_text_pattern = r"^\d+\.\d+"
1519
- has_subtitle_format = re.match(prefix_text_pattern, curr_line["text"])
1520
-
1521
- if has_same_prefix_digit or has_subtitle_format:
1522
- return True
1523
-
1524
- """
1525
- Title detecting main Process.
1526
- """
1527
-
1528
- """
1529
- Basic features about the current line.
1530
- """
1531
- curr_line_bbox = curr_line["bbox"]
1532
- curr_line_text = curr_line["text"]
1533
- curr_line_font_type, curr_line_font_size = __compute_line_font_type_and_size(curr_line)
1534
-
1535
- if len(curr_line_text.strip()) == 0: # skip empty lines
1536
- return False, False
1537
-
1538
- prev_line_bbox = prev_line["bbox"] if prev_line else None
1539
- if prev_line:
1540
- prev_line_font_type, prev_line_font_size = __compute_line_font_type_and_size(prev_line)
1541
- else:
1542
- prev_line_font_type, prev_line_font_size = None, None
1543
-
1544
- next_line_bbox = next_line["bbox"] if next_line else None
1545
- if next_line:
1546
- next_line_font_type, next_line_font_size = __compute_line_font_type_and_size(next_line)
1547
- else:
1548
- next_line_font_type, next_line_font_size = None, None
1549
-
1550
- """
1551
- Aggregated features about the current line.
1552
- """
1553
- is_italc_font = __is_italic_font_line(curr_line)
1554
- is_bold_font = __is_bold_font_line(curr_line)
1555
-
1556
- is_font_size_little_less_than_doc_avg = __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio=0.8)
1557
- is_font_size_not_less_than_doc_avg = __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio=1)
1558
- is_much_larger_font_than_doc_avg = __is_font_size_not_less_than_docAvg(curr_line_font_size, ratio=1.6)
1559
-
1560
- is_not_same_font_type_of_docAvg = not __is_same_font_type_of_docAvg(curr_line_font_type)
1561
-
1562
- is_potential_title_font = is_bold_font or is_font_size_not_less_than_doc_avg or is_not_same_font_type_of_docAvg
1563
-
1564
- is_mix_font_styles_strict = __has_mixed_font_styles(curr_line["spans"], strict_mode=True)
1565
- is_mix_font_styles_loose = __has_mixed_font_styles(curr_line["spans"], strict_mode=False)
1566
-
1567
- is_punctuation_heavy = __is_punctuation_heavy(curr_line_text)
1568
-
1569
- is_word_list_line_by_rules = __is_word_list_line_by_rules(curr_line_text)
1570
- is_person_or_org_list_line_by_nlp = __get_text_catgr_by_nlp(curr_line_text) in ["PERSON", "GPE", "ORG"]
1571
-
1572
- is_font_size_larger_than_neighbors = __is_larger_font_size_from_neighbors(
1573
- curr_line_font_size, prev_line_font_size, next_line_font_size
1574
- )
1575
-
1576
- is_font_type_diff_from_neighbors = __is_different_font_type_from_neighbors(
1577
- curr_line_font_type, prev_line_font_type, next_line_font_type
1578
- )
1579
-
1580
- has_sufficient_spaces_above, has_sufficient_spaces_below = __is_sufficient_spacing_above_and_below(
1581
- curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_height, median_font_size
1582
- )
1583
-
1584
- is_similar_to_pre_line = __is_similar_to_pre_line(
1585
- curr_line_font_type, prev_line_font_type, curr_line_font_size, prev_line_font_size
1586
- )
1587
-
1588
- is_consis_sub_title = __is_a_consistent_sub_title(prev_line, curr_line)
1589
-
1590
- """
1591
- Further aggregated features about the current line.
1592
-
1593
- Attention:
1594
- Features that start with __ are for internal use.
1595
- """
1596
-
1597
- __is_line_left_aligned_from_neighbors = is_line_left_aligned_from_neighbors(
1598
- curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width
1599
- )
1600
- __is_font_diff_from_neighbors = is_font_size_larger_than_neighbors or is_font_type_diff_from_neighbors
1601
- is_a_left_inline_title = (
1602
- is_mix_font_styles_strict and __is_line_left_aligned_from_neighbors and __is_font_diff_from_neighbors
1603
- )
1604
-
1605
- is_title_by_check_prev_line = prev_line is None and has_sufficient_spaces_above and is_potential_title_font
1606
- is_title_by_check_next_line = next_line is None and has_sufficient_spaces_below and is_potential_title_font
1607
-
1608
- is_title_by_check_pre_and_next_line = (
1609
- (prev_line is not None or next_line is not None)
1610
- and has_sufficient_spaces_above
1611
- and has_sufficient_spaces_below
1612
- and is_potential_title_font
1613
- )
1614
-
1615
- is_numbered_title = __is_numbered_title(curr_line_text) and (
1616
- (has_sufficient_spaces_above or prev_line is None) and (has_sufficient_spaces_below or next_line is None)
1617
- )
1618
-
1619
- is_not_end_with_ending_puncs = not __is_end_with_ending_puncs(curr_line_text)
1620
-
1621
- is_not_only_no_meaning_symbols = not __contains_only_no_meaning_symbols(curr_line_text)
1622
-
1623
- is_equation = __is_equation(curr_line_text)
1624
-
1625
- is_title_by_len = __is_title_by_len(curr_line_text)
1626
-
1627
- """
1628
- Decide if the line is a title.
1629
- """
1630
-
1631
- is_title = (
1632
- is_not_end_with_ending_puncs # not end with ending punctuation marks
1633
- and is_not_only_no_meaning_symbols # not only have no meaning symbols
1634
- and is_title_by_len # is a title by length, default max length is 200
1635
- and not is_equation # an interline equation should never be a title
1636
- and is_potential_title_font # is a potential title font, which is bold or larger than the document average font size or not the same font type as the document average font type
1637
- and (
1638
- (is_not_same_font_type_of_docAvg and is_font_size_not_less_than_doc_avg)
1639
- or (is_bold_font and is_much_larger_font_than_doc_avg and is_not_same_font_type_of_docAvg)
1640
- or (
1641
- is_much_larger_font_than_doc_avg
1642
- and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
1643
- )
1644
- or (
1645
- is_font_size_little_less_than_doc_avg
1646
- and is_bold_font
1647
- and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
1648
- )
1649
- ) # Consider the following situations: bold font, much larger font than doc avg, not same font type as doc avg, sufficient spacing above and below
1650
- and (
1651
- (
1652
- not is_person_or_org_list_line_by_nlp
1653
- and (
1654
- is_much_larger_font_than_doc_avg
1655
- or (is_not_same_font_type_of_docAvg and is_font_size_not_less_than_doc_avg)
1656
- )
1657
- )
1658
- or (
1659
- not (is_word_list_line_by_rules and is_person_or_org_list_line_by_nlp)
1660
- and not is_a_left_inline_title
1661
- and not is_punctuation_heavy
1662
- and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
1663
- )
1664
- or (
1665
- is_person_or_org_list_line_by_nlp
1666
- and (is_bold_font and is_much_larger_font_than_doc_avg and is_not_same_font_type_of_docAvg)
1667
- and (is_bold_font and is_much_larger_font_than_doc_avg and is_not_same_font_type_of_docAvg)
1668
- )
1669
- or (is_numbered_title and not is_a_left_inline_title)
1670
- ) # Exclude the following situations: person/org list
1671
- )
1672
- # ) or (prev_line_is_title and is_consis_sub_title)
1673
-
1674
- is_name_or_org_list_to_be_removed = (
1675
- (is_person_or_org_list_line_by_nlp)
1676
- and is_punctuation_heavy
1677
- and (is_title_by_check_prev_line or is_title_by_check_next_line or is_title_by_check_pre_and_next_line)
1678
- ) and not is_title
1679
-
1680
- if is_name_or_org_list_to_be_removed:
1681
- is_author_or_org_list = True
1682
- else:
1683
- is_author_or_org_list = False
1684
-
1685
- # return is_title, is_author_or_org_list
1686
-
1687
- """
1688
- # print reason why the line is a title
1689
- if is_title:
1690
- print_green("This line is a title.")
1691
- print_green("↓" * 10)
1692
- print()
1693
- print("curr_line_text: ", curr_line_text)
1694
- print()
1695
-
1696
- # print reason why the line is not a title
1697
- line_text = curr_line_text.strip()
1698
- test_text = "Career/Personal Life"
1699
- text_content_condition = line_text == test_text
1700
-
1701
- if not is_title and text_content_condition: # Print specific line
1702
- # if not is_title: # Print each line
1703
- print_red("This line is not a title.")
1704
- print_red("↓" * 10)
1705
-
1706
- print()
1707
- print("curr_line_text: ", curr_line_text)
1708
- print()
1709
-
1710
- if is_not_end_with_ending_puncs:
1711
- print_green(f"is_not_end_with_ending_puncs")
1712
- else:
1713
- print_red(f"is_end_with_ending_puncs")
1714
-
1715
- if is_not_only_no_meaning_symbols:
1716
- print_green(f"is_not_only_no_meaning_symbols")
1717
- else:
1718
- print_red(f"is_only_no_meaning_symbols")
1719
-
1720
- if is_title_by_len:
1721
- print_green(f"is_title_by_len: {is_title_by_len}")
1722
- else:
1723
- print_red(f"is_not_title_by_len: {is_title_by_len}")
1724
-
1725
- if is_equation:
1726
- print_red(f"is_equation")
1727
- else:
1728
- print_green(f"is_not_equation")
1729
-
1730
- if is_potential_title_font:
1731
- print_green(f"is_potential_title_font")
1732
- else:
1733
- print_red(f"is_not_potential_title_font")
1734
-
1735
- if is_punctuation_heavy:
1736
- print_red("is_punctuation_heavy")
1737
- else:
1738
- print_green("is_not_punctuation_heavy")
1739
-
1740
- if is_bold_font:
1741
- print_green(f"is_bold_font")
1742
- else:
1743
- print_red(f"is_not_bold_font")
1744
-
1745
- if is_font_size_not_less_than_doc_avg:
1746
- print_green(f"is_larger_font_than_doc_avg")
1747
- else:
1748
- print_red(f"is_not_larger_font_than_doc_avg")
1749
-
1750
- if is_much_larger_font_than_doc_avg:
1751
- print_green(f"is_much_larger_font_than_doc_avg")
1752
- else:
1753
- print_red(f"is_not_much_larger_font_than_doc_avg")
1754
-
1755
- if is_not_same_font_type_of_docAvg:
1756
- print_green(f"is_not_same_font_type_of_docAvg")
1757
- else:
1758
- print_red(f"is_same_font_type_of_docAvg")
1759
-
1760
- if is_word_list_line_by_rules:
1761
- print_red("is_word_list_line_by_rules")
1762
- else:
1763
- print_green("is_not_name_list_by_rules")
1764
-
1765
- if is_person_or_org_list_line_by_nlp:
1766
- print_red("is_person_or_org_list_line_by_nlp")
1767
- else:
1768
- print_green("is_not_person_or_org_list_line_by_nlp")
1769
-
1770
- if not is_numbered_title:
1771
- print_red("is_not_numbered_title")
1772
- else:
1773
- print_green("is_numbered_title")
1774
-
1775
- if is_a_left_inline_title:
1776
- print_red("is_a_left_inline_title")
1777
- else:
1778
- print_green("is_not_a_left_inline_title")
1779
-
1780
- if not is_title_by_check_prev_line:
1781
- print_red("is_not_title_by_check_prev_line")
1782
- else:
1783
- print_green("is_title_by_check_prev_line")
1784
-
1785
- if not is_title_by_check_next_line:
1786
- print_red("is_not_title_by_check_next_line")
1787
- else:
1788
- print_green("is_title_by_check_next_line")
1789
-
1790
- if not is_title_by_check_pre_and_next_line:
1791
- print_red("is_not_title_by_check_pre_and_next_line")
1792
- else:
1793
- print_green("is_title_by_check_pre_and_next_line")
1794
-
1795
- # print_green("Common features:")
1796
- # print_green("↓" * 10)
1797
-
1798
- # print(f" curr_line_font_type: {curr_line_font_type}")
1799
- # print(f" curr_line_font_size: {curr_line_font_size}")
1800
- # print()
1801
-
1802
- """
1803
-
1804
- return is_title, is_author_or_org_list
1805
-
1806
- def _detect_title(self, input_block):
1807
- """
1808
- Use the functions 'is_potential_title' to detect titles of each paragraph block.
1809
- If a line is a title, then the value of key 'is_title' of the line will be set to True.
1810
- """
1811
-
1812
- raw_lines = input_block["lines"]
1813
-
1814
- prev_line_is_title_flag = False
1815
-
1816
- for i, curr_line in enumerate(raw_lines):
1817
- prev_line = raw_lines[i - 1] if i > 0 else None
1818
- next_line = raw_lines[i + 1] if i < len(raw_lines) - 1 else None
1819
-
1820
- blk_avg_char_width = input_block["avg_char_width"]
1821
- blk_avg_char_height = input_block["avg_char_height"]
1822
- blk_media_font_size = input_block["median_font_size"]
1823
-
1824
- is_title, is_author_or_org_list = self._is_potential_title(
1825
- curr_line,
1826
- prev_line,
1827
- prev_line_is_title_flag,
1828
- next_line,
1829
- blk_avg_char_width,
1830
- blk_avg_char_height,
1831
- blk_media_font_size,
1832
- )
1833
-
1834
- if is_title:
1835
- curr_line["is_title"] = is_title
1836
- prev_line_is_title_flag = True
1837
- else:
1838
- curr_line["is_title"] = False
1839
- prev_line_is_title_flag = False
1840
-
1841
- # print(f"curr_line['text']: {curr_line['text']}")
1842
- # print(f"curr_line['is_title']: {curr_line['is_title']}")
1843
- # print(f"prev_line['text']: {prev_line['text'] if prev_line else None}")
1844
- # print(f"prev_line_is_title_flag: {prev_line_is_title_flag}")
1845
- # print()
1846
-
1847
- if is_author_or_org_list:
1848
- curr_line["is_author_or_org_list"] = is_author_or_org_list
1849
- else:
1850
- curr_line["is_author_or_org_list"] = False
1851
-
1852
- return input_block
1853
-
1854
- def batch_detect_titles(self, pdf_dic):
1855
- """
1856
- This function batch process the blocks to detect titles.
1857
-
1858
- Parameters
1859
- ----------
1860
- pdf_dict : dict
1861
- result dictionary
1862
-
1863
- Returns
1864
- -------
1865
- pdf_dict : dict
1866
- result dictionary
1867
- """
1868
- num_titles = 0
1869
-
1870
- for page_id, blocks in pdf_dic.items():
1871
- if page_id.startswith("page_"):
1872
- para_blocks = []
1873
- if "para_blocks" in blocks.keys():
1874
- para_blocks = blocks["para_blocks"]
1875
-
1876
- all_single_line_blocks = []
1877
- for block in para_blocks:
1878
- if len(block["lines"]) == 1:
1879
- all_single_line_blocks.append(block)
1880
-
1881
- new_para_blocks = []
1882
- if not len(all_single_line_blocks) == len(para_blocks): # Not all blocks are single line blocks.
1883
- for para_block in para_blocks:
1884
- new_block = self._detect_title(para_block)
1885
- new_para_blocks.append(new_block)
1886
- num_titles += sum([line.get("is_title", 0) for line in new_block["lines"]])
1887
- else: # All blocks are single line blocks.
1888
- for para_block in para_blocks:
1889
- new_para_blocks.append(para_block)
1890
- num_titles += sum([line.get("is_title", 0) for line in para_block["lines"]])
1891
- para_blocks = new_para_blocks
1892
-
1893
- blocks["para_blocks"] = para_blocks
1894
-
1895
- for para_block in para_blocks:
1896
- all_titles = all(safe_get(line, "is_title", False) for line in para_block["lines"])
1897
- para_text_len = sum([len(line["text"]) for line in para_block["lines"]])
1898
- if (
1899
- all_titles and para_text_len < 200
1900
- ): # total length of the paragraph is less than 200, more than this should not be a title
1901
- para_block["is_block_title"] = 1
1902
- else:
1903
- para_block["is_block_title"] = 0
1904
-
1905
- all_name_or_org_list_to_be_removed = all(
1906
- safe_get(line, "is_author_or_org_list", False) for line in para_block["lines"]
1907
- )
1908
- if all_name_or_org_list_to_be_removed and page_id == "page_0":
1909
- para_block["is_block_an_author_or_org_list"] = 1
1910
- else:
1911
- para_block["is_block_an_author_or_org_list"] = 0
1912
-
1913
- pdf_dic["statistics"]["num_titles"] = num_titles
1914
-
1915
- return pdf_dic
1916
-
1917
- def _recog_title_level(self, title_blocks):
1918
- """
1919
- This function determines the title level based on the font size of the title.
1920
-
1921
- Parameters
1922
- ----------
1923
- title_blocks : list
1924
-
1925
- Returns
1926
- -------
1927
- title_blocks : list
1928
- """
1929
-
1930
- font_sizes = np.array([safe_get(tb["block"], "block_font_size", 0) for tb in title_blocks])
1931
-
1932
- # Use the mean and std of font sizes to remove extreme values
1933
- mean_font_size = np.mean(font_sizes)
1934
- std_font_size = np.std(font_sizes)
1935
- min_extreme_font_size = mean_font_size - std_font_size # type: ignore
1936
- max_extreme_font_size = mean_font_size + std_font_size # type: ignore
1937
-
1938
- # Compute the threshold for title level
1939
- middle_font_sizes = font_sizes[(font_sizes > min_extreme_font_size) & (font_sizes < max_extreme_font_size)]
1940
- if middle_font_sizes.size > 0:
1941
- middle_mean_font_size = np.mean(middle_font_sizes)
1942
- level_threshold = middle_mean_font_size
1943
- else:
1944
- level_threshold = mean_font_size
1945
-
1946
- for tb in title_blocks:
1947
- title_block = tb["block"]
1948
- title_font_size = safe_get(title_block, "block_font_size", 0)
1949
-
1950
- current_level = 1 # Initialize title level, the biggest level is 1
1951
-
1952
- # print(f"Before adjustment by font size, {current_level}")
1953
- if title_font_size >= max_extreme_font_size:
1954
- current_level = 1
1955
- elif title_font_size <= min_extreme_font_size:
1956
- current_level = 3
1957
- elif float(title_font_size) >= float(level_threshold):
1958
- current_level = 2
1959
- else:
1960
- current_level = 3
1961
- # print(f"After adjustment by font size, {current_level}")
1962
-
1963
- title_block["block_title_level"] = current_level
1964
-
1965
- return title_blocks
1966
-
1967
- def batch_recog_title_level(self, pdf_dic):
1968
- """
1969
- This function batch process the blocks to recognize title level.
1970
-
1971
- Parameters
1972
- ----------
1973
- pdf_dict : dict
1974
- result dictionary
1975
-
1976
- Returns
1977
- -------
1978
- pdf_dict : dict
1979
- result dictionary
1980
- """
1981
- title_blocks = []
1982
-
1983
- # Collect all titles
1984
- for page_id, blocks in pdf_dic.items():
1985
- if page_id.startswith("page_"):
1986
- para_blocks = blocks.get("para_blocks", [])
1987
- for block in para_blocks:
1988
- if block.get("is_block_title"):
1989
- title_obj = {"page_id": page_id, "block": block}
1990
- title_blocks.append(title_obj)
1991
-
1992
- # Determine title level
1993
- if title_blocks:
1994
- # Determine title level based on font size
1995
- title_blocks = self._recog_title_level(title_blocks)
1996
-
1997
- return pdf_dic
1998
-
1999
-
2000
- class BlockTerminationProcessor:
2001
- """
2002
- This class is used to process the block termination.
2003
- """
2004
-
2005
- def __init__(self) -> None:
2006
- pass
2007
-
2008
- def _is_consistent_lines(
2009
- self,
2010
- curr_line,
2011
- prev_line,
2012
- next_line,
2013
- consistent_direction, # 0 for prev, 1 for next, 2 for both
2014
- ):
2015
- """
2016
- This function checks if the line is consistent with its neighbors
2017
-
2018
- Parameters
2019
- ----------
2020
- curr_line : dict
2021
- current line
2022
- prev_line : dict
2023
- previous line
2024
- next_line : dict
2025
- next line
2026
- consistent_direction : int
2027
- 0 for prev, 1 for next, 2 for both
2028
-
2029
- Returns
2030
- -------
2031
- bool
2032
- True if the line is consistent with its neighbors, False otherwise.
2033
- """
2034
-
2035
- curr_line_font_size = curr_line["spans"][0]["size"]
2036
- curr_line_font_type = curr_line["spans"][0]["font"].lower()
2037
-
2038
- if consistent_direction == 0:
2039
- if prev_line:
2040
- prev_line_font_size = prev_line["spans"][0]["size"]
2041
- prev_line_font_type = prev_line["spans"][0]["font"].lower()
2042
- return curr_line_font_size == prev_line_font_size and curr_line_font_type == prev_line_font_type
2043
- else:
2044
- return False
2045
-
2046
- elif consistent_direction == 1:
2047
- if next_line:
2048
- next_line_font_size = next_line["spans"][0]["size"]
2049
- next_line_font_type = next_line["spans"][0]["font"].lower()
2050
- return curr_line_font_size == next_line_font_size and curr_line_font_type == next_line_font_type
2051
- else:
2052
- return False
2053
-
2054
- elif consistent_direction == 2:
2055
- if prev_line and next_line:
2056
- prev_line_font_size = prev_line["spans"][0]["size"]
2057
- prev_line_font_type = prev_line["spans"][0]["font"].lower()
2058
- next_line_font_size = next_line["spans"][0]["size"]
2059
- next_line_font_type = next_line["spans"][0]["font"].lower()
2060
- return (curr_line_font_size == prev_line_font_size and curr_line_font_type == prev_line_font_type) and (
2061
- curr_line_font_size == next_line_font_size and curr_line_font_type == next_line_font_type
2062
- )
2063
- else:
2064
- return False
2065
-
2066
- else:
2067
- return False
2068
-
2069
- def _is_regular_line(self, curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, X0, X1, avg_line_height):
2070
- """
2071
- This function checks if the line is a regular line
2072
-
2073
- Parameters
2074
- ----------
2075
- curr_line_bbox : list
2076
- bbox of the current line
2077
- prev_line_bbox : list
2078
- bbox of the previous line
2079
- next_line_bbox : list
2080
- bbox of the next line
2081
- avg_char_width : float
2082
- average of char widths
2083
- X0 : float
2084
- median of x0 values, which represents the left average boundary of the page
2085
- X1 : float
2086
- median of x1 values, which represents the right average boundary of the page
2087
- avg_line_height : float
2088
- average of line heights
2089
-
2090
- Returns
2091
- -------
2092
- bool
2093
- True if the line is a regular line, False otherwise.
2094
- """
2095
- horizontal_ratio = 0.5
2096
- vertical_ratio = 0.5
2097
- horizontal_thres = horizontal_ratio * avg_char_width
2098
- vertical_thres = vertical_ratio * avg_line_height
2099
-
2100
- x0, y0, x1, y1 = curr_line_bbox
2101
-
2102
- x0_near_X0 = abs(x0 - X0) < horizontal_thres
2103
- x1_near_X1 = abs(x1 - X1) < horizontal_thres
2104
-
2105
- prev_line_is_end_of_para = prev_line_bbox and (abs(prev_line_bbox[2] - X1) > avg_char_width)
2106
-
2107
- sufficient_spacing_above = False
2108
- if prev_line_bbox:
2109
- vertical_spacing_above = y1 - prev_line_bbox[3]
2110
- sufficient_spacing_above = vertical_spacing_above > vertical_thres
2111
-
2112
- sufficient_spacing_below = False
2113
- if next_line_bbox:
2114
- vertical_spacing_below = next_line_bbox[1] - y0
2115
- sufficient_spacing_below = vertical_spacing_below > vertical_thres
2116
-
2117
- return (
2118
- (sufficient_spacing_above or sufficient_spacing_below)
2119
- or (not x0_near_X0 and not x1_near_X1)
2120
- or prev_line_is_end_of_para
2121
- )
2122
-
2123
- def _is_possible_start_of_para(self, curr_line, prev_line, next_line, X0, X1, avg_char_width, avg_font_size):
2124
- """
2125
- This function checks if the line is a possible start of a paragraph
2126
-
2127
- Parameters
2128
- ----------
2129
- curr_line : dict
2130
- current line
2131
- prev_line : dict
2132
- previous line
2133
- next_line : dict
2134
- next line
2135
- X0 : float
2136
- median of x0 values, which represents the left average boundary of the page
2137
- X1 : float
2138
- median of x1 values, which represents the right average boundary of the page
2139
- avg_char_width : float
2140
- average of char widths
2141
- avg_line_height : float
2142
- average of line heights
2143
-
2144
- Returns
2145
- -------
2146
- bool
2147
- True if the line is a possible start of a paragraph, False otherwise.
2148
- """
2149
- start_confidence = 0.5 # Initial confidence of the line being a start of a paragraph
2150
- decision_path = [] # Record the decision path
2151
-
2152
- curr_line_bbox = curr_line["bbox"]
2153
- prev_line_bbox = prev_line["bbox"] if prev_line else None
2154
- next_line_bbox = next_line["bbox"] if next_line else None
2155
-
2156
- indent_ratio = 1
2157
-
2158
- vertical_ratio = 1.5
2159
- vertical_thres = vertical_ratio * avg_font_size
2160
-
2161
- left_horizontal_ratio = 0.5
2162
- left_horizontal_thres = left_horizontal_ratio * avg_char_width
2163
-
2164
- right_horizontal_ratio = 2.5
2165
- right_horizontal_thres = right_horizontal_ratio * avg_char_width
2166
-
2167
- x0, y0, x1, y1 = curr_line_bbox
2168
-
2169
- indent_condition = x0 > X0 + indent_ratio * avg_char_width
2170
- if indent_condition:
2171
- start_confidence += 0.2
2172
- decision_path.append("indent_condition_met")
2173
-
2174
- x0_near_X0 = abs(x0 - X0) < left_horizontal_thres
2175
- if x0_near_X0:
2176
- start_confidence += 0.1
2177
- decision_path.append("x0_near_X0")
2178
-
2179
- x1_near_X1 = abs(x1 - X1) < right_horizontal_thres
2180
- if x1_near_X1:
2181
- start_confidence += 0.1
2182
- decision_path.append("x1_near_X1")
2183
-
2184
- if prev_line is None:
2185
- prev_line_is_end_of_para = True
2186
- start_confidence += 0.2
2187
- decision_path.append("no_prev_line")
2188
- else:
2189
- prev_line_is_end_of_para, _, _ = self._is_possible_end_of_para(prev_line, next_line, X0, X1, avg_char_width)
2190
- if prev_line_is_end_of_para:
2191
- start_confidence += 0.1
2192
- decision_path.append("prev_line_is_end_of_para")
2193
-
2194
- sufficient_spacing_above = False
2195
- if prev_line_bbox:
2196
- vertical_spacing_above = y1 - prev_line_bbox[3]
2197
- sufficient_spacing_above = vertical_spacing_above > vertical_thres
2198
- if sufficient_spacing_above:
2199
- start_confidence += 0.2
2200
- decision_path.append("sufficient_spacing_above")
2201
-
2202
- sufficient_spacing_below = False
2203
- if next_line_bbox:
2204
- vertical_spacing_below = next_line_bbox[1] - y0
2205
- sufficient_spacing_below = vertical_spacing_below > vertical_thres
2206
- if sufficient_spacing_below:
2207
- start_confidence += 0.2
2208
- decision_path.append("sufficient_spacing_below")
2209
-
2210
- is_regular_line = self._is_regular_line(
2211
- curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, X0, X1, avg_font_size
2212
- )
2213
- if is_regular_line:
2214
- start_confidence += 0.1
2215
- decision_path.append("is_regular_line")
2216
-
2217
- is_start_of_para = (
2218
- (sufficient_spacing_above or sufficient_spacing_below)
2219
- or (indent_condition)
2220
- or (not indent_condition and x0_near_X0 and x1_near_X1 and not is_regular_line)
2221
- or prev_line_is_end_of_para
2222
- )
2223
- return (is_start_of_para, start_confidence, decision_path)
2224
-
2225
- def _is_possible_end_of_para(self, curr_line, next_line, X0, X1, avg_char_width):
2226
- """
2227
- This function checks if the line is a possible end of a paragraph
2228
-
2229
- Parameters
2230
- ----------
2231
- curr_line : dict
2232
- current line
2233
- next_line : dict
2234
- next line
2235
- X0 : float
2236
- median of x0 values, which represents the left average boundary of the page
2237
- X1 : float
2238
- median of x1 values, which represents the right average boundary of the page
2239
- avg_char_width : float
2240
- average of char widths
2241
-
2242
- Returns
2243
- -------
2244
- bool
2245
- True if the line is a possible end of a paragraph, False otherwise.
2246
- """
2247
-
2248
- end_confidence = 0.5 # Initial confidence of the line being a end of a paragraph
2249
- decision_path = [] # Record the decision path
2250
-
2251
- curr_line_bbox = curr_line["bbox"]
2252
- next_line_bbox = next_line["bbox"] if next_line else None
2253
-
2254
- left_horizontal_ratio = 0.5
2255
- right_horizontal_ratio = 0.5
2256
-
2257
- x0, _, x1, y1 = curr_line_bbox
2258
- next_x0, next_y0, _, _ = next_line_bbox if next_line_bbox else (0, 0, 0, 0)
2259
-
2260
- x0_near_X0 = abs(x0 - X0) < left_horizontal_ratio * avg_char_width
2261
- if x0_near_X0:
2262
- end_confidence += 0.1
2263
- decision_path.append("x0_near_X0")
2264
-
2265
- x1_smaller_than_X1 = x1 < X1 - right_horizontal_ratio * avg_char_width
2266
- if x1_smaller_than_X1:
2267
- end_confidence += 0.1
2268
- decision_path.append("x1_smaller_than_X1")
2269
-
2270
- next_line_is_start_of_para = (
2271
- next_line_bbox
2272
- and (next_x0 > X0 + left_horizontal_ratio * avg_char_width)
2273
- and (not is_line_left_aligned_from_neighbors(curr_line_bbox, None, next_line_bbox, avg_char_width, direction=1))
2274
- )
2275
- if next_line_is_start_of_para:
2276
- end_confidence += 0.2
2277
- decision_path.append("next_line_is_start_of_para")
2278
-
2279
- is_line_left_aligned_from_neighbors_bool = is_line_left_aligned_from_neighbors(
2280
- curr_line_bbox, None, next_line_bbox, avg_char_width
2281
- )
2282
- if is_line_left_aligned_from_neighbors_bool:
2283
- end_confidence += 0.1
2284
- decision_path.append("line_is_left_aligned_from_neighbors")
2285
-
2286
- is_line_right_aligned_from_neighbors_bool = is_line_right_aligned_from_neighbors(
2287
- curr_line_bbox, None, next_line_bbox, avg_char_width
2288
- )
2289
- if not is_line_right_aligned_from_neighbors_bool:
2290
- end_confidence += 0.1
2291
- decision_path.append("line_is_not_right_aligned_from_neighbors")
2292
-
2293
- is_end_of_para = end_with_punctuation(curr_line["text"]) and (
2294
- (x0_near_X0 and x1_smaller_than_X1)
2295
- or (is_line_left_aligned_from_neighbors_bool and not is_line_right_aligned_from_neighbors_bool)
2296
- )
2297
-
2298
- return (is_end_of_para, end_confidence, decision_path)
2299
-
2300
- def _cut_paras_per_block(
2301
- self,
2302
- block,
2303
- ):
2304
- """
2305
- Processes a raw block from PyMuPDF and returns the processed block.
2306
-
2307
- Parameters
2308
- ----------
2309
- raw_block : dict
2310
- A raw block from pymupdf.
2311
-
2312
- Returns
2313
- -------
2314
- processed_block : dict
2315
-
2316
- """
2317
-
2318
- def _construct_para(lines, is_block_title, para_title_level):
2319
- """
2320
- Construct a paragraph from given lines.
2321
- """
2322
-
2323
- font_sizes = [span["size"] for line in lines for span in line["spans"]]
2324
- avg_font_size = sum(font_sizes) / len(font_sizes) if font_sizes else 0
2325
-
2326
- font_colors = [span["color"] for line in lines for span in line["spans"]]
2327
- most_common_font_color = max(set(font_colors), key=font_colors.count) if font_colors else None
2328
-
2329
- font_type_lengths = {}
2330
- for line in lines:
2331
- for span in line["spans"]:
2332
- font_type = span["font"]
2333
- bbox_width = span["bbox"][2] - span["bbox"][0]
2334
- if font_type in font_type_lengths:
2335
- font_type_lengths[font_type] += bbox_width
2336
- else:
2337
- font_type_lengths[font_type] = bbox_width
2338
-
2339
- # get the font type with the longest bbox width
2340
- most_common_font_type = max(font_type_lengths, key=font_type_lengths.get) if font_type_lengths else None # type: ignore
2341
-
2342
- para_bbox = calculate_para_bbox(lines)
2343
- para_text = " ".join(line["text"] for line in lines)
2344
-
2345
- return {
2346
- "para_bbox": para_bbox,
2347
- "para_text": para_text,
2348
- "para_font_type": most_common_font_type,
2349
- "para_font_size": avg_font_size,
2350
- "para_font_color": most_common_font_color,
2351
- "is_para_title": is_block_title,
2352
- "para_title_level": para_title_level,
2353
- }
2354
-
2355
- block_bbox = block["bbox"]
2356
- block_text = block["text"]
2357
- block_lines = block["lines"]
2358
-
2359
- X0 = safe_get(block, "X0", 0)
2360
- X1 = safe_get(block, "X1", 0)
2361
- avg_char_width = safe_get(block, "avg_char_width", 0)
2362
- avg_char_height = safe_get(block, "avg_char_height", 0)
2363
- avg_font_size = safe_get(block, "avg_font_size", 0)
2364
-
2365
- is_block_title = safe_get(block, "is_block_title", False)
2366
- para_title_level = safe_get(block, "block_title_level", 0)
2367
-
2368
- # Segment into paragraphs
2369
- para_ranges = []
2370
- in_paragraph = False
2371
- start_idx_of_para = None
2372
-
2373
- # Create the processed paragraphs
2374
- processed_paras = {}
2375
- para_bboxes = []
2376
- end_idx_of_para = 0
2377
-
2378
- for line_index, line in enumerate(block_lines):
2379
- curr_line = line
2380
- prev_line = block_lines[line_index - 1] if line_index > 0 else None
2381
- next_line = block_lines[line_index + 1] if line_index < len(block_lines) - 1 else None
2382
-
2383
- """
2384
- Start processing paragraphs.
2385
- """
2386
-
2387
- # Check if the line is the start of a paragraph
2388
- is_start_of_para, start_confidence, decision_path = self._is_possible_start_of_para(
2389
- curr_line, prev_line, next_line, X0, X1, avg_char_width, avg_font_size
2390
- )
2391
- if not in_paragraph and is_start_of_para:
2392
- in_paragraph = True
2393
- start_idx_of_para = line_index
2394
-
2395
- # print_green(">>> Start of a paragraph")
2396
- # print(" curr_line_text: ", curr_line["text"])
2397
- # print(" start_confidence: ", start_confidence)
2398
- # print(" decision_path: ", decision_path)
2399
-
2400
- # Check if the line is the end of a paragraph
2401
- is_end_of_para, end_confidence, decision_path = self._is_possible_end_of_para(
2402
- curr_line, next_line, X0, X1, avg_char_width
2403
- )
2404
- if in_paragraph and (is_end_of_para or not next_line):
2405
- para_ranges.append((start_idx_of_para, line_index))
2406
- start_idx_of_para = None
2407
- in_paragraph = False
2408
-
2409
- # print_red(">>> End of a paragraph")
2410
- # print(" curr_line_text: ", curr_line["text"])
2411
- # print(" end_confidence: ", end_confidence)
2412
- # print(" decision_path: ", decision_path)
2413
-
2414
- # Add the last paragraph if it is not added
2415
- if in_paragraph and start_idx_of_para is not None:
2416
- para_ranges.append((start_idx_of_para, len(block_lines) - 1))
2417
-
2418
- # Process the matched paragraphs
2419
- for para_index, (start_idx, end_idx) in enumerate(para_ranges):
2420
- matched_lines = block_lines[start_idx : end_idx + 1]
2421
- para_properties = _construct_para(matched_lines, is_block_title, para_title_level)
2422
- para_key = f"para_{len(processed_paras)}"
2423
- processed_paras[para_key] = para_properties
2424
- para_bboxes.append(para_properties["para_bbox"])
2425
- end_idx_of_para = end_idx + 1
2426
-
2427
- # Deal with the remaining lines
2428
- if end_idx_of_para < len(block_lines):
2429
- unmatched_lines = block_lines[end_idx_of_para:]
2430
- unmatched_properties = _construct_para(unmatched_lines, is_block_title, para_title_level)
2431
- unmatched_key = f"para_{len(processed_paras)}"
2432
- processed_paras[unmatched_key] = unmatched_properties
2433
- para_bboxes.append(unmatched_properties["para_bbox"])
2434
-
2435
- block["paras"] = processed_paras
2436
-
2437
- return block
2438
-
2439
- def batch_process_blocks(self, pdf_dict):
2440
- """
2441
- Parses the blocks of all pages.
2442
-
2443
- Parameters
2444
- ----------
2445
- pdf_dict : dict
2446
- PDF dictionary.
2447
- filter_blocks : list
2448
- List of bounding boxes to filter.
2449
-
2450
- Returns
2451
- -------
2452
- result_dict : dict
2453
- Result dictionary.
2454
-
2455
- """
2456
-
2457
- num_paras = 0
2458
-
2459
- for page_id, page in pdf_dict.items():
2460
- if page_id.startswith("page_"):
2461
- para_blocks = []
2462
- if "para_blocks" in page.keys():
2463
- input_blocks = page["para_blocks"]
2464
- for input_block in input_blocks:
2465
- new_block = self._cut_paras_per_block(input_block)
2466
- para_blocks.append(new_block)
2467
- num_paras += len(new_block["paras"])
2468
-
2469
- page["para_blocks"] = para_blocks
2470
-
2471
- pdf_dict["statistics"]["num_paras"] = num_paras
2472
- return pdf_dict
2473
-
2474
-
2475
- class BlockContinuationProcessor:
2476
- """
2477
- This class is used to process the blocks to detect block continuations.
2478
- """
2479
-
2480
- def __init__(self) -> None:
2481
- pass
2482
-
2483
- def __is_similar_font_type(self, font_type_1, font_type_2, prefix_length_ratio=0.3):
2484
- """
2485
- This function checks if the two font types are similar.
2486
- Definition of similar font types: the two font types have a common prefix,
2487
- and the length of the common prefix is at least a certain ratio of the length of the shorter font type.
2488
-
2489
- Parameters
2490
- ----------
2491
- font_type1 : str
2492
- font type 1
2493
- font_type2 : str
2494
- font type 2
2495
- prefix_length_ratio : float
2496
- minimum ratio of the common prefix length to the length of the shorter font type
2497
-
2498
- Returns
2499
- -------
2500
- bool
2501
- True if the two font types are similar, False otherwise.
2502
- """
2503
-
2504
- if isinstance(font_type_1, list):
2505
- font_type_1 = font_type_1[0] if font_type_1 else ""
2506
- if isinstance(font_type_2, list):
2507
- font_type_2 = font_type_2[0] if font_type_2 else ""
2508
-
2509
- if font_type_1 == font_type_2:
2510
- return True
2511
-
2512
- # Find the length of the common prefix
2513
- common_prefix_length = len(os.path.commonprefix([font_type_1, font_type_2]))
2514
-
2515
- # Calculate the minimum prefix length based on the ratio
2516
- min_prefix_length = int(min(len(font_type_1), len(font_type_2)) * prefix_length_ratio)
2517
-
2518
- return common_prefix_length >= min_prefix_length
2519
-
2520
- def __is_same_block_font(self, block_1, block_2):
2521
- """
2522
- This function compares the font of block1 and block2
2523
-
2524
- Parameters
2525
- ----------
2526
- block1 : dict
2527
- block1
2528
- block2 : dict
2529
- block2
2530
-
2531
- Returns
2532
- -------
2533
- is_same : bool
2534
- True if block1 and block2 have the same font, else False
2535
- """
2536
- block_1_font_type = safe_get(block_1, "block_font_type", "")
2537
- block_1_font_size = safe_get(block_1, "block_font_size", 0)
2538
- block_1_avg_char_width = safe_get(block_1, "avg_char_width", 0)
2539
-
2540
- block_2_font_type = safe_get(block_2, "block_font_type", "")
2541
- block_2_font_size = safe_get(block_2, "block_font_size", 0)
2542
- block_2_avg_char_width = safe_get(block_2, "avg_char_width", 0)
2543
-
2544
- if isinstance(block_1_font_size, list):
2545
- block_1_font_size = block_1_font_size[0] if block_1_font_size else 0
2546
- if isinstance(block_2_font_size, list):
2547
- block_2_font_size = block_2_font_size[0] if block_2_font_size else 0
2548
-
2549
- block_1_text = safe_get(block_1, "text", "")
2550
- block_2_text = safe_get(block_2, "text", "")
2551
-
2552
- if block_1_avg_char_width == 0 or block_2_avg_char_width == 0:
2553
- return False
2554
-
2555
- if not block_1_text or not block_2_text:
2556
- return False
2557
- else:
2558
- text_len_ratio = len(block_2_text) / len(block_1_text)
2559
- if text_len_ratio < 0.2:
2560
- avg_char_width_condition = (
2561
- abs(block_1_avg_char_width - block_2_avg_char_width) / min(block_1_avg_char_width, block_2_avg_char_width)
2562
- < 0.5
2563
- )
2564
- else:
2565
- avg_char_width_condition = (
2566
- abs(block_1_avg_char_width - block_2_avg_char_width) / min(block_1_avg_char_width, block_2_avg_char_width)
2567
- < 0.2
2568
- )
2569
-
2570
- block_font_size_condition = abs(block_1_font_size - block_2_font_size) < 1
2571
-
2572
- return (
2573
- self.__is_similar_font_type(block_1_font_type, block_2_font_type)
2574
- and avg_char_width_condition
2575
- and block_font_size_condition
2576
- )
2577
-
2578
- def _is_alphabet_char(self, char):
2579
- if (char >= "\u0041" and char <= "\u005a") or (char >= "\u0061" and char <= "\u007a"):
2580
- return True
2581
- else:
2582
- return False
2583
-
2584
- def _is_chinese_char(self, char):
2585
- if char >= "\u4e00" and char <= "\u9fa5":
2586
- return True
2587
- else:
2588
- return False
2589
-
2590
- def _is_other_letter_char(self, char):
2591
- try:
2592
- cat = unicodedata.category(char)
2593
- if cat == "Lu" or cat == "Ll":
2594
- return not self._is_alphabet_char(char) and not self._is_chinese_char(char)
2595
- except TypeError:
2596
- print("The input to the function must be a single character.")
2597
- return False
2598
-
2599
- def _is_year(self, s: str):
2600
- try:
2601
- number = int(s)
2602
- return 1900 <= number <= 2099
2603
- except ValueError:
2604
- return False
2605
-
2606
- def _match_brackets(self, text):
2607
- # pattern = r"^[\(\)\[\]()【】{}{}<><>〔〕〘〙\"\'“”‘’]"
2608
- pattern = r"^[\(\)\]()】{}{}>>〕〙\"\'“”‘’]"
2609
- return bool(re.match(pattern, text))
2610
-
2611
- def _is_para_font_consistent(self, para_1, para_2):
2612
- """
2613
- This function compares the font of para1 and para2
2614
-
2615
- Parameters
2616
- ----------
2617
- para1 : dict
2618
- para1
2619
- para2 : dict
2620
- para2
2621
-
2622
- Returns
2623
- -------
2624
- is_same : bool
2625
- True if para1 and para2 have the same font, else False
2626
- """
2627
- if para_1 is None or para_2 is None:
2628
- return False
2629
-
2630
- para_1_font_type = safe_get(para_1, "para_font_type", "")
2631
- para_1_font_size = safe_get(para_1, "para_font_size", 0)
2632
- para_1_font_color = safe_get(para_1, "para_font_color", "")
2633
-
2634
- para_2_font_type = safe_get(para_2, "para_font_type", "")
2635
- para_2_font_size = safe_get(para_2, "para_font_size", 0)
2636
- para_2_font_color = safe_get(para_2, "para_font_color", "")
2637
-
2638
- if isinstance(para_1_font_type, list): # get the most common font type
2639
- para_1_font_type = max(set(para_1_font_type), key=para_1_font_type.count)
2640
- if isinstance(para_2_font_type, list):
2641
- para_2_font_type = max(set(para_2_font_type), key=para_2_font_type.count)
2642
- if isinstance(para_1_font_size, list): # compute average font type
2643
- para_1_font_size = sum(para_1_font_size) / len(para_1_font_size)
2644
- if isinstance(para_2_font_size, list): # compute average font type
2645
- para_2_font_size = sum(para_2_font_size) / len(para_2_font_size)
2646
-
2647
- return (
2648
- self.__is_similar_font_type(para_1_font_type, para_2_font_type)
2649
- and abs(para_1_font_size - para_2_font_size) < 1.5
2650
- # and para_font_color1 == para_font_color2
2651
- )
2652
-
2653
- def _is_para_puncs_consistent(self, para_1, para_2):
2654
- """
2655
- This function determines whether para1 and para2 are originally from the same paragraph by checking the puncs of para1(former) and para2(latter)
2656
-
2657
- Parameters
2658
- ----------
2659
- para1 : dict
2660
- para1
2661
- para2 : dict
2662
- para2
2663
-
2664
- Returns
2665
- -------
2666
- is_same : bool
2667
- True if para1 and para2 are from the same paragraph by using the puncs, else False
2668
- """
2669
- para_1_text = safe_get(para_1, "para_text", "").strip()
2670
- para_2_text = safe_get(para_2, "para_text", "").strip()
2671
-
2672
- para_1_bboxes = safe_get(para_1, "para_bbox", [])
2673
- para_1_font_sizes = safe_get(para_1, "para_font_size", 0)
2674
-
2675
- para_2_bboxes = safe_get(para_2, "para_bbox", [])
2676
- para_2_font_sizes = safe_get(para_2, "para_font_size", 0)
2677
-
2678
- # print_yellow(" Features of determine puncs_consistent:")
2679
- # print(f" para_1_text: {para_1_text}")
2680
- # print(f" para_2_text: {para_2_text}")
2681
- # print(f" para_1_bboxes: {para_1_bboxes}")
2682
- # print(f" para_2_bboxes: {para_2_bboxes}")
2683
- # print(f" para_1_font_sizes: {para_1_font_sizes}")
2684
- # print(f" para_2_font_sizes: {para_2_font_sizes}")
2685
-
2686
- if is_nested_list(para_1_bboxes):
2687
- x0_1, y0_1, x1_1, y1_1 = para_1_bboxes[-1]
2688
- else:
2689
- x0_1, y0_1, x1_1, y1_1 = para_1_bboxes
2690
-
2691
- if is_nested_list(para_2_bboxes):
2692
- x0_2, y0_2, x1_2, y1_2 = para_2_bboxes[0]
2693
- para_2_font_sizes = para_2_font_sizes[0] # type: ignore
2694
- else:
2695
- x0_2, y0_2, x1_2, y1_2 = para_2_bboxes
2696
-
2697
- right_align_threshold = 0.5 * (para_1_font_sizes + para_2_font_sizes) * 0.8
2698
- are_two_paras_right_aligned = abs(x1_1 - x1_2) < right_align_threshold
2699
-
2700
- left_indent_threshold = 0.5 * (para_1_font_sizes + para_2_font_sizes) * 0.8
2701
- is_para1_left_indent_than_papa2 = x0_1 - x0_2 > left_indent_threshold
2702
- is_para2_left_indent_than_papa1 = x0_2 - x0_1 > left_indent_threshold
2703
-
2704
- # Check if either para_text1 or para_text2 is empty
2705
- if not para_1_text or not para_2_text:
2706
- return False
2707
-
2708
- # Define the end puncs for a sentence to end and hyphen
2709
- end_puncs = [".", "?", "!", "。", "?", "!", "…"]
2710
- hyphen = ["-", "—"]
2711
-
2712
- # Check if para_text1 ends with either hyphen or non-end punctuation or spaces
2713
- para_1_end_with_hyphen = para_1_text and para_1_text[-1] in hyphen
2714
- para_1_end_with_end_punc = para_1_text and para_1_text[-1] in end_puncs
2715
- para_1_end_with_space = para_1_text and para_1_text[-1] == " "
2716
- para_1_not_end_with_end_punc = para_1_text and para_1_text[-1] not in end_puncs
2717
-
2718
- # print_yellow(f" para_1_end_with_hyphen: {para_1_end_with_hyphen}")
2719
- # print_yellow(f" para_1_end_with_end_punc: {para_1_end_with_end_punc}")
2720
- # print_yellow(f" para_1_not_end_with_end_punc: {para_1_not_end_with_end_punc}")
2721
- # print_yellow(f" para_1_end_with_space: {para_1_end_with_space}")
2722
-
2723
- if para_1_end_with_hyphen: # If para_text1 ends with hyphen
2724
- # print_red(f"para_1 is end with hyphen.")
2725
- para_2_is_consistent = para_2_text and (
2726
- para_2_text[0] in hyphen
2727
- or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].islower())
2728
- or (self._is_chinese_char(para_2_text[0]))
2729
- or (self._is_other_letter_char(para_2_text[0]))
2730
- )
2731
- if para_2_is_consistent:
2732
- # print(f"para_2 is consistent.\n")
2733
- return True
2734
- else:
2735
- # print(f"para_2 is not consistent.\n")
2736
- pass
2737
-
2738
- elif para_1_end_with_end_punc: # If para_text1 ends with ending punctuations
2739
- # print_red(f"para_1 is end with end_punc.")
2740
- para_2_is_consistent = (
2741
- para_2_text
2742
- and (
2743
- para_2_text[0]
2744
- == " "
2745
- # or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].isupper())
2746
- # or (self._is_chinese_char(para_2_text[0]))
2747
- # or (self._is_other_letter_char(para_2_text[0]))
2748
- )
2749
- and not is_para2_left_indent_than_papa1
2750
- )
2751
- if para_2_is_consistent:
2752
- # print(f"para_2 is consistent.\n")
2753
- return True
2754
- else:
2755
- # print(f"para_2 is not consistent.\n")
2756
- pass
2757
-
2758
- elif para_1_not_end_with_end_punc: # If para_text1 is not end with ending punctuations
2759
- # print_red(f"para_1 is NOT end with end_punc.")
2760
- para_2_is_consistent = para_2_text and (
2761
- para_2_text[0] == " "
2762
- or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].islower())
2763
- or (self._is_alphabet_char(para_2_text[0]))
2764
- or (self._is_year(para_2_text[0:4]))
2765
- or (are_two_paras_right_aligned or is_para1_left_indent_than_papa2)
2766
- or (self._is_chinese_char(para_2_text[0]))
2767
- or (self._is_other_letter_char(para_2_text[0]))
2768
- or (self._match_brackets(para_2_text[0]))
2769
- )
2770
- if para_2_is_consistent:
2771
- # print(f"para_2 is consistent.\n")
2772
- return True
2773
- else:
2774
- # print(f"para_2 is not consistent.\n")
2775
- pass
2776
-
2777
- elif para_1_end_with_space: # If para_text1 ends with space
2778
- # print_red(f"para_1 is end with space.")
2779
- para_2_is_consistent = para_2_text and (
2780
- para_2_text[0] == " "
2781
- or (self._is_alphabet_char(para_2_text[0]) and para_2_text[0].islower())
2782
- or (self._is_chinese_char(para_2_text[0]))
2783
- or (self._is_other_letter_char(para_2_text[0]))
2784
- )
2785
- if para_2_is_consistent:
2786
- # print(f"para_2 is consistent.\n")
2787
- return True
2788
- else:
2789
- pass
2790
- # print(f"para_2 is not consistent.\n")
2791
-
2792
- return False
2793
-
2794
- def _is_block_consistent(self, block_1, block_2):
2795
- """
2796
- This function determines whether block1 and block2 are originally from the same block
2797
-
2798
- Parameters
2799
- ----------
2800
- block1 : dict
2801
- block1s
2802
- block2 : dict
2803
- block2
2804
-
2805
- Returns
2806
- -------
2807
- is_same : bool
2808
- True if block1 and block2 are from the same block, else False
2809
- """
2810
- return self.__is_same_block_font(block_1, block_2)
2811
-
2812
- def _is_para_continued(self, para_1, para_2):
2813
- """
2814
- This function determines whether para1 and para2 are originally from the same paragraph
2815
-
2816
- Parameters
2817
- ----------
2818
- para1 : dict
2819
- para1
2820
- para2 : dict
2821
- para2
2822
-
2823
- Returns
2824
- -------
2825
- is_same : bool
2826
- True if para1 and para2 are from the same paragraph, else False
2827
- """
2828
- is_para_font_consistent = self._is_para_font_consistent(para_1, para_2)
2829
- is_para_puncs_consistent = self._is_para_puncs_consistent(para_1, para_2)
2830
-
2831
- return is_para_font_consistent and is_para_puncs_consistent
2832
-
2833
- def _are_boundaries_of_block_consistent(self, block_1, block_2):
2834
- """
2835
- This function checks if the boundaries of block1 and block2 are consistent
2836
-
2837
- Parameters
2838
- ----------
2839
- block1 : dict
2840
- block1
2841
-
2842
- block2 : dict
2843
- block2
2844
-
2845
- Returns
2846
- -------
2847
- is_consistent : bool
2848
- True if the boundaries of block1 and block2 are consistent, else False
2849
- """
2850
-
2851
- last_line_of_block_1 = block_1["lines"][-1]
2852
- first_line_of_block_2 = block_2["lines"][0]
2853
-
2854
- spans_of_last_line_of_block_1 = last_line_of_block_1["spans"]
2855
- spans_of_first_line_of_block_2 = first_line_of_block_2["spans"]
2856
-
2857
- font_type_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["font"].lower()
2858
- font_size_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["size"]
2859
- font_color_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["color"]
2860
- font_flags_of_last_line_of_block_1 = spans_of_last_line_of_block_1[0]["flags"]
2861
-
2862
- font_type_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["font"].lower()
2863
- font_size_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["size"]
2864
- font_color_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["color"]
2865
- font_flags_of_first_line_of_block_2 = spans_of_first_line_of_block_2[0]["flags"]
2866
-
2867
- return (
2868
- self.__is_similar_font_type(font_type_of_last_line_of_block_1, font_type_of_first_line_of_block_2)
2869
- and abs(font_size_of_last_line_of_block_1 - font_size_of_first_line_of_block_2) < 1
2870
- # and font_color_of_last_line_of_block1 == font_color_of_first_line_of_block2
2871
- and font_flags_of_last_line_of_block_1 == font_flags_of_first_line_of_block_2
2872
- )
2873
-
2874
- def should_merge_next_para(self, curr_para, next_para):
2875
- """
2876
- This function checks if the next_para should be merged into the curr_para.
2877
-
2878
- Parameters
2879
- ----------
2880
- curr_para : dict
2881
- The current paragraph.
2882
- next_para : dict
2883
- The next paragraph.
2884
-
2885
- Returns
2886
- -------
2887
- bool
2888
- True if the next_para should be merged into the curr_para, False otherwise.
2889
- """
2890
- if self._is_para_continued(curr_para, next_para):
2891
- return True
2892
- else:
2893
- return False
2894
-
2895
- def batch_tag_paras(self, pdf_dict):
2896
- """
2897
- This function tags the paragraphs in the pdf_dict.
2898
-
2899
- Parameters
2900
- ----------
2901
- pdf_dict : dict
2902
- PDF dictionary.
2903
-
2904
- Returns
2905
- -------
2906
- pdf_dict : dict
2907
- PDF dictionary with tagged paragraphs.
2908
- """
2909
- the_last_page_id = len(pdf_dict) - 1
2910
-
2911
- for curr_page_idx, (curr_page_id, curr_page_content) in enumerate(pdf_dict.items()):
2912
- if curr_page_id.startswith("page_") and curr_page_content.get("para_blocks", []):
2913
- para_blocks_of_curr_page = curr_page_content["para_blocks"]
2914
- next_page_idx = curr_page_idx + 1
2915
- next_page_id = f"page_{next_page_idx}"
2916
- next_page_content = pdf_dict.get(next_page_id, {})
2917
-
2918
- for i, current_block in enumerate(para_blocks_of_curr_page):
2919
- for para_id, curr_para in current_block["paras"].items():
2920
- curr_para["curr_para_location"] = [
2921
- curr_page_idx,
2922
- current_block["block_id"],
2923
- int(para_id.split("_")[-1]),
2924
- ]
2925
- curr_para["next_para_location"] = None # 默认设置为None
2926
- curr_para["merge_next_para"] = False # 默认设置为False
2927
-
2928
- next_block = para_blocks_of_curr_page[i + 1] if i < len(para_blocks_of_curr_page) - 1 else None
2929
-
2930
- if next_block:
2931
- curr_block_last_para_key = list(current_block["paras"].keys())[-1]
2932
- curr_blk_last_para = current_block["paras"][curr_block_last_para_key]
2933
-
2934
- next_block_first_para_key = list(next_block["paras"].keys())[0]
2935
- next_blk_first_para = next_block["paras"][next_block_first_para_key]
2936
-
2937
- if self.should_merge_next_para(curr_blk_last_para, next_blk_first_para):
2938
- curr_blk_last_para["next_para_location"] = [
2939
- curr_page_idx,
2940
- next_block["block_id"],
2941
- int(next_block_first_para_key.split("_")[-1]),
2942
- ]
2943
- curr_blk_last_para["merge_next_para"] = True
2944
- else:
2945
- # Handle the case where the next block is in a different page
2946
- curr_block_last_para_key = list(current_block["paras"].keys())[-1]
2947
- curr_blk_last_para = current_block["paras"][curr_block_last_para_key]
2948
-
2949
- while not next_page_content.get("para_blocks", []) and next_page_idx <= the_last_page_id:
2950
- next_page_idx += 1
2951
- next_page_id = f"page_{next_page_idx}"
2952
- next_page_content = pdf_dict.get(next_page_id, {})
2953
-
2954
- if next_page_content.get("para_blocks", []):
2955
- next_blk_first_para_key = list(next_page_content["para_blocks"][0]["paras"].keys())[0]
2956
- next_blk_first_para = next_page_content["para_blocks"][0]["paras"][next_blk_first_para_key]
2957
-
2958
- if self.should_merge_next_para(curr_blk_last_para, next_blk_first_para):
2959
- curr_blk_last_para["next_para_location"] = [
2960
- next_page_idx,
2961
- next_page_content["para_blocks"][0]["block_id"],
2962
- int(next_blk_first_para_key.split("_")[-1]),
2963
- ]
2964
- curr_blk_last_para["merge_next_para"] = True
2965
-
2966
- return pdf_dict
2967
-
2968
- def find_block_by_id(self, para_blocks, block_id):
2969
- """
2970
- This function finds a block by its id.
2971
-
2972
- Parameters
2973
- ----------
2974
- para_blocks : list
2975
- List of blocks.
2976
- block_id : int
2977
- Id of the block to find.
2978
-
2979
- Returns
2980
- -------
2981
- block : dict
2982
- The block with the given id.
2983
- """
2984
- for blk_idx, block in enumerate(para_blocks):
2985
- if block.get("block_id") == block_id:
2986
- return block
2987
- return None
2988
-
2989
- def batch_merge_paras(self, pdf_dict):
2990
- """
2991
- This function merges the paragraphs in the pdf_dict.
2992
-
2993
- Parameters
2994
- ----------
2995
- pdf_dict : dict
2996
- PDF dictionary.
2997
-
2998
- Returns
2999
- -------
3000
- pdf_dict : dict
3001
- PDF dictionary with merged paragraphs.
3002
- """
3003
- for page_id, page_content in pdf_dict.items():
3004
- if page_id.startswith("page_") and page_content.get("para_blocks", []):
3005
- para_blocks_of_page = page_content["para_blocks"]
3006
-
3007
- for i in range(len(para_blocks_of_page)):
3008
- current_block = para_blocks_of_page[i]
3009
- paras = current_block["paras"]
3010
-
3011
- for para_id, curr_para in list(paras.items()):
3012
- # print(f"current para_id: {para_id}")
3013
- # 跳过标题段落
3014
- if curr_para.get("is_para_title"):
3015
- continue
3016
-
3017
- while curr_para.get("merge_next_para"):
3018
- curr_para_location = curr_para.get("curr_para_location")
3019
- next_para_location = curr_para.get("next_para_location")
3020
-
3021
- # print(f"curr_para_location: {curr_para_location}, next_para_location: {next_para_location}")
3022
-
3023
- if not next_para_location:
3024
- break
3025
-
3026
- if curr_para_location == next_para_location:
3027
- # print_red("The next para is in the same block as the current para.")
3028
- curr_para["merge_next_para"] = False
3029
- break
3030
-
3031
- next_page_idx, next_block_id, next_para_id = next_para_location
3032
- next_page_id = f"page_{next_page_idx}"
3033
- next_page_content = pdf_dict.get(next_page_id)
3034
- if not next_page_content:
3035
- break
3036
-
3037
- next_block = self.find_block_by_id(next_page_content.get("para_blocks", []), next_block_id)
3038
-
3039
- if not next_block:
3040
- break
3041
-
3042
- next_para = next_block["paras"].get(f"para_{next_para_id}")
3043
-
3044
- if not next_para or next_para.get("is_para_title"):
3045
- break
3046
-
3047
- # 合并段落文本
3048
- curr_para_text = curr_para.get("para_text", "")
3049
- next_para_text = next_para.get("para_text", "")
3050
- curr_para["para_text"] = curr_para_text + " " + next_para_text
3051
-
3052
- # 更新 next_para_location
3053
- curr_para["next_para_location"] = next_para.get("next_para_location")
3054
-
3055
- # 将下一个段落文本置为空,表示已被合并
3056
- next_para["para_text"] = ""
3057
-
3058
- # 更新 merge_next_para 标记
3059
- curr_para["merge_next_para"] = next_para.get("merge_next_para", False)
3060
-
3061
- return pdf_dict
3062
-
3063
-
3064
- class DrawAnnos:
3065
- """
3066
- This class draws annotations on the pdf file
3067
-
3068
- ----------------------------------------
3069
- Color Code
3070
- ----------------------------------------
3071
- Red: (1, 0, 0)
3072
- Green: (0, 1, 0)
3073
- Blue: (0, 0, 1)
3074
- Yellow: (1, 1, 0) - mix of red and green
3075
- Cyan: (0, 1, 1) - mix of green and blue
3076
- Magenta: (1, 0, 1) - mix of red and blue
3077
- White: (1, 1, 1) - red, green and blue full intensity
3078
- Black: (0, 0, 0) - no color component whatsoever
3079
- Gray: (0.5, 0.5, 0.5) - equal and medium intensity of red, green and blue color components
3080
- Orange: (1, 0.65, 0) - maximum intensity of red, medium intensity of green, no blue component
3081
- """
3082
-
3083
- def __init__(self) -> None:
3084
- pass
3085
-
3086
- def __is_nested_list(self, lst):
3087
- """
3088
- This function returns True if the given list is a nested list of any degree.
3089
- """
3090
- if isinstance(lst, list):
3091
- return any(self.__is_nested_list(i) for i in lst) or any(isinstance(i, list) for i in lst)
3092
- return False
3093
-
3094
- def __valid_rect(self, bbox):
3095
- # Ensure that the rectangle is not empty or invalid
3096
- if isinstance(bbox[0], list):
3097
- return False # It's a nested list, hence it can't be valid rect
3098
- else:
3099
- return bbox[0] < bbox[2] and bbox[1] < bbox[3]
3100
-
3101
- def __draw_nested_boxes(self, page, nested_bbox, color=(0, 1, 1)):
3102
- """
3103
- This function draws the nested boxes
3104
-
3105
- Parameters
3106
- ----------
3107
- page : fitz.Page
3108
- page
3109
- nested_bbox : list
3110
- nested bbox
3111
- color : tuple
3112
- color, by default (0, 1, 1) # draw with cyan color for combined paragraph
3113
- """
3114
- if self.__is_nested_list(nested_bbox): # If it's a nested list
3115
- for bbox in nested_bbox:
3116
- self.__draw_nested_boxes(page, bbox, color) # Recursively call the function
3117
- elif self.__valid_rect(nested_bbox): # If valid rectangle
3118
- para_rect = fitz.Rect(nested_bbox)
3119
- para_anno = page.add_rect_annot(para_rect)
3120
- para_anno.set_colors(stroke=color) # draw with cyan color for combined paragraph
3121
- para_anno.set_border(width=1)
3122
- para_anno.update()
3123
-
3124
- def draw_annos(self, input_pdf_path, pdf_dic, output_pdf_path):
3125
- """
3126
- This function draws annotations on the pdf file.
3127
-
3128
- Parameters
3129
- ----------
3130
- input_pdf_path : str
3131
- path to the input pdf file
3132
- pdf_dic : dict
3133
- pdf dictionary
3134
- output_pdf_path : str
3135
- path to the output pdf file
3136
-
3137
- pdf_dic : dict
3138
- pdf dictionary
3139
- """
3140
- pdf_doc = open_pdf(input_pdf_path)
3141
-
3142
- if pdf_dic is None:
3143
- pdf_dic = {}
3144
-
3145
- if output_pdf_path is None:
3146
- output_pdf_path = input_pdf_path.replace(".pdf", "_anno.pdf")
3147
-
3148
- for page_id, page in enumerate(pdf_doc): # type: ignore
3149
- page_key = f"page_{page_id}"
3150
- for ele_key, ele_data in pdf_dic[page_key].items():
3151
- if ele_key == "para_blocks":
3152
- para_blocks = ele_data
3153
- for para_block in para_blocks:
3154
- if "paras" in para_block.keys():
3155
- paras = para_block["paras"]
3156
- for para_key, para_content in paras.items():
3157
- para_bbox = para_content["para_bbox"]
3158
- # print(f"para_bbox: {para_bbox}")
3159
- # print(f"is a nested list: {self.__is_nested_list(para_bbox)}")
3160
- if self.__is_nested_list(para_bbox) and len(para_bbox) > 1:
3161
- color = (0, 1, 1)
3162
- self.__draw_nested_boxes(
3163
- page, para_bbox, color
3164
- ) # draw with cyan color for combined paragraph
3165
- else:
3166
- if self.__valid_rect(para_bbox):
3167
- para_rect = fitz.Rect(para_bbox)
3168
- para_anno = page.add_rect_annot(para_rect)
3169
- para_anno.set_colors(stroke=(0, 1, 0)) # draw with green color for normal paragraph
3170
- para_anno.set_border(width=0.5)
3171
- para_anno.update()
3172
-
3173
- is_para_title = para_content["is_para_title"]
3174
- if is_para_title:
3175
- if self.__is_nested_list(para_content["para_bbox"]) and len(para_content["para_bbox"]) > 1:
3176
- color = (0, 0, 1)
3177
- self.__draw_nested_boxes(
3178
- page, para_content["para_bbox"], color
3179
- ) # draw with cyan color for combined title
3180
- else:
3181
- if self.__valid_rect(para_content["para_bbox"]):
3182
- para_rect = fitz.Rect(para_content["para_bbox"])
3183
- if self.__valid_rect(para_content["para_bbox"]):
3184
- para_anno = page.add_rect_annot(para_rect)
3185
- para_anno.set_colors(stroke=(0, 0, 1)) # draw with blue color for normal title
3186
- para_anno.set_border(width=0.5)
3187
- para_anno.update()
3188
-
3189
- pdf_doc.save(output_pdf_path)
3190
- pdf_doc.close()
3191
-
3192
-
3193
- class ParaProcessPipeline:
3194
- def __init__(self) -> None:
3195
- pass
3196
-
3197
- def para_process_pipeline(self, pdf_info_dict, para_debug_mode=None, input_pdf_path=None, output_pdf_path=None):
3198
- """
3199
- This function processes the paragraphs, including:
3200
- 1. Read raw input json file into pdf_dic
3201
- 2. Detect and replace equations
3202
- 3. Combine spans into a natural line
3203
- 4. Check if the paragraphs are inside bboxes passed from "layout_bboxes" key
3204
- 5. Compute statistics for each block
3205
- 6. Detect titles in the document
3206
- 7. Detect paragraphs inside each block
3207
- 8. Divide the level of the titles
3208
- 9. Detect and combine paragraphs from different blocks into one paragraph
3209
- 10. Check whether the final results after checking headings, dividing paragraphs within blocks, and merging paragraphs between blocks are plausible and reasonable.
3210
- 11. Draw annotations on the pdf file
3211
-
3212
- Parameters
3213
- ----------
3214
- pdf_dic_json_fpath : str
3215
- path to the pdf dictionary json file.
3216
- Notice: data noises, including overlap blocks, header, footer, watermark, vertical margin note have been removed already.
3217
- input_pdf_doc : str
3218
- path to the input pdf file
3219
- output_pdf_path : str
3220
- path to the output pdf file
3221
-
3222
- Returns
3223
- -------
3224
- pdf_dict : dict
3225
- result dictionary
3226
- """
3227
-
3228
- error_info = None
3229
-
3230
- output_json_file = ""
3231
- output_dir = ""
3232
-
3233
- if input_pdf_path is not None:
3234
- input_pdf_path = os.path.abspath(input_pdf_path)
3235
-
3236
- # print_green_on_red(f">>>>>>>>>>>>>>>>>>> Process the paragraphs of {input_pdf_path}")
3237
-
3238
- if output_pdf_path is not None:
3239
- output_dir = os.path.dirname(output_pdf_path)
3240
- output_json_file = f"{output_dir}/pdf_dic.json"
3241
-
3242
- def __save_pdf_dic(pdf_dic, output_pdf_path, stage="0", para_debug_mode=para_debug_mode):
3243
- """
3244
- Save the pdf_dic to a json file
3245
- """
3246
- output_pdf_file_name = os.path.basename(output_pdf_path)
3247
- # output_dir = os.path.dirname(output_pdf_path)
3248
- output_dir = "\\tmp\\pdf_parse"
3249
- output_pdf_file_name = output_pdf_file_name.replace(".pdf", f"_stage_{stage}.json")
3250
- pdf_dic_json_fpath = os.path.join(output_dir, output_pdf_file_name)
3251
-
3252
- if not os.path.exists(output_dir):
3253
- os.makedirs(output_dir)
3254
-
3255
- if para_debug_mode == "full":
3256
- with open(pdf_dic_json_fpath, "w", encoding="utf-8") as f:
3257
- json.dump(pdf_dic, f, indent=2, ensure_ascii=False)
3258
-
3259
- # Validate the output already exists
3260
- if not os.path.exists(pdf_dic_json_fpath):
3261
- print_red(f"Failed to save the pdf_dic to {pdf_dic_json_fpath}")
3262
- return None
3263
- else:
3264
- print_green(f"Succeed to save the pdf_dic to {pdf_dic_json_fpath}")
3265
-
3266
- return pdf_dic_json_fpath
3267
-
3268
- """
3269
- Preprocess the lines of block
3270
- """
3271
- # Combine spans into a natural line
3272
- rawBlockProcessor = RawBlockProcessor()
3273
- pdf_dic = rawBlockProcessor.batch_process_blocks(pdf_info_dict)
3274
- # print(f"pdf_dic['page_0']['para_blocks'][0]: {pdf_dic['page_0']['para_blocks'][0]}", end="\n\n")
3275
-
3276
- # Check if the paragraphs are inside bboxes passed from "layout_bboxes" key
3277
- layoutFilter = LayoutFilterProcessor()
3278
- pdf_dic = layoutFilter.batch_process_blocks(pdf_dic)
3279
-
3280
- # Compute statistics for each block
3281
- blockStatisticsCalculator = BlockStatisticsCalculator()
3282
- pdf_dic = blockStatisticsCalculator.batch_process_blocks(pdf_dic)
3283
- # print(f"pdf_dic['page_0']['para_blocks'][0]: {pdf_dic['page_0']['para_blocks'][0]}", end="\n\n")
3284
-
3285
- # Compute statistics for all blocks(namely this pdf document)
3286
- docStatisticsCalculator = DocStatisticsCalculator()
3287
- pdf_dic = docStatisticsCalculator.calc_stats_of_doc(pdf_dic)
3288
- # print(f"pdf_dic['statistics']: {pdf_dic['statistics']}", end="\n\n")
3289
-
3290
- # Dump the first three stages of pdf_dic to a json file
3291
- if para_debug_mode == "full":
3292
- pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="0", para_debug_mode=para_debug_mode)
3293
-
3294
- """
3295
- Detect titles in the document
3296
- """
3297
- doc_statistics = pdf_dic["statistics"]
3298
- titleProcessor = TitleProcessor(doc_statistics)
3299
- pdf_dic = titleProcessor.batch_detect_titles(pdf_dic)
3300
-
3301
- if para_debug_mode == "full":
3302
- pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="1", para_debug_mode=para_debug_mode)
3303
-
3304
- """
3305
- Detect and divide the level of the titles
3306
- """
3307
- titleProcessor = TitleProcessor()
3308
-
3309
- pdf_dic = titleProcessor.batch_recog_title_level(pdf_dic)
3310
-
3311
- if para_debug_mode == "full":
3312
- pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="2", para_debug_mode=para_debug_mode)
3313
-
3314
- """
3315
- Detect and split paragraphs inside each block
3316
- """
3317
- blockInnerParasProcessor = BlockTerminationProcessor()
3318
-
3319
- pdf_dic = blockInnerParasProcessor.batch_process_blocks(pdf_dic)
3320
-
3321
- if para_debug_mode == "full":
3322
- pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="3", para_debug_mode=para_debug_mode)
3323
-
3324
- # pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="3", para_debug_mode="full")
3325
- # print_green(f"pdf_dic_json_fpath: {pdf_dic_json_fpath}")
3326
-
3327
- """
3328
- Detect and combine paragraphs from different blocks into one paragraph
3329
- """
3330
- blockContinuationProcessor = BlockContinuationProcessor()
3331
-
3332
- pdf_dic = blockContinuationProcessor.batch_tag_paras(pdf_dic)
3333
- pdf_dic = blockContinuationProcessor.batch_merge_paras(pdf_dic)
3334
-
3335
- if para_debug_mode == "full":
3336
- pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="4", para_debug_mode=para_debug_mode)
3337
-
3338
- # pdf_dic_json_fpath = __save_pdf_dic(pdf_dic, output_pdf_path, stage="4", para_debug_mode="full")
3339
- # print_green(f"pdf_dic_json_fpath: {pdf_dic_json_fpath}")
3340
-
3341
- """
3342
- Discard pdf files by checking exceptions and return the error info to the caller
3343
- """
3344
- discardByException = DiscardByException()
3345
-
3346
- is_discard_by_single_line_block = discardByException.discard_by_single_line_block(
3347
- pdf_dic, exception=DenseSingleLineBlockException()
3348
- )
3349
- is_discard_by_title_detection = discardByException.discard_by_title_detection(
3350
- pdf_dic, exception=TitleDetectionException()
3351
- )
3352
- is_discard_by_title_level = discardByException.discard_by_title_level(pdf_dic, exception=TitleLevelException())
3353
- is_discard_by_split_para = discardByException.discard_by_split_para(pdf_dic, exception=ParaSplitException())
3354
- is_discard_by_merge_para = discardByException.discard_by_merge_para(pdf_dic, exception=ParaMergeException())
3355
-
3356
- if is_discard_by_single_line_block is not None:
3357
- error_info = is_discard_by_single_line_block
3358
- elif is_discard_by_title_detection is not None:
3359
- error_info = is_discard_by_title_detection
3360
- elif is_discard_by_title_level is not None:
3361
- error_info = is_discard_by_title_level
3362
- elif is_discard_by_split_para is not None:
3363
- error_info = is_discard_by_split_para
3364
- elif is_discard_by_merge_para is not None:
3365
- error_info = is_discard_by_merge_para
3366
-
3367
- if error_info is not None:
3368
- return pdf_dic, error_info
3369
-
3370
- """
3371
- Dump the final pdf_dic to a json file
3372
- """
3373
- if para_debug_mode is not None:
3374
- with open(output_json_file, "w", encoding="utf-8") as f:
3375
- json.dump(pdf_info_dict, f, ensure_ascii=False, indent=4)
3376
-
3377
- """
3378
- Draw the annotations
3379
- """
3380
- if para_debug_mode is not None:
3381
- drawAnnos = DrawAnnos()
3382
- drawAnnos.draw_annos(input_pdf_path, pdf_dic, output_pdf_path)
3383
-
3384
- """
3385
- Remove the intermediate files which are generated in the process of paragraph processing if debug_mode is simple
3386
- """
3387
- if para_debug_mode is not None:
3388
- for fpath in os.listdir(output_dir):
3389
- if fpath.endswith(".json") and "stage" in fpath:
3390
- os.remove(os.path.join(output_dir, fpath))
3391
-
3392
- return pdf_dic, error_info
3393
-
3394
-
3395
- """
3396
- Run this script to test the function with Command:
3397
-
3398
- python detect_para.py [pdf_path] [output_pdf_path]
3399
-
3400
- Params:
3401
- - pdf_path: the path of the pdf file
3402
- - output_pdf_path: the path of the output pdf file
3403
- """
3404
-
3405
- if __name__ == "__main__":
3406
- DEFAULT_PDF_PATH = (
3407
- "app/pdf_toolbox/tests/assets/paper/paper.pdf" if os.name != "nt" else "app\\pdf_toolbox\\tests\\assets\\paper\\paper.pdf"
3408
- )
3409
- input_pdf_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_PDF_PATH
3410
- output_pdf_path = sys.argv[2] if len(sys.argv) > 2 else input_pdf_path.split(".")[0] + "_recogPara.pdf"
3411
- output_json_path = sys.argv[3] if len(sys.argv) > 3 else input_pdf_path.split(".")[0] + "_recogPara.json"
3412
-
3413
- import stat
3414
-
3415
- # Remove existing output file if it exists
3416
- if os.path.exists(output_pdf_path):
3417
- os.chmod(output_pdf_path, stat.S_IWRITE)
3418
- os.remove(output_pdf_path)
3419
-
3420
- input_pdf_doc = open_pdf(input_pdf_path)
3421
-
3422
- # postprocess the paragraphs
3423
- paraProcessPipeline = ParaProcessPipeline()
3424
-
3425
- # parse paragraph and save to json file
3426
- pdf_dic = {}
3427
-
3428
- blockInnerParasProcessor = BlockTerminationProcessor()
3429
-
3430
- """
3431
- Construct the pdf dictionary.
3432
- """
3433
-
3434
- for page_id, page in enumerate(input_pdf_doc): # type: ignore
3435
- # print(f"Processing page {page_id}")
3436
- # print(f"page: {page}")
3437
- raw_blocks = page.get_text("dict")["blocks"]
3438
-
3439
- # Save text blocks to "preproc_blocks"
3440
- preproc_blocks = []
3441
- for block in raw_blocks:
3442
- if block["type"] == 0:
3443
- preproc_blocks.append(block)
3444
-
3445
- layout_bboxes = []
3446
-
3447
- # Construct the pdf dictionary as schema above
3448
- page_dict = {
3449
- "para_blocks": None,
3450
- "preproc_blocks": preproc_blocks,
3451
- "images": None,
3452
- "tables": None,
3453
- "interline_equations": None,
3454
- "inline_equations": None,
3455
- "layout_bboxes": None,
3456
- "pymu_raw_blocks": None,
3457
- "global_statistic": None,
3458
- "droped_text_block": None,
3459
- "droped_image_block": None,
3460
- "droped_table_block": None,
3461
- "image_backup": None,
3462
- "table_backup": None,
3463
- }
3464
-
3465
- pdf_dic[f"page_{page_id}"] = page_dict
3466
-
3467
- # print(f"pdf_dic: {pdf_dic}")
3468
-
3469
- with open(output_json_path, "w", encoding="utf-8") as f:
3470
- json.dump(pdf_dic, f, ensure_ascii=False, indent=4)
3471
-
3472
- pdf_dic = paraProcessPipeline.para_process_pipeline(output_json_path, input_pdf_doc, output_pdf_path)