fix: explore 段落命中两级回退(原词→展开词)+ expandKeywords 去重 + HardBudget 接入
| | |
| | | if topN <= 0 && cfg.Explore.TopN > 0 { |
| | | topN = cfg.Explore.TopN |
| | | } |
| | | hardBudget := cfg.Explore.HardBudget // 0 = Explore 内部用缺省 32000 |
| | | |
| | | res, err := search.Explore(store, allKeywords, search.ExploreOptions{Budget: budget, TopN: topN}) |
| | | res, err := search.Explore(store, allKeywords, search.ExploreOptions{Budget: budget, TopN: topN, HardBudget: hardBudget}) |
| | | if err != nil { |
| | | return fmt.Errorf("explore 失败: %w", err) |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | return out |
| | | // 去重保序(原词在前、展开词在后):避免 bigram 与原词重复时 text 分重复计权 |
| | | var dedup []string |
| | | seen := make(map[string]bool, len(out)) |
| | | for _, kw := range out { |
| | | if !seen[kw] { |
| | | seen[kw] = true |
| | | dedup = append(dedup, kw) |
| | | } |
| | | } |
| | | return dedup |
| | | } |
| | | |
| | | // ExploreOptions explore 参数 |
| | | type ExploreOptions struct { |
| | | Budget int // 字节预算(0 = 用配置默认) |
| | | TopN int // 0 = 用配置默认 |
| | | HardBudget int // 字节硬上限(0 = 用代码缺省 32000) |
| | | } |
| | | |
| | | // ExploredDoc 入选文档及其输出正文 |
| | |
| | | budget = 16000 // 代码缺省(config 读取在 cmd 层完成) |
| | | } |
| | | if budget > 32000 { |
| | | budget = 32000 // 硬上限 |
| | | budget = 32000 // 硬上限缺省 |
| | | } |
| | | // 配置显式给出的硬上限优先(cmd 层读 config.yaml 的 explore.hard_budget) |
| | | if cfg.HardBudget > 0 && budget > cfg.HardBudget { |
| | | budget = cfg.HardBudget |
| | | } |
| | | topN := cfg.TopN |
| | | if topN <= 0 { |
| | |
| | | if err != nil || content == "" { |
| | | continue |
| | | } |
| | | // 两级回退:先用原词提取(精确);原词在任何段落都不连续出现时 |
| | | //(复合 CJK 词常见:bigram 召回了文档,但原词不连续)回退到展开词提取, |
| | | // 避免大文档被 continue 静默丢弃、最终输出「未找到相关文档」。 |
| | | body := extractRelevantParagraphs(content, keywords, perDoc) |
| | | if body == "" { |
| | | body = extractRelevantParagraphs(content, searchKeywords, perDoc) |
| | | } |
| | | if body == "" { |
| | | // 无命中段落但文档入选 → 整篇(若放得下),否则跳过 |
| | | if len(content) <= perDoc { |
| | | body = content |
| | |
| | | package search |
| | | |
| | | import ( |
| | | "path/filepath" |
| | | "strings" |
| | | "testing" |
| | | |
| | | "github.com/aisim/kb-cli/internal/graph" |
| | | "github.com/aisim/kb-cli/internal/index" |
| | | ) |
| | | |
| | | // TestExploreParagraphExtraction 段落截取:只输出命中关键词的段落,整段不截半句 |
| | |
| | | } |
| | | } |
| | | |
| | | // TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆) |
| | | // TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆;末尾去重保序) |
| | | func TestExploreKeywordExpansion(t *testing.T) { |
| | | got := expandKeywords([]string{"电子秤补气失败", "补气", "abc"}) |
| | | want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "补气", "abc"} |
| | | want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "abc"} |
| | | if len(got) != len(want) { |
| | | t.Fatalf("展开数量: got=%d want=%d (%v)", len(got), len(want), got) |
| | | } |
| | |
| | | t.Errorf("超预算段落应跳过: %q", got) |
| | | } |
| | | } |
| | | |
| | | // TestExploreFallbackToExpanded 回归(审查 Important):单复合 CJK 词在大文档中 |
| | | // 原文不连续出现(bigram 召回入选,但原词在任何段落都不出现)时, |
| | | // 段落截取必须回退到展开词,不能静默丢弃文档。 |
| | | func TestExploreFallbackToExpanded(t *testing.T) { |
| | | tmpDir := t.TempDir() |
| | | dbPath := filepath.Join(tmpDir, "test.db") |
| | | store, err := index.Open(dbPath) |
| | | if err != nil { |
| | | t.Fatalf("Open failed: %v", err) |
| | | } |
| | | defer store.Close() |
| | | |
| | | // 大文档(> 2000 字节预算):某段落只含「电子秤补气」片段,不含完整原词「电子秤补气失败」 |
| | | filler := strings.Repeat("填充段落内容,用于把文档撑过预算。\n", 60) |
| | | content := "# 称重故障排查\n\n" + filler + "\n## 称重故障\n\n电子秤补气 时先检查阀门,失败则断电重启。\n" |
| | | |
| | | n := &graph.Node{ |
| | | ID: 1, |
| | | Path: "FAQ/称重/001-test.md", |
| | | Title: "称重故障排查", |
| | | Section: "FAQ", |
| | | Content: content, |
| | | } |
| | | if _, err := store.InsertNode(n); err != nil { |
| | | t.Fatalf("InsertNode failed: %v", err) |
| | | } |
| | | if err := store.CreateFTS(); err != nil { |
| | | t.Fatalf("CreateFTS failed: %v", err) |
| | | } |
| | | if err := store.PopulateFTS(); err != nil { |
| | | t.Fatalf("PopulateFTS failed: %v", err) |
| | | } |
| | | |
| | | // 原词提取必为空(内容里没有完整的「电子秤补气失败」) |
| | | if b := extractRelevantParagraphs(content, []string{"电子秤补气失败"}, 2000); b != "" { |
| | | t.Fatalf("前提不成立:原词提取应为空,got=%q", b) |
| | | } |
| | | |
| | | res, err := Explore(store, []string{"电子秤补气失败"}, ExploreOptions{Budget: 2000, TopN: 5}) |
| | | if err != nil { |
| | | t.Fatalf("Explore failed: %v", err) |
| | | } |
| | | if len(res.Docs) != 1 { |
| | | t.Fatalf("应召回 1 篇文档(bigram 命中),got=%d", len(res.Docs)) |
| | | } |
| | | if !strings.Contains(res.Docs[0].Body, "电子秤补气") { |
| | | t.Errorf("回退展开词后应提取含片段段落: body=%q", res.Docs[0].Body) |
| | | } |
| | | } |