ocr_mkcontent.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import re
  2. from loguru import logger
  3. from magic_pdf.config.make_content_config import DropMode, MakeMode
  4. from magic_pdf.config.ocr_content_type import BlockType, ContentType
  5. from magic_pdf.libs.commons import join_path
  6. from magic_pdf.libs.language import detect_lang
  7. from magic_pdf.libs.markdown_utils import ocr_escape_special_markdown_char
  8. from magic_pdf.para.para_split_v3 import ListLineTag
  9. def __is_hyphen_at_line_end(line):
  10. """Check if a line ends with one or more letters followed by a hyphen.
  11. Args:
  12. line (str): The line of text to check.
  13. Returns:
  14. bool: True if the line ends with one or more letters followed by a hyphen, False otherwise.
  15. """
  16. # Use regex to check if the line ends with one or more letters followed by a hyphen
  17. return bool(re.search(r'[A-Za-z]+-\s*$', line))
  18. def ocr_mk_mm_markdown_with_para_and_pagination(pdf_info_dict: list,
  19. img_buket_path):
  20. markdown_with_para_and_pagination = []
  21. page_no = 0
  22. for page_info in pdf_info_dict:
  23. paras_of_layout = page_info.get('para_blocks')
  24. if not paras_of_layout:
  25. continue
  26. page_markdown = ocr_mk_markdown_with_para_core_v2(
  27. paras_of_layout, 'mm', img_buket_path)
  28. markdown_with_para_and_pagination.append({
  29. 'page_no':
  30. page_no,
  31. 'md_content':
  32. '\n\n'.join(page_markdown)
  33. })
  34. page_no += 1
  35. return markdown_with_para_and_pagination
  36. def ocr_mk_markdown_with_para_core_v2(paras_of_layout,
  37. mode,
  38. img_buket_path='',
  39. ):
  40. page_markdown = []
  41. for para_block in paras_of_layout:
  42. para_text = ''
  43. para_type = para_block['type']
  44. if para_type in [BlockType.Text, BlockType.List, BlockType.Index]:
  45. para_text = merge_para_with_text(para_block)
  46. elif para_type == BlockType.Title:
  47. para_text = f'# {merge_para_with_text(para_block)}'
  48. elif para_type == BlockType.InterlineEquation:
  49. para_text = merge_para_with_text(para_block)
  50. elif para_type == BlockType.Image:
  51. if mode == 'nlp':
  52. continue
  53. elif mode == 'mm':
  54. for block in para_block['blocks']: # 1st.拼image_body
  55. if block['type'] == BlockType.ImageBody:
  56. for line in block['lines']:
  57. for span in line['spans']:
  58. if span['type'] == ContentType.Image:
  59. if span.get('image_path', ''):
  60. para_text += f"\n![]({join_path(img_buket_path, span['image_path'])}) \n"
  61. for block in para_block['blocks']: # 2nd.拼image_caption
  62. if block['type'] == BlockType.ImageCaption:
  63. para_text += merge_para_with_text(block) + ' \n'
  64. for block in para_block['blocks']: # 3rd.拼image_footnote
  65. if block['type'] == BlockType.ImageFootnote:
  66. para_text += merge_para_with_text(block) + ' \n'
  67. elif para_type == BlockType.Table:
  68. if mode == 'nlp':
  69. continue
  70. elif mode == 'mm':
  71. for block in para_block['blocks']: # 1st.拼table_caption
  72. if block['type'] == BlockType.TableCaption:
  73. para_text += merge_para_with_text(block) + ' \n'
  74. for block in para_block['blocks']: # 2nd.拼table_body
  75. if block['type'] == BlockType.TableBody:
  76. for line in block['lines']:
  77. for span in line['spans']:
  78. if span['type'] == ContentType.Table:
  79. # if processed by table model
  80. if span.get('latex', ''):
  81. para_text += f"\n\n$\n {span['latex']}\n$\n\n"
  82. elif span.get('html', ''):
  83. para_text += f"\n\n{span['html']}\n\n"
  84. elif span.get('image_path', ''):
  85. para_text += f"\n![]({join_path(img_buket_path, span['image_path'])}) \n"
  86. for block in para_block['blocks']: # 3rd.拼table_footnote
  87. if block['type'] == BlockType.TableFootnote:
  88. para_text += merge_para_with_text(block) + ' \n'
  89. if para_text.strip() == '':
  90. continue
  91. else:
  92. page_markdown.append(para_text.strip() + ' ')
  93. return page_markdown
  94. def detect_language(text):
  95. en_pattern = r'[a-zA-Z]+'
  96. en_matches = re.findall(en_pattern, text)
  97. en_length = sum(len(match) for match in en_matches)
  98. if len(text) > 0:
  99. if en_length / len(text) >= 0.5:
  100. return 'en'
  101. else:
  102. return 'unknown'
  103. else:
  104. return 'empty'
  105. # 连写字符拆分
  106. def __replace_ligatures(text: str):
  107. text = re.sub(r'fi', 'fi', text) # 替换 fi 连写符
  108. text = re.sub(r'fl', 'fl', text) # 替换 fl 连写符
  109. text = re.sub(r'ff', 'ff', text) # 替换 ff 连写符
  110. text = re.sub(r'ffi', 'ffi', text) # 替换 ffi 连写符
  111. text = re.sub(r'ffl', 'ffl', text) # 替换 ffl 连写符
  112. return text
  113. def merge_para_with_text(para_block):
  114. para_text = ''
  115. for i, line in enumerate(para_block['lines']):
  116. if i >= 1 and line.get(ListLineTag.IS_LIST_START_LINE, False):
  117. para_text += ' \n'
  118. line_text = ''
  119. line_lang = ''
  120. for span in line['spans']:
  121. span_type = span['type']
  122. if span_type == ContentType.Text:
  123. line_text += span['content'].strip()
  124. if line_text != '':
  125. line_lang = detect_lang(line_text)
  126. for span in line['spans']:
  127. span_type = span['type']
  128. content = ''
  129. if span_type == ContentType.Text:
  130. content = ocr_escape_special_markdown_char(span['content'])
  131. elif span_type == ContentType.InlineEquation:
  132. content = f"${span['content']}$"
  133. elif span_type == ContentType.InterlineEquation:
  134. content = f"\n$$\n{span['content']}\n$$\n"
  135. content = content.strip()
  136. if content != '':
  137. langs = ['zh', 'ja', 'ko']
  138. if line_lang in langs: # 遇到一些一个字一个span的文档,这种单字语言判断不准,需要用整行文本判断
  139. if span_type in [ContentType.Text, ContentType.InterlineEquation]:
  140. para_text += content # 中文/日语/韩文语境下,content间不需要空格分隔
  141. elif span_type == ContentType.InlineEquation:
  142. para_text += f' {content} '
  143. else:
  144. if span_type in [ContentType.Text, ContentType.InlineEquation]:
  145. # 如果是前一行带有-连字符,那么末尾不应该加空格
  146. if __is_hyphen_at_line_end(content):
  147. para_text += content[:-1]
  148. elif len(content) == 1 and content not in ['A', 'I', 'a', 'i'] and not content.isdigit():
  149. para_text += content
  150. else: # 西方文本语境下 content间需要空格分隔
  151. para_text += f'{content} '
  152. elif span_type == ContentType.InterlineEquation:
  153. para_text += content
  154. else:
  155. continue
  156. # 连写字符拆分
  157. para_text = __replace_ligatures(para_text)
  158. return para_text
  159. def para_to_standard_format_v2(para_block, img_buket_path, page_idx, drop_reason=None):
  160. para_type = para_block['type']
  161. para_content = {}
  162. if para_type in [BlockType.Text, BlockType.List, BlockType.Index]:
  163. para_content = {
  164. 'type': 'text',
  165. 'text': merge_para_with_text(para_block),
  166. }
  167. elif para_type == BlockType.Title:
  168. para_content = {
  169. 'type': 'text',
  170. 'text': merge_para_with_text(para_block),
  171. 'text_level': 1,
  172. }
  173. elif para_type == BlockType.InterlineEquation:
  174. para_content = {
  175. 'type': 'equation',
  176. 'text': merge_para_with_text(para_block),
  177. 'text_format': 'latex',
  178. }
  179. elif para_type == BlockType.Image:
  180. para_content = {'type': 'image', 'img_path': '', 'img_caption': [], 'img_footnote': []}
  181. for block in para_block['blocks']:
  182. if block['type'] == BlockType.ImageBody:
  183. for line in block['lines']:
  184. for span in line['spans']:
  185. if span['type'] == ContentType.Image:
  186. if span.get('image_path', ''):
  187. para_content['img_path'] = join_path(img_buket_path, span['image_path'])
  188. if block['type'] == BlockType.ImageCaption:
  189. para_content['img_caption'].append(merge_para_with_text(block))
  190. if block['type'] == BlockType.ImageFootnote:
  191. para_content['img_footnote'].append(merge_para_with_text(block))
  192. elif para_type == BlockType.Table:
  193. para_content = {'type': 'table', 'img_path': '', 'table_caption': [], 'table_footnote': []}
  194. for block in para_block['blocks']:
  195. if block['type'] == BlockType.TableBody:
  196. for line in block['lines']:
  197. for span in line['spans']:
  198. if span['type'] == ContentType.Table:
  199. if span.get('latex', ''):
  200. para_content['table_body'] = f"\n\n$\n {span['latex']}\n$\n\n"
  201. elif span.get('html', ''):
  202. para_content['table_body'] = f"\n\n{span['html']}\n\n"
  203. if span.get('image_path', ''):
  204. para_content['img_path'] = join_path(img_buket_path, span['image_path'])
  205. if block['type'] == BlockType.TableCaption:
  206. para_content['table_caption'].append(merge_para_with_text(block))
  207. if block['type'] == BlockType.TableFootnote:
  208. para_content['table_footnote'].append(merge_para_with_text(block))
  209. para_content['page_idx'] = page_idx
  210. if drop_reason is not None:
  211. para_content['drop_reason'] = drop_reason
  212. return para_content
  213. def union_make(pdf_info_dict: list,
  214. make_mode: str,
  215. drop_mode: str,
  216. img_buket_path: str = '',
  217. ):
  218. output_content = []
  219. for page_info in pdf_info_dict:
  220. drop_reason_flag = False
  221. drop_reason = None
  222. if page_info.get('need_drop', False):
  223. drop_reason = page_info.get('drop_reason')
  224. if drop_mode == DropMode.NONE:
  225. pass
  226. elif drop_mode == DropMode.NONE_WITH_REASON:
  227. drop_reason_flag = True
  228. elif drop_mode == DropMode.WHOLE_PDF:
  229. raise Exception((f'drop_mode is {DropMode.WHOLE_PDF} ,'
  230. f'drop_reason is {drop_reason}'))
  231. elif drop_mode == DropMode.SINGLE_PAGE:
  232. logger.warning((f'drop_mode is {DropMode.SINGLE_PAGE} ,'
  233. f'drop_reason is {drop_reason}'))
  234. continue
  235. else:
  236. raise Exception('drop_mode can not be null')
  237. paras_of_layout = page_info.get('para_blocks')
  238. page_idx = page_info.get('page_idx')
  239. if not paras_of_layout:
  240. continue
  241. if make_mode == MakeMode.MM_MD:
  242. page_markdown = ocr_mk_markdown_with_para_core_v2(
  243. paras_of_layout, 'mm', img_buket_path)
  244. output_content.extend(page_markdown)
  245. elif make_mode == MakeMode.NLP_MD:
  246. page_markdown = ocr_mk_markdown_with_para_core_v2(
  247. paras_of_layout, 'nlp')
  248. output_content.extend(page_markdown)
  249. elif make_mode == MakeMode.STANDARD_FORMAT:
  250. for para_block in paras_of_layout:
  251. if drop_reason_flag:
  252. para_content = para_to_standard_format_v2(
  253. para_block, img_buket_path, page_idx)
  254. else:
  255. para_content = para_to_standard_format_v2(
  256. para_block, img_buket_path, page_idx)
  257. output_content.append(para_content)
  258. if make_mode in [MakeMode.MM_MD, MakeMode.NLP_MD]:
  259. return '\n\n'.join(output_content)
  260. elif make_mode == MakeMode.STANDARD_FORMAT:
  261. return output_content