fex(kb):知识库数据清洗针对pdf含有大量图片ocr清洗超时问题优化;perf(chat):问答回复点击doc移动端无法预览问题优化
This commit is contained in:
1 parent
0890c79070
commit
900a13722c
8 files changed
+170
-18
No files matched your search
@@ -163,6 +163,26 @@ def _page_image_structured_lines(payload: str) -> list[str]:
|
||||
|
||||
|
||||
def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
def _is_meaningful_ocr_segment(text: str) -> bool:
|
||||
token = re.sub(r"\s+", "", text or "")
|
||||
if not token or re.fullmatch(r"[\W_]+", token):
|
||||
return False
|
||||
cjk_count = len(re.findall(r"[\u4e00-\u9fff]", token))
|
||||
alnum_count = len(re.findall(r"[A-Za-z0-9]", token))
|
||||
if cjk_count >= 2:
|
||||
return True
|
||||
if cjk_count >= 1 and len(token) >= 2:
|
||||
return True
|
||||
if alnum_count >= 5:
|
||||
return True
|
||||
return len(token) >= 4
|
||||
|
||||
def _is_rich_ocr_payload(payload: str) -> bool:
|
||||
token = re.sub(r"\s+", "", payload or "")
|
||||
cjk_count = len(re.findall(r"[\u4e00-\u9fff]", token))
|
||||
alnum_count = len(re.findall(r"[A-Za-z0-9]", token))
|
||||
return cjk_count >= 6 or alnum_count >= 12 or len(token) >= 20
|
||||
|
||||
def _ocr_payload(txt: str) -> str:
|
||||
payload = txt
|
||||
if payload.startswith("OCR_TEXT:"):
|
||||
@@ -170,8 +190,12 @@ def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
if not payload or payload == "<empty>":
|
||||
return ""
|
||||
payload = re.sub(r"(?m)^\s*-\s*", "", payload)
|
||||
payload = ";".join(x.strip() for x in payload.splitlines() if x.strip())
|
||||
return payload
|
||||
segments = [
|
||||
x.strip()
|
||||
for x in payload.splitlines()
|
||||
if x.strip() and _is_meaningful_ocr_segment(x.strip())
|
||||
]
|
||||
return ";".join(segments)
|
||||
|
||||
ocr_by_image: dict[str, str] = {}
|
||||
for b in blocks:
|
||||
@@ -212,9 +236,14 @@ def blocks_to_plain_markdown(blocks: list[Block]) -> str:
|
||||
and b.meta.get("page_image_source") == "pdf"
|
||||
)
|
||||
if is_pdf_page_image:
|
||||
lines.append(f"![page-image-{b.page}]")
|
||||
ocr_text = ocr_by_image.get(b.id)
|
||||
if ocr_text:
|
||||
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 "")
|
||||
if not suppress_pdf_page_image:
|
||||
lines.append(f"![page-image-{b.page}]")
|
||||
if ocr_text and not suppress_pdf_page_image:
|
||||
lines.append(f"图中文字:{ocr_text}")
|
||||
elif data_uri:
|
||||
image_idx += 1
|
||||
|
||||
Reference in new issue
Block a user