common.py
3.61 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
from __future__ import annotations
import re
from typing import Any
from src.rag.loaders.base import DocumentBlock
RAG_PARSER_VERSION = "rag-parser-v2"
RAG_CHUNKER_VERSION = "rag-chunker-v2"
def enrich_blocks(
blocks: list[DocumentBlock],
*,
parse_method: str,
source_page_count: int | None = None,
) -> list[DocumentBlock]:
"""Apply the common RAG loader metadata contract in-place."""
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)
if block.slide_number is not None:
metadata.setdefault("slide_start", block.slide_number)
metadata.setdefault("slide_end", 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))
return blocks
def markdown_table_header(text: str) -> 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
def _quality_for_text(text: str) -> str:
stripped = (text or "").strip()
if not stripped:
return "empty"
replacement_ratio = stripped.count("\ufffd") / max(len(stripped), 1)
if replacement_ratio > 0.02:
return "garbled"
if len(stripped) < 12:
return "short"
return "normal"