Verbesserung des Chunking-Parsers zur Unterstützung von H1-Überschriften und Anpassung der Metadatenlogik. Implementierung einer atomaren Sektions-Chunking-Strategie, die Überschriften und deren Inhalte zusammenhält.
This commit is contained in:
1 parent
8f5eb36b5f
commit
838083b909
2 files changed
+61
-48
No files matched your search
@@ -20,7 +20,8 @@ def _create_context_win(doc_title: str, sec_title: Optional[str], text: str) ->
|
||||
|
||||
def strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id: str, doc_title: str = "") -> List[Chunk]:
|
||||
"""
|
||||
Splittet Text basierend auf Markdown-Überschriften mit atomarem Block-Erhalt.
|
||||
Implementiert atomares Sektions-Chunking.
|
||||
Hält Überschriften und ihren Inhalt (inkl. Edges) zusammen.
|
||||
"""
|
||||
strict = config.get("strict_heading_split", False)
|
||||
target = config.get("target", 400)
|
||||
@@ -45,23 +46,21 @@ def strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id:
|
||||
def _flush():
|
||||
nonlocal buf, cur_tokens
|
||||
if not buf: return
|
||||
|
||||
# Metadaten stammen immer vom ersten Block im Puffer (meist die Überschrift)
|
||||
main_title = buf[0].section_title
|
||||
main_path = buf[0].section_path
|
||||
full_text = "\n\n".join([b.text for b in buf])
|
||||
|
||||
# Falls der gesamte Puffer in einen Chunk passt
|
||||
if estimate_tokens(full_text) <= max_tokens:
|
||||
_add_to_chunks(full_text, main_title, main_path)
|
||||
else:
|
||||
# Nur wenn ein einzelner Abschnitt größer als 'max' ist, wird intern gesplittet
|
||||
# Fallback: Nur wenn eine Sektion ALLEINE zu groß ist, wird intern gesplittet
|
||||
sents = split_sentences(full_text)
|
||||
cur_sents = []; sub_len = 0
|
||||
for s in sents:
|
||||
slen = estimate_tokens(s)
|
||||
if sub_len + slen > target and cur_sents:
|
||||
_add_to_chunks(" ".join(cur_sents), main_title, main_path)
|
||||
# Overlap-Logik...
|
||||
ov_s = []; ov_l = 0
|
||||
for os in reversed(cur_sents):
|
||||
if ov_l + estimate_tokens(os) < overlap:
|
||||
@@ -70,34 +69,43 @@ def strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id:
|
||||
cur_sents = list(ov_s); cur_sents.append(s); sub_len = ov_l + slen
|
||||
else: cur_sents.append(s); sub_len += slen
|
||||
if cur_sents: _add_to_chunks(" ".join(cur_sents), main_title, main_path)
|
||||
|
||||
buf = []; cur_tokens = 0
|
||||
|
||||
# SCHRITT 1: Gruppierung in atomare Sektions-Einheiten
|
||||
sections = []
|
||||
curr_sec = []
|
||||
for b in blocks:
|
||||
b_tokens = estimate_tokens(b.text)
|
||||
|
||||
# Prüfung auf Split-Trigger (Überschriften)
|
||||
is_split_trigger = False
|
||||
if b.kind == "heading":
|
||||
if b.level < split_level:
|
||||
is_split_trigger = True
|
||||
elif b.level == split_level:
|
||||
if strict or cur_tokens >= target:
|
||||
is_split_trigger = True
|
||||
|
||||
if is_split_trigger:
|
||||
_flush() # Vorherigen Puffer leeren
|
||||
buf.append(b) # Neue Überschrift in den neuen Puffer aufnehmen
|
||||
cur_tokens = b_tokens
|
||||
# Ein Split-Trigger startet eine neue Sektion
|
||||
if b.kind == "heading" and b.level <= split_level:
|
||||
if curr_sec: sections.append(curr_sec)
|
||||
curr_sec = [b]
|
||||
else:
|
||||
# Atomarer Check: Wenn der neue Block den aktuellen Chunk sprengen würde
|
||||
if cur_tokens + b_tokens > max_tokens and buf:
|
||||
_flush() # Puffer leeren, Block 'b' wird Teil des nächsten Chunks
|
||||
|
||||
buf.append(b)
|
||||
cur_tokens += b_tokens
|
||||
curr_sec.append(b)
|
||||
if curr_sec: sections.append(curr_sec)
|
||||
|
||||
_flush() # Letzten Puffer leeren
|
||||
# SCHRITT 2: Verarbeitung der Sektionen mit Vorausschau
|
||||
for sec in sections:
|
||||
sec_tokens = sum(estimate_tokens(b.text) for b in sec)
|
||||
|
||||
if buf:
|
||||
# PRÜFUNG: Passt die gesamte Sektion noch in den aktuellen Chunk?
|
||||
if cur_tokens + sec_tokens > max_tokens:
|
||||
_flush()
|
||||
# PRÜFUNG: Harter Split gefordert?
|
||||
elif strict:
|
||||
_flush()
|
||||
# PRÜFUNG: Weicher Split (Target erreicht)?
|
||||
elif cur_tokens >= target:
|
||||
_flush()
|
||||
|
||||
buf.extend(sec)
|
||||
cur_tokens += sec_tokens
|
||||
|
||||
# Falls die Sektion selbst das Limit sprengt, sofort flashen
|
||||
if cur_tokens >= max_tokens:
|
||||
_flush()
|
||||
|
||||
_flush()
|
||||
return chunks
|
||||
|
||||
def strategy_sliding_window(blocks: List[RawBlock],
|
||||
|
||||
Reference in new issue
Block a user