ocr_mkcontent.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. content = span['content']
  63. elif span_type == ContentType.InlineEquation:
  64. content = f" ${span['content']}$ "
  65. elif span_type == ContentType.InterlineEquation:
  66. content = f"$$\n{span['content']}\n$$ "
  67. elif span_type in [ContentType.Image, ContentType.Table]:
  68. content = f"![]({join_path(s3_image_save_path, span['image_path'])})"
  69. para_text += content + ' '
  70. markdown.append(para_text.strip() + ' ')
  71. return '\n'.join(markdown)
  72. def make_standard_format_with_para(pdf_info_dict: dict):
  73. content_list = []
  74. for _, page_info in pdf_info_dict.items():
  75. paras = page_info.get("para_blocks")
  76. if not paras:
  77. continue
  78. for para in paras:
  79. for line in para:
  80. content = line_to_standard_format(line)
  81. content_list.append(content)
  82. return content_list
  83. def line_to_standard_format(line):
  84. line_text = ""
  85. inline_equation_num = 0
  86. for span in line['spans']:
  87. if not span.get('content'):
  88. if not span.get('image_path'):
  89. continue
  90. else:
  91. if span['type'] == ContentType.Image:
  92. content = {
  93. 'type': 'image',
  94. 'img_path': join_path(s3_image_save_path, span['image_path'])
  95. }
  96. return content
  97. elif span['type'] == ContentType.Table:
  98. content = {
  99. 'type': 'table',
  100. 'img_path': join_path(s3_image_save_path, span['image_path'])
  101. }
  102. return content
  103. else:
  104. if span['type'] == ContentType.InterlineEquation:
  105. interline_equation = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  106. content = {
  107. 'type': 'equation',
  108. 'latex': f"$$\n{interline_equation}\n$$"
  109. }
  110. return content
  111. elif span['type'] == ContentType.InlineEquation:
  112. inline_equation = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  113. line_text += f"${inline_equation}$"
  114. inline_equation_num += 1
  115. elif span['type'] == ContentType.Text:
  116. text_content = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  117. line_text += text_content
  118. content = {
  119. 'type': 'text',
  120. 'text': line_text,
  121. 'inline_equation_num': inline_equation_num
  122. }
  123. return content
  124. def ocr_mk_mm_standard_format(pdf_info_dict: dict):
  125. '''
  126. content_list
  127. type string image/text/table/equation(行间的单独拿出来,行内的和text合并)
  128. latex string latex文本字段。
  129. text string 纯文本格式的文本数据。
  130. md string markdown格式的文本数据。
  131. img_path string s3://full/path/to/img.jpg
  132. '''
  133. content_list = []
  134. for _, page_info in pdf_info_dict.items():
  135. blocks = page_info.get("preproc_blocks")
  136. if not blocks:
  137. continue
  138. for block in blocks:
  139. for line in block['lines']:
  140. content = line_to_standard_format(line)
  141. content_list.append(content)
  142. return content_list