refactor(chunk): improve parent-child chunk relationship handling in SplitTextParentChild

- Updated the SplitTextParentChild function to maintain a newParents slice, ensuring accurate parent indexing when splitting text into child chunks.
- This change enhances the integrity of parent-child relationships by correctly associating child chunks with their respective parents, improving overall chunk processing.
This commit is contained in:
wizardchen
2026-04-16 18:28:34 +08:00
parent fdbd981868
commit 92b6b7507d
+11 -3
View File
@@ -519,10 +519,18 @@ func SplitTextParentChild(text string, parentCfg, childCfg SplitterConfig) Paren
return ParentChildResult{} return ParentChildResult{}
} }
var newParents []Chunk
var children []ChildChunk var children []ChildChunk
childSeq := 0 childSeq := 0
for pi, parent := range parents { for _, parent := range parents {
subs := SplitText(parent.Content, childCfg) subs := SplitText(parent.Content, childCfg)
parentIndex := -1
if len(subs) > 1 || (len(subs) == 1 && subs[0].Content != parent.Content) {
parentIndex = len(newParents)
newParents = append(newParents, parent)
}
for _, sub := range subs { for _, sub := range subs {
// Adjust offsets: sub positions are relative to parent content, // Adjust offsets: sub positions are relative to parent content,
// shift to document-level offsets. // shift to document-level offsets.
@@ -533,12 +541,12 @@ func SplitTextParentChild(text string, parentCfg, childCfg SplitterConfig) Paren
sub.End += parent.Start sub.End += parent.Start
children = append(children, ChildChunk{ children = append(children, ChildChunk{
Chunk: sub, Chunk: sub,
ParentIndex: pi, ParentIndex: parentIndex,
}) })
childSeq++ childSeq++
} }
} }
return ParentChildResult{Parents: parents, Children: children} return ParentChildResult{Parents: newParents, Children: children}
} }
// ExtractImageRefs extracts markdown image references from text. // ExtractImageRefs extracts markdown image references from text.