ocr_mkcontent.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. def mk_nlp_markdown(pdf_info_dict: dict):
  2. markdown = []
  3. for _, page_info in pdf_info_dict.items():
  4. blocks = page_info.get("preproc_blocks")
  5. if not blocks:
  6. continue
  7. for block in blocks:
  8. for line in block['lines']:
  9. line_text = ''
  10. for span in line['spans']:
  11. if not span.get('content'):
  12. continue
  13. content = span['content'].replace('$', '\$') # 转义$
  14. if span['type'] == 'inline_equation':
  15. content = f"${content}$"
  16. elif span['type'] == 'displayed_equation':
  17. content = f"$$\n{content}\n$$"
  18. line_text += content + ' '
  19. # 在行末添加两个空格以强制换行
  20. markdown.append(line_text.strip() + ' ')
  21. return '\n'.join(markdown)
  22. def mk_mm_markdown(pdf_info_dict: dict):
  23. markdown = []
  24. for _, page_info in pdf_info_dict.items():
  25. blocks = page_info.get("preproc_blocks")
  26. if not blocks:
  27. continue
  28. for block in blocks:
  29. for line in block['lines']:
  30. line_text = ''
  31. for span in line['spans']:
  32. if not span.get('content'):
  33. if not span.get('image_path'):
  34. continue
  35. else:
  36. content = f"![]({span['image_path']})"
  37. else:
  38. content = span['content'].replace('$', '\$') # 转义$
  39. if span['type'] == 'inline_equation':
  40. content = f"${content}$"
  41. elif span['type'] == 'displayed_equation':
  42. content = f"$$\n{content}\n$$"
  43. line_text += content + ' '
  44. # 在行末添加两个空格以强制换行
  45. markdown.append(line_text.strip() + ' ')
  46. return '\n'.join(markdown)