Verbesserung des Chunking-Parsers zur Unterstützung aller Überschriften (H1-H6) und Optimierung der Block-Trennung für atomares Sektions-Chunking. Aktualisierung der Sektions-Chunking-Strategie mit striktem Look-Ahead und präziserer Token-Schätzung für eine verbesserte Handhabung von großen Blöcken.
This commit is contained in:
1 parent
e5a34efee9
commit
b1a897e51c
2 files changed
+87
-117
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
FILE: app/core/chunking/chunking_parser.py
|
||||
DESCRIPTION: Zerlegt Markdown in Blöcke. Erhält H1-Überschriften und
|
||||
gewährleistet die Integrität von Callouts und Listen.
|
||||
DESCRIPTION: Zerlegt Markdown in logische Blöcke. Hält H1-Überschriften im Stream
|
||||
und optimiert die Block-Trennung für atomares Sektions-Chunking.
|
||||
"""
|
||||
import re
|
||||
from typing import List, Tuple, Set
|
||||
@@ -12,13 +12,14 @@ _WS = re.compile(r'\s+')
|
||||
_SENT_SPLIT = re.compile(r'(?<=[.!?])\s+(?=[A-ZÄÖÜ0-9„(])')
|
||||
|
||||
def split_sentences(text: str) -> list[str]:
|
||||
"""Teilt Text in Sätze auf."""
|
||||
"""Teilt Text in Sätze auf unter Berücksichtigung deutscher Interpunktion."""
|
||||
text = _WS.sub(' ', text.strip())
|
||||
if not text: return []
|
||||
# Splittet bei Satzzeichen, gefolgt von Leerzeichen und Großbuchstaben
|
||||
return [p.strip() for p in _SENT_SPLIT.split(text) if p.strip()]
|
||||
|
||||
def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
"""Zerlegt Text in logische Einheiten (RawBlocks), inklusive H1."""
|
||||
"""Zerlegt Text in logische Einheiten (RawBlocks), inklusive H1-H6."""
|
||||
blocks = []
|
||||
h1_title = "Dokument"
|
||||
section_path = "/"
|
||||
@@ -27,7 +28,7 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
# Frontmatter entfernen
|
||||
fm, text_without_fm = extract_frontmatter_from_text(md_text)
|
||||
|
||||
# H1 für Note-Titel extrahieren
|
||||
# H1 für Note-Metadaten extrahieren
|
||||
h1_match = re.search(r'^#\s+(.*)', text_without_fm, re.MULTILINE)
|
||||
if h1_match:
|
||||
h1_title = h1_match.group(1).strip()
|
||||
@@ -41,7 +42,7 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
# Heading-Erkennung (H1 bis H6)
|
||||
heading_match = re.match(r'^(#{1,6})\s+(.*)', stripped)
|
||||
if heading_match:
|
||||
# Vorherigen Text-Block abschließen
|
||||
# Vorherigen Block abschließen
|
||||
if buffer:
|
||||
content = "\n".join(buffer).strip()
|
||||
if content:
|
||||
@@ -51,7 +52,7 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
level = len(heading_match.group(1))
|
||||
title = heading_match.group(2).strip()
|
||||
|
||||
# Pfad- und Titel-Update für die Metadaten
|
||||
# Update der Pfad-Metadaten für die folgenden Blöcke
|
||||
if level == 1:
|
||||
current_section_title = title
|
||||
section_path = "/"
|
||||
@@ -59,11 +60,11 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
current_section_title = title
|
||||
section_path = f"/{current_section_title}"
|
||||
|
||||
# Die Überschrift als Block hinzufügen (H1 wird NICHT mehr gefiltert)
|
||||
# Die Überschrift als regulären Block hinzufügen (Fix: H1 bleibt im Text)
|
||||
blocks.append(RawBlock("heading", stripped, level, section_path, current_section_title))
|
||||
continue
|
||||
|
||||
# Trenner oder Leerzeilen beenden einen Block
|
||||
# Trenner (---) oder Leerzeilen beenden Blöcke, außer in Callouts
|
||||
if (not stripped or stripped == "---") and not line.startswith('>'):
|
||||
if buffer:
|
||||
content = "\n".join(buffer).strip()
|
||||
@@ -75,6 +76,7 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
else:
|
||||
buffer.append(line)
|
||||
|
||||
# Letzten Puffer leeren
|
||||
if buffer:
|
||||
content = "\n".join(buffer).strip()
|
||||
if content:
|
||||
@@ -85,7 +87,7 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||
def parse_edges_robust(text: str) -> Set[str]:
|
||||
"""Extrahiert Kanten-Kandidaten aus Wikilinks und Callouts."""
|
||||
found_edges = set()
|
||||
# 1. Inline Wikilinks [[rel:kind|target]]
|
||||
# 1. Wikilinks [[rel:kind|target]]
|
||||
inlines = re.findall(r'\[\[rel:([^\|\]]+)\|?([^\]]*)\]\]', text)
|
||||
for kind, target in inlines:
|
||||
k = kind.strip().lower()
|
||||
@@ -100,12 +102,10 @@ def parse_edges_robust(text: str) -> Set[str]:
|
||||
callout_match = re.match(r'>\s*\[!edge\]\s*([^:\s]+)', stripped)
|
||||
if callout_match:
|
||||
current_edge_type = callout_match.group(1).strip().lower()
|
||||
# Links in der gleichen Zeile
|
||||
links = re.findall(r'\[\[([^\]]+)\]\]', stripped)
|
||||
for l in links:
|
||||
if "rel:" not in l: found_edges.add(f"{current_edge_type}:{l}")
|
||||
continue
|
||||
# Links in Folgezeilen des Callouts
|
||||
if current_edge_type and stripped.startswith('>'):
|
||||
links = re.findall(r'\[\[([^\]]+)\]\]', stripped)
|
||||
for l in links:
|
||||
|
||||
Reference in new issue
Block a user