common.py
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from __future__ import annotations
import re
from src.rag.core.contracts import (
RAG_CHUNKER_VERSION,
RAG_PARSER_VERSION,
quality_for_text,
validate_block_meta,
)
from src.rag.ingestion.loaders.base import DocumentBlock
__all__ = [
"RAG_CHUNKER_VERSION",
"RAG_PARSER_VERSION",
"enrich_blocks",
"markdown_table_data_rows",
"markdown_table_header",
]
def enrich_blocks(
blocks: list[DocumentBlock],
*,
parse_method: str,
source_page_count: int | None = None,
) -> list[DocumentBlock]:
"""给所有 loader 产出的块补齐引擎层 metadata 契约。
全程用 setdefault:loader 已经写好的值(如 PDF 编排器拼接续表时设的跨页 page_end)
不会被覆盖。PDF 路径此前绕过本函数自造 metadata,导致契约只对非 PDF 生效。
"""
for block in blocks:
metadata = block.metadata
metadata.setdefault("parser_version", RAG_PARSER_VERSION)
metadata.setdefault("chunker_version", RAG_CHUNKER_VERSION)
metadata.setdefault("parse_method", parse_method)
metadata.setdefault("parse_status", "ok")
metadata.setdefault("parse_quality", quality_for_text(block.text))
metadata.setdefault("content_role", "table" if block.block_type == "table" else "body")
metadata.setdefault("content_kind", "table" if block.block_type == "table" else "text")
if source_page_count is not None:
metadata.setdefault("source_page_count", source_page_count)
if block.page_number is not None:
metadata.setdefault("page_start", block.page_number)
metadata.setdefault("page_end", block.page_number)
# 单值 slide_number:PPTX 一块一页,range 无意义。此前写的是 slide_start/slide_end,
# 而下游一直读 slide_number——PPTX 页码因此到不了前端。
if block.slide_number is not None:
metadata.setdefault("slide_number", block.slide_number)
if block.sheet_name:
metadata.setdefault("sheet_name", block.sheet_name)
if block.row_start is not None:
metadata.setdefault("row_start", block.row_start)
if block.row_end is not None:
metadata.setdefault("row_end", block.row_end)
if block.title_path:
section_path = [part.strip() for part in block.title_path.split("/") if part.strip()]
if section_path:
metadata.setdefault("section_path", section_path)
metadata.setdefault("section_title", section_path[-1])
if block.block_type == "table":
metadata.setdefault("table_markdown", True)
metadata.setdefault("table_header", markdown_table_header(block.text))
rows = markdown_table_data_rows(block.text)
if block.row_start is None and rows:
block.row_start = rows[0]
block.row_end = rows[-1]
metadata.setdefault("row_start", block.row_start)
metadata.setdefault("row_end", block.row_end)
metadata.setdefault("table_row_count", len(rows))
validate_block_meta(metadata, where=parse_method)
return blocks
def markdown_table_header(text: str) -> list[str]:
"""表头单元格列表。唯一实现——同一个 payload 字段此前有 list 和 str 两种类型。"""
lines = [line.strip() for line in text.splitlines() if line.strip()]
if not lines or "|" not in lines[0]:
return []
return [cell.strip() for cell in lines[0].strip("|").split("|")]
def markdown_table_data_rows(text: str) -> list[int]:
lines = [line.strip() for line in text.splitlines() if line.strip()]
rows: list[int] = []
for index, line in enumerate(lines, start=1):
if "|" not in line:
continue
cells = [cell.strip() for cell in line.strip("|").split("|")]
if not cells:
continue
if index == 1 or all(re.fullmatch(r":?-{3,}:?", cell or "") for cell in cells):
continue
rows.append(index)
return rows