document.py 503 B

123456789101112131415161718
  1. """文档加载和文本清洗。"""
  2. import re
  3. from langchain_community.document_loaders import PyMuPDFLoader
  4. from langchain_core.documents import Document
  5. def load_pdf_document(pdf_path: str) -> list[Document]:
  6. """按页加载 PDF 文档。"""
  7. return PyMuPDFLoader(pdf_path).load()
  8. def clean_pdf_text(text: str) -> str:
  9. """规整 PDF 提取文本中的空白字符。"""
  10. text = re.sub(r"[\t\r\f\v ]+", " ", text)
  11. text = re.sub(r"\n{3,}", "\n\n", text)
  12. return text.strip()