ocr_mkcontent.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from magic_pdf.libs.commons import s3_image_save_path, join_path
  2. from magic_pdf.libs.markdown_utils import ocr_escape_special_markdown_char
  3. from magic_pdf.libs.ocr_content_type import ContentType
  4. def ocr_mk_nlp_markdown(pdf_info_dict: dict):
  5. markdown = []
  6. for _, page_info in pdf_info_dict.items():
  7. blocks = page_info.get("preproc_blocks")
  8. if not blocks:
  9. continue
  10. for block in blocks:
  11. for line in block['lines']:
  12. line_text = ''
  13. for span in line['spans']:
  14. if not span.get('content'):
  15. continue
  16. content = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  17. if span['type'] == ContentType.InlineEquation:
  18. content = f"${content}$"
  19. elif span['type'] == ContentType.InterlineEquation:
  20. content = f"$$\n{content}\n$$"
  21. line_text += content + ' '
  22. # 在行末添加两个空格以强制换行
  23. markdown.append(line_text.strip() + ' ')
  24. return '\n'.join(markdown)
  25. def ocr_mk_mm_markdown(pdf_info_dict: dict):
  26. markdown = []
  27. for _, page_info in pdf_info_dict.items():
  28. blocks = page_info.get("preproc_blocks")
  29. if not blocks:
  30. continue
  31. for block in blocks:
  32. for line in block['lines']:
  33. line_text = ''
  34. for span in line['spans']:
  35. if not span.get('content'):
  36. if not span.get('image_path'):
  37. continue
  38. else:
  39. content = f"![]({join_path(s3_image_save_path, span['image_path'])})"
  40. else:
  41. content = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  42. if span['type'] == ContentType.InlineEquation:
  43. content = f"${content}$"
  44. elif span['type'] == ContentType.InterlineEquation:
  45. content = f"$$\n{content}\n$$"
  46. line_text += content + ' '
  47. # 在行末添加两个空格以强制换行
  48. markdown.append(line_text.strip() + ' ')
  49. return '\n'.join(markdown)
  50. def ocr_mk_mm_markdown_with_para(pdf_info_dict: dict):
  51. markdown = []
  52. for _, page_info in pdf_info_dict.items():
  53. paras = page_info.get("para_blocks")
  54. if not paras:
  55. continue
  56. for para in paras:
  57. para_text = ''
  58. for line in para:
  59. for span in line['spans']:
  60. span_type = span.get('type')
  61. if span_type == ContentType.Text:
  62. para_text += span['content']
  63. elif span_type == ContentType.InlineEquation:
  64. para_text += f" ${span['content']}$ "
  65. elif span_type == ContentType.InterlineEquation:
  66. para_text += f"$$\n{span['content']}\n$$ "
  67. elif span_type == ContentType.Image:
  68. para_text += f"![]({join_path(s3_image_save_path, span['image_path'])})"
  69. markdown.append(para_text)
  70. return '\n\n'.join(markdown)
  71. def make_standard_format_with_para(pdf_info_dict: dict):
  72. content_list = []
  73. for _, page_info in pdf_info_dict.items():
  74. paras = page_info.get("para_blocks")
  75. if not paras:
  76. continue
  77. for para in paras:
  78. for line in para:
  79. content = line_to_standard_format(line)
  80. content_list.append(content)
  81. return content_list
  82. def line_to_standard_format(line):
  83. line_text = ""
  84. inline_equation_num = 0
  85. for span in line['spans']:
  86. if not span.get('content'):
  87. if not span.get('image_path'):
  88. continue
  89. else:
  90. if span['type'] == ContentType.Image:
  91. content = {
  92. 'type': 'image',
  93. 'img_path': join_path(s3_image_save_path, span['image_path'])
  94. }
  95. return content
  96. elif span['type'] == ContentType.Table:
  97. content = {
  98. 'type': 'table',
  99. 'img_path': join_path(s3_image_save_path, span['image_path'])
  100. }
  101. return content
  102. else:
  103. if span['type'] == ContentType.InterlineEquation:
  104. interline_equation = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  105. content = {
  106. 'type': 'equation',
  107. 'latex': f"$$\n{interline_equation}\n$$"
  108. }
  109. return content
  110. elif span['type'] == ContentType.InlineEquation:
  111. inline_equation = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  112. line_text += f"${inline_equation}$"
  113. inline_equation_num += 1
  114. elif span['type'] == ContentType.Text:
  115. line_text += span['content']
  116. content = {
  117. 'type': 'text',
  118. 'text': line_text,
  119. 'inline_equation_num': inline_equation_num
  120. }
  121. return content
  122. def ocr_mk_mm_standard_format(pdf_info_dict: dict):
  123. '''
  124. content_list
  125. type string image/text/table/equation(行间的单独拿出来,行内的和text合并)
  126. latex string latex文本字段。
  127. text string 纯文本格式的文本数据。
  128. md string markdown格式的文本数据。
  129. img_path string s3://full/path/to/img.jpg
  130. '''
  131. content_list = []
  132. for _, page_info in pdf_info_dict.items():
  133. blocks = page_info.get("preproc_blocks")
  134. if not blocks:
  135. continue
  136. for block in blocks:
  137. for line in block['lines']:
  138. content = line_to_standard_format(line)
  139. content_list.append(content)
  140. return content_list