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/clean.py
|
||||
|
||||
用途:对 loader/OCR 产出的 markdown 做去重、排版与图注附着等清洗。
|
||||
依赖:edkb_system.contracts.Context。
|
||||
逻辑:由 controller 在 blocks→markdown 之后调用;需保留手册页图「图中文字」。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
@@ -907,13 +915,28 @@ def _compact_remarks_to_single_line(text: str) -> str:
|
||||
|
||||
|
||||
def _attach_ocr_lines_to_images(text: str) -> str:
|
||||
"""
|
||||
将散落的「图中文字」行重新挂到对应图片 markdown 行之后。
|
||||
|
||||
核心逻辑:抽出全部 OCR 行后,按出现顺序挂到 image/page-image 标记后;
|
||||
支持带 URL 的 `` 以及手册整页图占位 `![page-image-N]`(无 URL)。
|
||||
|
||||
Args:
|
||||
text: 清洗中的 markdown。
|
||||
|
||||
Returns:
|
||||
OCR 行已附着到图片后的 markdown;无图时原样返回。
|
||||
"""
|
||||
lines = text.splitlines()
|
||||
ocr_lines = [ln.strip() for ln in lines if ln.strip().startswith("图中文字:")]
|
||||
if not ocr_lines:
|
||||
return text
|
||||
|
||||
kept = [ln for ln in lines if not ln.strip().startswith("图中文字:")]
|
||||
img_pat = re.compile(r"^!\[(?:image|page-image)-\d+\]\((?:data:image/|https?://)")
|
||||
# 含 data/http URL 的图,以及无 URL 的 page-image 占位(手册整页 OCR)
|
||||
img_pat = re.compile(
|
||||
r"^!\[(?:image|page-image)-\d+\](?:\((?:data:image/|https?://)[^)]*\))?$"
|
||||
)
|
||||
img_count = sum(1 for ln in kept if img_pat.match(ln.strip()))
|
||||
if img_count <= 0:
|
||||
return text
|
||||
@@ -927,6 +950,10 @@ def _attach_ocr_lines_to_images(text: str) -> str:
|
||||
if img_pat.match(ln.strip()):
|
||||
out.append(ocr_lines[o_idx])
|
||||
o_idx += 1
|
||||
# 多余 OCR 行仍保留,避免手册图注被静默丢弃
|
||||
while o_idx < len(ocr_lines):
|
||||
out.append(ocr_lines[o_idx])
|
||||
o_idx += 1
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user