增加unstructured处理策略
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
"""PDF 文件解析模块,提供三种解析方法。"""
|
||||
"""PDF 文件解析模块,提供多种解析方法。"""
|
||||
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from common import parse_with_docling, parse_with_markitdown
|
||||
from common import _unstructured_elements_to_markdown, parse_with_docling, parse_with_markitdown
|
||||
|
||||
|
||||
def parse_pdf_with_docling(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
@@ -11,41 +11,46 @@ def parse_pdf_with_docling(file_path: str) -> Tuple[Optional[str], Optional[str]
|
||||
return parse_with_docling(file_path)
|
||||
|
||||
|
||||
def parse_pdf_with_markitdown(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 MarkItDown 库解析 PDF 文件"""
|
||||
return parse_with_markitdown(file_path)
|
||||
|
||||
|
||||
def parse_pdf_with_unstructured(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 unstructured 库解析 PDF 文件"""
|
||||
"""使用 unstructured 库解析 PDF 文件,优先 hi_res 策略配合 PaddleOCR"""
|
||||
try:
|
||||
from unstructured.partition.pdf import partition_pdf
|
||||
except ImportError:
|
||||
return None, "unstructured 库未安装"
|
||||
|
||||
base_kwargs = {"filename": file_path, "infer_table_structure": True}
|
||||
|
||||
try:
|
||||
elements = partition_pdf(
|
||||
filename=file_path,
|
||||
strategy="fast",
|
||||
infer_table_structure=True,
|
||||
extract_images_in_pdf=False,
|
||||
)
|
||||
# 优先 hi_res 策略(版面分析 + PaddleOCR),失败则回退 fast
|
||||
try:
|
||||
from unstructured.partition.utils.constants import OCR_AGENT_PADDLE
|
||||
|
||||
md_lines = []
|
||||
for element in elements:
|
||||
if hasattr(element, "text") and element.text and element.text.strip():
|
||||
text = element.text.strip()
|
||||
md_lines.append(text)
|
||||
md_lines.append("")
|
||||
elements = partition_pdf(
|
||||
**base_kwargs,
|
||||
strategy="hi_res",
|
||||
languages=["chi_sim"],
|
||||
ocr_agent=OCR_AGENT_PADDLE,
|
||||
table_ocr_agent=OCR_AGENT_PADDLE,
|
||||
)
|
||||
trust_titles = True
|
||||
except Exception:
|
||||
# fast 策略不做版面分析,Title 类型标注不可靠
|
||||
elements = partition_pdf(**base_kwargs, strategy="fast", languages=["chi_sim"])
|
||||
trust_titles = False
|
||||
|
||||
content = "\n".join(md_lines).strip()
|
||||
if not content:
|
||||
content = _unstructured_elements_to_markdown(elements, trust_titles)
|
||||
if not content.strip():
|
||||
return None, "文档为空"
|
||||
return content, None
|
||||
except Exception as e:
|
||||
return None, f"unstructured 解析失败: {str(e)}"
|
||||
|
||||
|
||||
def parse_pdf_with_markitdown(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 MarkItDown 库解析 PDF 文件"""
|
||||
return parse_with_markitdown(file_path)
|
||||
|
||||
|
||||
def parse_pdf_with_pypdf(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 pypdf 库解析 PDF 文件"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user