ocr_mkcontent.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. import wordninja
  5. import re
  6. def split_long_words(text):
  7. segments = text.split(' ')
  8. for i in range(len(segments)):
  9. words = re.findall(r'\w+|[^\w\s]', segments[i], re.UNICODE)
  10. for j in range(len(words)):
  11. if len(words[j]) > 15:
  12. words[j] = ' '.join(wordninja.split(words[j]))
  13. segments[i] = ''.join(words)
  14. return ' '.join(segments)
  15. def ocr_mk_nlp_markdown(pdf_info_dict: dict):
  16. markdown = []
  17. for _, page_info in pdf_info_dict.items():
  18. blocks = page_info.get("preproc_blocks")
  19. if not blocks:
  20. continue
  21. for block in blocks:
  22. for line in block['lines']:
  23. line_text = ''
  24. for span in line['spans']:
  25. if not span.get('content'):
  26. continue
  27. content = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  28. if span['type'] == ContentType.InlineEquation:
  29. content = f"${content}$"
  30. elif span['type'] == ContentType.InterlineEquation:
  31. content = f"$$\n{content}\n$$"
  32. line_text += content + ' '
  33. # 在行末添加两个空格以强制换行
  34. markdown.append(line_text.strip() + ' ')
  35. return '\n'.join(markdown)
  36. def ocr_mk_mm_markdown(pdf_info_dict: dict):
  37. markdown = []
  38. for _, page_info in pdf_info_dict.items():
  39. blocks = page_info.get("preproc_blocks")
  40. if not blocks:
  41. continue
  42. for block in blocks:
  43. for line in block['lines']:
  44. line_text = ''
  45. for span in line['spans']:
  46. if not span.get('content'):
  47. if not span.get('image_path'):
  48. continue
  49. else:
  50. content = f"![]({join_path(s3_image_save_path, span['image_path'])})"
  51. else:
  52. content = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  53. if span['type'] == ContentType.InlineEquation:
  54. content = f"${content}$"
  55. elif span['type'] == ContentType.InterlineEquation:
  56. content = f"$$\n{content}\n$$"
  57. line_text += content + ' '
  58. # 在行末添加两个空格以强制换行
  59. markdown.append(line_text.strip() + ' ')
  60. return '\n'.join(markdown)
  61. def ocr_mk_mm_markdown_with_para(pdf_info_dict: dict):
  62. markdown = []
  63. for _, page_info in pdf_info_dict.items():
  64. paras_of_layout = page_info.get("para_blocks")
  65. if not paras_of_layout:
  66. continue
  67. for paras in paras_of_layout:
  68. for para in paras:
  69. para_text = ''
  70. for line in para:
  71. for span in line['spans']:
  72. span_type = span.get('type')
  73. if span_type == ContentType.Text:
  74. content = split_long_words(span['content'])
  75. pass
  76. elif span_type == ContentType.InlineEquation:
  77. content = f" ${span['content']}$ "
  78. elif span_type == ContentType.InterlineEquation:
  79. content = f"\n$$\n{span['content']}\n$$\n"
  80. elif span_type in [ ContentType.Image, ContentType.Table ]:
  81. content = f"\n![]({join_path(s3_image_save_path, span['image_path'])})\n"
  82. para_text += content + ' '
  83. markdown.append(para_text.strip() + ' ')
  84. return '\n\n'.join(markdown)
  85. def ocr_mk_mm_markdown_with_para_and_pagination(pdf_info_dict: dict):
  86. markdown_with_para_and_pagination = []
  87. for page_no, page_info in pdf_info_dict.items():
  88. page_markdown = []
  89. paras_of_layout = page_info.get("para_blocks")
  90. if not paras_of_layout:
  91. continue
  92. for paras in paras_of_layout:
  93. for para in paras:
  94. para_text = ''
  95. for line in para:
  96. for span in line['spans']:
  97. span_type = span.get('type')
  98. if span_type == ContentType.Text:
  99. content = split_long_words(span['content'])
  100. # content = span['content']
  101. elif span_type == ContentType.InlineEquation:
  102. content = f"${span['content']}$"
  103. elif span_type == ContentType.InterlineEquation:
  104. content = f"\n$$\n{span['content']}\n$$\n"
  105. elif span_type in [ContentType.Image, ContentType.Table]:
  106. content = f"\n![]({join_path(s3_image_save_path, span['image_path'])})\n"
  107. para_text += content + ' '
  108. page_markdown.append(para_text.strip() + ' ')
  109. markdown_with_para_and_pagination.append({
  110. 'page_no': page_no,
  111. 'md_content': '\n\n'.join(page_markdown)
  112. })
  113. return markdown_with_para_and_pagination
  114. def make_standard_format_with_para(pdf_info_dict: dict):
  115. content_list = []
  116. for _, page_info in pdf_info_dict.items():
  117. paras = page_info.get("para_blocks")
  118. if not paras:
  119. continue
  120. for para in paras:
  121. for line in para:
  122. content = line_to_standard_format(line)
  123. content_list.append(content)
  124. return content_list
  125. def line_to_standard_format(line):
  126. line_text = ""
  127. inline_equation_num = 0
  128. for span in line['spans']:
  129. if not span.get('content'):
  130. if not span.get('image_path'):
  131. continue
  132. else:
  133. if span['type'] == ContentType.Image:
  134. content = {
  135. 'type': 'image',
  136. 'img_path': join_path(s3_image_save_path, span['image_path'])
  137. }
  138. return content
  139. elif span['type'] == ContentType.Table:
  140. content = {
  141. 'type': 'table',
  142. 'img_path': join_path(s3_image_save_path, span['image_path'])
  143. }
  144. return content
  145. else:
  146. if span['type'] == ContentType.InterlineEquation:
  147. interline_equation = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  148. content = {
  149. 'type': 'equation',
  150. 'latex': f"$$\n{interline_equation}\n$$"
  151. }
  152. return content
  153. elif span['type'] == ContentType.InlineEquation:
  154. inline_equation = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  155. line_text += f"${inline_equation}$"
  156. inline_equation_num += 1
  157. elif span['type'] == ContentType.Text:
  158. text_content = ocr_escape_special_markdown_char(span['content']) # 转义特殊符号
  159. line_text += text_content
  160. content = {
  161. 'type': 'text',
  162. 'text': line_text,
  163. 'inline_equation_num': inline_equation_num
  164. }
  165. return content
  166. def ocr_mk_mm_standard_format(pdf_info_dict: dict):
  167. '''
  168. content_list
  169. type string image/text/table/equation(行间的单独拿出来,行内的和text合并)
  170. latex string latex文本字段。
  171. text string 纯文本格式的文本数据。
  172. md string markdown格式的文本数据。
  173. img_path string s3://full/path/to/img.jpg
  174. '''
  175. content_list = []
  176. for _, page_info in pdf_info_dict.items():
  177. blocks = page_info.get("preproc_blocks")
  178. if not blocks:
  179. continue
  180. for block in blocks:
  181. for line in block['lines']:
  182. content = line_to_standard_format(line)
  183. content_list.append(content)
  184. return content_list