Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(chunker): require space after English ? and ! in overlap boundaries

wizardchenc193b949aac8
diff --git a/internal/infrastructure/chunker/splitter.go b/internal/infrastructure/chunker/splitter.go
index 0820bc7f3..bf170f46b 100644
--- a/internal/infrastructure/chunker/splitter.go
+++ b/internal/infrastructure/chunker/splitter.go
@@ -593,7 +593,7 @@ func buildChunk(units []splitUnit, seq int) Chunk {
//
// 1. paragraph break (\n\n / \r\n\r\n)
// 2. line break (\n / \r\n)
-// 3. sentence end (。?!, ". ", and English ?/!)
+// 3. sentence end (。?!, ". ", "? ", "! ")
//
// Priority wins first; for boundaries with the same priority, the earliest
// one in the window wins so the useful overlap is as large as possible. The
@@ -796,12 +796,10 @@ func findSemanticOverlapBoundaryEndingAtOrAfter(text string, minEnd int) (int, b
switch runes[i] {
case '。', '?', '!':
consider(i, i+1, 3)
- case '.':
+ case '.', '?', '!':
if i+1 < len(runes) && runes[i+1] == ' ' {
consider(i, i+2, 3)
}
- case '?', '!':
- consider(i, i+1, 3)
}
}
diff --git a/internal/infrastructure/chunker/splitter_test.go b/internal/infrastructure/chunker/splitter_test.go
index b71d44e40..02d2f62a0 100644
--- a/internal/infrastructure/chunker/splitter_test.go
+++ b/internal/infrastructure/chunker/splitter_test.go
@@ -317,13 +317,13 @@ func TestFindSemanticOverlapBoundary_PriorityThenEarliest(t *testing.T) {
want: "Second sentence",
},
{
- name: "english question mark needs no following space",
- text: "Question?Answer",
+ name: "english question mark requires following space",
+ text: "Question? Answer",
want: "Answer",
},
{
- name: "english exclamation mark needs no following space",
- text: "Warning!Continue",
+ name: "english exclamation mark requires following space",
+ text: "Warning! Continue",
want: "Continue",
},
}
@@ -348,6 +348,7 @@ func TestFindSemanticOverlapBoundary_NoConfiguredSemanticSeparator(t *testing.T)
"只有,逗号;分号:冒号",
"version1.2 remains one unit",
"address 192.168.1.1 remains one unit",
+ "see https://ex.com?q=1&foo=bar",
} {
if end, ok := findSemanticOverlapBoundary(text); ok {
t.Errorf("findSemanticOverlapBoundary(%q) returned boundary at %d", text, end)
@@ -382,7 +383,7 @@ func TestFindSemanticOverlapBoundary_FiltersEligibilityBeforePriorityAndPosition
},
{
name: "ineligible paragraph does not outrank eligible sentence",
- text: "\n\nx?tail",
+ text: "\n\nx? tail",
minEnd: 3,
want: "tail",
},
@@ -485,6 +486,31 @@ func TestComputeOverlap_LookbehindBoundaryEligibility(t *testing.T) {
}
}
+func TestSplitText_OverlapDoesNotBreakURLQuery(t *testing.T) {
+ line1 := strings.Repeat("a", 35)
+ text := line1 + "\n" + "https://ex.com?q=1 tail\n" + "more content here"
+ cfg := SplitterConfig{ChunkSize: 40, ChunkOverlap: 10, Separators: []string{"\n\n", "\n"}}
+
+ chunks := SplitText(text, cfg)
+ if len(chunks) < 3 {
+ t.Fatalf("expected at least 3 chunks, got %d: %#v", len(chunks), chunks)
+ }
+ if strings.HasPrefix(chunks[2].Content, "q=1") {
+ t.Fatalf("overlap broke at URL query string: chunk[2]=%q", chunks[2].Content)
+ }
+}
+
+func TestBuildUnitsWithProtection_InlineCodeNotSplit(t *testing.T) {
+ text := "intro `code.here` suffix"
+ units := buildUnitsWithProtection(text, protectedSpans(text), []string{"\n"}, 0)
+
+ for _, u := range units {
+ if strings.Contains(u.text, "code.here") && !strings.Contains(u.text, "`code.here`") {
+ t.Fatalf("inline code period split unit: %q", u.text)
+ }
+ }
+}
+
func TestSplitText_SemanticOverlapInsideParagraph(t *testing.T) {
first := strings.Repeat("甲", 15) + "。尾巴"
text := first + "\n\n" + "后续内容"