vlm_middle_json_mkcontent.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from mineru.utils.config_reader import get_latex_delimiter_config
  2. from mineru.utils.enum_class import MakeMode, BlockType, ContentType
  3. latex_delimiters_config = get_latex_delimiter_config()
  4. default_delimiters = {
  5. 'display': {'left': '$$', 'right': '$$'},
  6. 'inline': {'left': '$', 'right': '$'}
  7. }
  8. delimiters = latex_delimiters_config if latex_delimiters_config else default_delimiters
  9. display_left_delimiter = delimiters['display']['left']
  10. display_right_delimiter = delimiters['display']['right']
  11. inline_left_delimiter = delimiters['inline']['left']
  12. inline_right_delimiter = delimiters['inline']['right']
  13. def merge_para_with_text(para_block):
  14. para_text = ''
  15. for line in para_block['lines']:
  16. for j, span in enumerate(line['spans']):
  17. span_type = span['type']
  18. content = ''
  19. if span_type == ContentType.TEXT:
  20. content = span['content']
  21. elif span_type == ContentType.INLINE_EQUATION:
  22. content = f"{inline_left_delimiter}{span['content']}{inline_right_delimiter}"
  23. elif span_type == ContentType.INTERLINE_EQUATION:
  24. content = f"\n{display_left_delimiter}\n{span['content']}\n{display_right_delimiter}\n"
  25. # content = content.strip()
  26. if content:
  27. if span_type in [ContentType.TEXT, ContentType.INLINE_EQUATION]:
  28. if j == len(line['spans']) - 1:
  29. para_text += content
  30. else:
  31. para_text += f'{content} '
  32. elif span_type == ContentType.INTERLINE_EQUATION:
  33. para_text += content
  34. return para_text
  35. def mk_blocks_to_markdown(para_blocks, make_mode, img_buket_path=''):
  36. page_markdown = []
  37. for para_block in para_blocks:
  38. para_text = ''
  39. para_type = para_block['type']
  40. if para_type in [BlockType.TEXT, BlockType.LIST, BlockType.INDEX, BlockType.INTERLINE_EQUATION]:
  41. para_text = merge_para_with_text(para_block)
  42. elif para_type == BlockType.TITLE:
  43. title_level = get_title_level(para_block)
  44. para_text = f'{"#" * title_level} {merge_para_with_text(para_block)}'
  45. elif para_type == BlockType.IMAGE:
  46. if make_mode == MakeMode.NLP_MD:
  47. continue
  48. elif make_mode == MakeMode.MM_MD:
  49. # 检测是否存在图片脚注
  50. has_image_footnote = any(block['type'] == BlockType.IMAGE_FOOTNOTE for block in para_block['blocks'])
  51. # 如果存在图片脚注,则将图片脚注拼接到图片正文后面
  52. if has_image_footnote:
  53. for block in para_block['blocks']: # 1st.拼image_caption
  54. if block['type'] == BlockType.IMAGE_CAPTION:
  55. para_text += merge_para_with_text(block) + ' \n'
  56. for block in para_block['blocks']: # 2nd.拼image_body
  57. if block['type'] == BlockType.IMAGE_BODY:
  58. for line in block['lines']:
  59. for span in line['spans']:
  60. if span['type'] == ContentType.IMAGE:
  61. if span.get('image_path', ''):
  62. para_text += f"![]({img_buket_path}/{span['image_path']})"
  63. for block in para_block['blocks']: # 3rd.拼image_footnote
  64. if block['type'] == BlockType.IMAGE_FOOTNOTE:
  65. para_text += ' \n' + merge_para_with_text(block)
  66. else:
  67. for block in para_block['blocks']: # 1st.拼image_body
  68. if block['type'] == BlockType.IMAGE_BODY:
  69. for line in block['lines']:
  70. for span in line['spans']:
  71. if span['type'] == ContentType.IMAGE:
  72. if span.get('image_path', ''):
  73. para_text += f"![]({img_buket_path}/{span['image_path']})"
  74. for block in para_block['blocks']: # 2nd.拼image_caption
  75. if block['type'] == BlockType.IMAGE_CAPTION:
  76. para_text += ' \n' + merge_para_with_text(block)
  77. elif para_type == BlockType.TABLE:
  78. if make_mode == MakeMode.NLP_MD:
  79. continue
  80. elif make_mode == MakeMode.MM_MD:
  81. for block in para_block['blocks']: # 1st.拼table_caption
  82. if block['type'] == BlockType.TABLE_CAPTION:
  83. para_text += merge_para_with_text(block) + ' \n'
  84. for block in para_block['blocks']: # 2nd.拼table_body
  85. if block['type'] == BlockType.TABLE_BODY:
  86. for line in block['lines']:
  87. for span in line['spans']:
  88. if span['type'] == ContentType.TABLE:
  89. # if processed by table model
  90. if span.get('html', ''):
  91. para_text += f"\n{span['html']}\n"
  92. elif span.get('image_path', ''):
  93. para_text += f"![]({img_buket_path}/{span['image_path']})"
  94. for block in para_block['blocks']: # 3rd.拼table_footnote
  95. if block['type'] == BlockType.TABLE_FOOTNOTE:
  96. para_text += '\n' + merge_para_with_text(block) + ' '
  97. if para_text.strip() == '':
  98. continue
  99. else:
  100. # page_markdown.append(para_text.strip() + ' ')
  101. page_markdown.append(para_text.strip())
  102. return page_markdown
  103. def make_blocks_to_content_list(para_block, img_buket_path, page_idx):
  104. para_type = para_block['type']
  105. para_content = {}
  106. if para_type in [BlockType.TEXT, BlockType.LIST, BlockType.INDEX]:
  107. para_content = {
  108. 'type': 'text',
  109. 'text': merge_para_with_text(para_block),
  110. }
  111. elif para_type == BlockType.TITLE:
  112. title_level = get_title_level(para_block)
  113. para_content = {
  114. 'type': 'text',
  115. 'text': merge_para_with_text(para_block),
  116. }
  117. if title_level != 0:
  118. para_content['text_level'] = title_level
  119. elif para_type == BlockType.INTERLINE_EQUATION:
  120. para_content = {
  121. 'type': 'equation',
  122. 'text': merge_para_with_text(para_block),
  123. 'text_format': 'latex',
  124. }
  125. elif para_type == BlockType.IMAGE:
  126. para_content = {'type': 'image', 'img_path': '', 'img_caption': [], 'img_footnote': []}
  127. for block in para_block['blocks']:
  128. if block['type'] == BlockType.IMAGE_BODY:
  129. for line in block['lines']:
  130. for span in line['spans']:
  131. if span['type'] == ContentType.IMAGE:
  132. if span.get('image_path', ''):
  133. para_content['img_path'] = f"{img_buket_path}/{span['image_path']}"
  134. if block['type'] == BlockType.IMAGE_CAPTION:
  135. para_content['img_caption'].append(merge_para_with_text(block))
  136. if block['type'] == BlockType.IMAGE_FOOTNOTE:
  137. para_content['img_footnote'].append(merge_para_with_text(block))
  138. elif para_type == BlockType.TABLE:
  139. para_content = {'type': 'table', 'img_path': '', 'table_caption': [], 'table_footnote': []}
  140. for block in para_block['blocks']:
  141. if block['type'] == BlockType.TABLE_BODY:
  142. for line in block['lines']:
  143. for span in line['spans']:
  144. if span['type'] == ContentType.TABLE:
  145. if span.get('html', ''):
  146. para_content['table_body'] = f"{span['html']}"
  147. if span.get('image_path', ''):
  148. para_content['img_path'] = f"{img_buket_path}/{span['image_path']}"
  149. if block['type'] == BlockType.TABLE_CAPTION:
  150. para_content['table_caption'].append(merge_para_with_text(block))
  151. if block['type'] == BlockType.TABLE_FOOTNOTE:
  152. para_content['table_footnote'].append(merge_para_with_text(block))
  153. para_content['page_idx'] = page_idx
  154. return para_content
  155. def union_make(pdf_info_dict: list,
  156. make_mode: str,
  157. img_buket_path: str = '',
  158. ):
  159. output_content = []
  160. for page_info in pdf_info_dict:
  161. paras_of_layout = page_info.get('para_blocks')
  162. page_idx = page_info.get('page_idx')
  163. if not paras_of_layout:
  164. continue
  165. if make_mode in [MakeMode.MM_MD, MakeMode.NLP_MD]:
  166. page_markdown = mk_blocks_to_markdown(paras_of_layout, make_mode, img_buket_path)
  167. output_content.extend(page_markdown)
  168. elif make_mode == MakeMode.CONTENT_LIST:
  169. for para_block in paras_of_layout:
  170. para_content = make_blocks_to_content_list(para_block, img_buket_path, page_idx)
  171. output_content.append(para_content)
  172. if make_mode in [MakeMode.MM_MD, MakeMode.NLP_MD]:
  173. return '\n\n'.join(output_content)
  174. elif make_mode == MakeMode.CONTENT_LIST:
  175. return output_content
  176. return None
  177. def get_title_level(block):
  178. title_level = block.get('level', 1)
  179. if title_level > 4:
  180. title_level = 4
  181. elif title_level < 1:
  182. title_level = 0
  183. return title_level