59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
"""
|
|
FILE: app/core/chunking/chunking_propagation.py
|
|
DESCRIPTION: Injiziert Sektions-Kanten physisch in den Text (Embedding-Enrichment).
|
|
Stellt die "Gold-Standard"-Qualität von v3.1.0 wieder her.
|
|
VERSION: 3.3.1
|
|
STATUS: Active
|
|
"""
|
|
from typing import List, Dict, Set
|
|
from .chunking_models import Chunk
|
|
from .chunking_parser import parse_edges_robust
|
|
|
|
def propagate_section_edges(chunks: List[Chunk]) -> List[Chunk]:
|
|
"""
|
|
Sammelt Kanten pro Sektion und schreibt sie hart in den Text und das Window.
|
|
Dies ist essenziell für die Vektorisierung der Beziehungen.
|
|
"""
|
|
# 1. Sammeln: Alle expliziten Kanten pro Sektions-Pfad aggregieren
|
|
section_map: Dict[str, Set[str]] = {} # path -> set(kind:target)
|
|
|
|
for ch in chunks:
|
|
# Root-Level "/" ignorieren (zu global), Fokus auf spezifische Kapitel
|
|
if not ch.section_path or ch.section_path == "/":
|
|
continue
|
|
|
|
# Nutzt den robusten Parser aus dem Package
|
|
edges = parse_edges_robust(ch.text)
|
|
if edges:
|
|
if ch.section_path not in section_map:
|
|
section_map[ch.section_path] = set()
|
|
section_map[ch.section_path].update(edges)
|
|
|
|
# 2. Injizieren: Kanten in jeden Chunk der Sektion zurückschreiben (Broadcasting)
|
|
for ch in chunks:
|
|
if ch.section_path in section_map:
|
|
edges_to_add = section_map[ch.section_path]
|
|
if not edges_to_add:
|
|
continue
|
|
|
|
injections = []
|
|
for e_str in edges_to_add:
|
|
kind, target = e_str.split(':', 1)
|
|
# Nur injizieren, wenn die Kante nicht bereits im Text steht
|
|
token = f"[[rel:{kind}|{target}]]"
|
|
if token not in ch.text:
|
|
injections.append(token)
|
|
|
|
if injections:
|
|
# Physische Anreicherung (Der v3.1.0 Qualitäts-Fix)
|
|
# Triple-Newline für saubere Trennung im Embedding-Fenster
|
|
block = "\n\n\n" + " ".join(injections)
|
|
ch.text += block
|
|
|
|
# ENTSCHEIDEND: Auch ins Window schreiben, da Qdrant hier sucht!
|
|
if ch.window:
|
|
ch.window += block
|
|
else:
|
|
ch.window = ch.text
|
|
|
|
return chunks |