fix(knowdge-base): 手册/设计类 PDF 不抑制图片,并对保留图片跑 OCR,把 图中文写进md再同步
This commit is contained in:
1 parent
64205b72bd
commit
0939aeb0d8
5 files changed
+626
-17
No files matched your search
@@ -1,3 +1,11 @@
|
||||
"""
|
||||
edkb_system/skills/common.py
|
||||
|
||||
用途:edkb 清洗链路的通用工具(哈希、OCR 载荷整理、blocks→markdown)。
|
||||
依赖:edkb_system.contracts.Block。
|
||||
逻辑:controller / export / loader 等技能复用;手册类图文保留策略也集中在此判定。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
@@ -9,6 +17,45 @@ from typing import Any
|
||||
|
||||
from edkb_system.contracts import Block
|
||||
|
||||
# 机械设计手册等图文密集文档:清洗时需保留图片并写入 OCR「图中文字」供问答检索。
|
||||
_FIGURE_RICH_HANDBOOK_KEYWORDS = (
|
||||
"手册",
|
||||
"圖冊",
|
||||
"图册",
|
||||
"图集",
|
||||
"圖集",
|
||||
"机械设计",
|
||||
"機械設計",
|
||||
"handbook",
|
||||
"design manual",
|
||||
)
|
||||
|
||||
|
||||
def is_figure_rich_handbook(name: str) -> bool:
|
||||
"""
|
||||
判断文件名是否像「图文密集手册/设计手册」。
|
||||
|
||||
核心逻辑:文件名命中手册/图册/机械设计等关键词时返回 True,供 loader/controller
|
||||
放宽嵌图保留、跳过 policy_text 丢图、并强制 OCR。
|
||||
|
||||
Args:
|
||||
name: 文件名或 stem(可含扩展名)。
|
||||
|
||||
Returns:
|
||||
True 表示应按图文手册策略处理。
|
||||
"""
|
||||
text = str(name or "").strip()
|
||||
if not text:
|
||||
return False
|
||||
lowered = text.lower()
|
||||
for keyword in _FIGURE_RICH_HANDBOOK_KEYWORDS:
|
||||
if keyword.isascii():
|
||||
if keyword in lowered:
|
||||
return True
|
||||
elif keyword in text:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def hash_text(text: str) -> str:
|
||||
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
@@ -162,7 +209,23 @@ def _page_image_structured_lines(payload: str) -> list[str]:
|
||||
return lines
|
||||
|
||||
|
||||
def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
def blocks_to_plain_markdown(
|
||||
blocks: list[Block], *, keep_figure_ocr: bool = False
|
||||
) -> str:
|
||||
"""
|
||||
将 block 列表转为纯文本 markdown(含可选图片与「图中文字」OCR)。
|
||||
|
||||
核心逻辑:按页排序输出 heading/paragraph/image;PDF 整页图在正文较多时默认抑制,
|
||||
但 keep_figure_ocr=True(手册类)时始终保留 OCR 文案供问答检索。
|
||||
|
||||
Args:
|
||||
blocks: 清洗后的内容块。
|
||||
keep_figure_ocr: 为 True 时不因页内正文较多而丢弃 PDF 页图 OCR。
|
||||
|
||||
Returns:
|
||||
markdown 字符串(末尾带换行)。
|
||||
"""
|
||||
|
||||
def _is_meaningful_ocr_segment(text: str) -> bool:
|
||||
token = re.sub(r"\s+", "", text or "")
|
||||
if not token or re.fullmatch(r"[\W_]+", token):
|
||||
@@ -237,10 +300,15 @@ def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
)
|
||||
if is_pdf_page_image:
|
||||
ocr_text = ocr_by_image.get(b.id)
|
||||
# [STEP] 手册类保留页图 OCR;普通 PDF 仍避免与正文重复的整页图噪声
|
||||
suppress_pdf_page_image = (
|
||||
page_body_chars.get(b.page, 0) >= 80
|
||||
or page_text_chars.get(b.page, 0) >= 120
|
||||
) and not _is_rich_ocr_payload(ocr_text or "")
|
||||
not keep_figure_ocr
|
||||
and (
|
||||
page_body_chars.get(b.page, 0) >= 80
|
||||
or page_text_chars.get(b.page, 0) >= 120
|
||||
)
|
||||
and not _is_rich_ocr_payload(ocr_text or "")
|
||||
)
|
||||
if not suppress_pdf_page_image:
|
||||
lines.append(f"![page-image-{b.page}]")
|
||||
if ocr_text and not suppress_pdf_page_image:
|
||||
@@ -253,11 +321,13 @@ def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
lines.append(f"")
|
||||
ocr_text = ocr_by_image.get(b.id)
|
||||
if ocr_text:
|
||||
suppress_pptx_ocr = bool(
|
||||
b.meta and b.meta.get("page_image_source") == "pptx"
|
||||
) and (
|
||||
page_body_chars.get(b.page, 0) >= 15
|
||||
or page_text_chars.get(b.page, 0) >= 40
|
||||
suppress_pptx_ocr = (
|
||||
not keep_figure_ocr
|
||||
and bool(b.meta and b.meta.get("page_image_source") == "pptx")
|
||||
and (
|
||||
page_body_chars.get(b.page, 0) >= 15
|
||||
or page_text_chars.get(b.page, 0) >= 40
|
||||
)
|
||||
)
|
||||
if b.meta and b.meta.get("page_image"):
|
||||
lines.extend(_page_image_structured_lines(ocr_text))
|
||||
@@ -265,6 +335,9 @@ def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
lines.append(f"图中文字:{ocr_text}")
|
||||
else:
|
||||
lines.append("![image]")
|
||||
ocr_text = ocr_by_image.get(b.id)
|
||||
if ocr_text and keep_figure_ocr:
|
||||
lines.append(f"图中文字:{ocr_text}")
|
||||
else:
|
||||
lines.append(txt)
|
||||
return "\n\n".join(lines).strip() + "\n"
|
||||
|
||||
Reference in new issue
Block a user