ocr_mkcontent.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 mk_mm_markdown2(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 ocr_mk_mm_standard_format():
  72. '''
  73. content_list
  74. type string image/text/table/equation(行间的单独拿出来,行内的和text合并)
  75. '''
  76. pass