fix(rerank): preserve code and math passages
parent
31a2358bbf
commit
60028cbe0e
|
|
@ -559,10 +559,10 @@ var (
|
|||
reMarkdownLink = regexp.MustCompile(`\[([^\]]+)\]\([^()\s]*(?:\([^)]*\)[^()\s]*)*\)`)
|
||||
// reRawURL matches standalone http(s) URLs.
|
||||
reRawURL = regexp.MustCompile(`https?://[^\s)\]>]+`)
|
||||
// reCodeBlock matches fenced code blocks (```...```).
|
||||
reCodeBlock = regexp.MustCompile("(?s)```(?:\\w*)\n?.*?```")
|
||||
// reLatexBlock matches block-level LaTeX ($$...$$).
|
||||
reLatexBlock = regexp.MustCompile(`(?s)\$\$.*?\$\$`)
|
||||
// reCodeBlock captures the semantic body of fenced code blocks.
|
||||
reCodeBlock = regexp.MustCompile("(?s)```[^\\r\\n]*\\r?\\n(.*?)\\r?\\n?```")
|
||||
// reLatexBlock captures the semantic body of block-level LaTeX ($$...$$).
|
||||
reLatexBlock = regexp.MustCompile(`(?s)\$\$(.*?)\$\$`)
|
||||
// reTableSep matches table separator rows like |---|---|.
|
||||
// Uses [ \t] instead of \s to avoid consuming newlines across rows.
|
||||
reTableSep = regexp.MustCompile(`(?m)^[ \t]*\|[ \t:|-]+\|[ \t]*$`)
|
||||
|
|
@ -589,13 +589,13 @@ var (
|
|||
|
||||
// cleanPassageForRerank strips markdown/structural noise from text to produce
|
||||
// a clean semantic passage for the rerank model. The cleaning is designed to
|
||||
// preserve all meaningful natural-language content while removing formatting
|
||||
// preserve all meaningful semantic content while removing formatting
|
||||
// that would confuse text-similarity scoring.
|
||||
func cleanPassageForRerank(text string) string {
|
||||
// 1. Remove code blocks (before other patterns to avoid partial matches)
|
||||
text = reCodeBlock.ReplaceAllString(text, "")
|
||||
// 2. Remove LaTeX block math
|
||||
text = reLatexBlock.ReplaceAllString(text, "")
|
||||
// 1. Unwrap code blocks so code-only candidates remain rerankable.
|
||||
text = reCodeBlock.ReplaceAllString(text, "$1")
|
||||
// 2. Unwrap LaTeX blocks so formula-only candidates remain rerankable.
|
||||
text = reLatexBlock.ReplaceAllString(text, "$1")
|
||||
// 3. Remove HTML tags
|
||||
text = reHTMLTag.ReplaceAllString(text, "")
|
||||
// 3.5. Unwrap nested [](link_url) → 
|
||||
|
|
|
|||
|
|
@ -29,6 +29,37 @@ func TestGetEnrichedPassageKeepsQuestionsFromEarlierRevision(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGetEnrichedPassageKeepsCodeAndMathCandidates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "code only",
|
||||
content: "```go\nfunc answer() int { return 42 }\n```",
|
||||
want: "func answer() int { return 42 }",
|
||||
},
|
||||
{
|
||||
name: "math only",
|
||||
content: "$$\nE = mc^2\n$$",
|
||||
want: "E = mc^2",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
passage := getEnrichedPassage(context.Background(), &types.SearchResult{Content: tt.content})
|
||||
if strings.TrimSpace(passage) == "" {
|
||||
t.Fatal("semantic-only candidate was removed from the rerank passage")
|
||||
}
|
||||
if !strings.Contains(passage, tt.want) {
|
||||
t.Fatalf("rerank passage %q does not preserve %q", passage, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanPassageForRerank(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -56,14 +87,14 @@ func TestCleanPassageForRerank(t *testing.T) {
|
|||
expect: "访问 获取更多信息",
|
||||
},
|
||||
{
|
||||
name: "remove code blocks",
|
||||
name: "unwrap code blocks",
|
||||
input: "示例代码:\n```python\nprint('hello')\n```\n以上是示例",
|
||||
expect: "示例代码:\n\n以上是示例",
|
||||
expect: "示例代码:\nprint('hello')\n以上是示例",
|
||||
},
|
||||
{
|
||||
name: "remove LaTeX blocks",
|
||||
name: "unwrap LaTeX blocks",
|
||||
input: "公式如下 $$E=mc^2$$ 其中E是能量",
|
||||
expect: "公式如下 其中E是能量",
|
||||
expect: "公式如下 E=mc^2 其中E是能量",
|
||||
},
|
||||
{
|
||||
name: "remove table separator rows and convert data rows",
|
||||
|
|
@ -114,7 +145,7 @@ func TestCleanPassageForRerank(t *testing.T) {
|
|||
- 功能二
|
||||
|
||||
` + "```json\n{\"key\": \"value\"}\n```",
|
||||
expect: "产品介绍\n\n这是一个 重要的 产品。详见 产品页面。\n\n用户评价:非常好用\n\n功能一\n功能二",
|
||||
expect: "产品介绍\n\n这是一个 重要的 产品。详见 产品页面。\n\n用户评价:非常好用\n\n功能一\n功能二\n\n{\"key\": \"value\"}",
|
||||
},
|
||||
{
|
||||
name: "convert table data rows to plain text",
|
||||
|
|
|
|||
Loading…
Reference in New Issue