package utils import ( "fmt" ) // TestFileMatching 测试文件匹配功能 func TestFileMatching() { fmt.Println("=== 文件匹配测试 ===") // 模拟数据库中的文件列表 mockFiles := []struct { fileName string filePath string }{ {"安全生产管理制度.pdf", "/files/safety/安全生产管理制度.pdf"}, {"消防安全管理规定.docx", "/files/fire/消防安全管理规定.docx"}, {"职业健康安全管理手册.pdf", "/files/health/职业健康安全管理手册.pdf"}, {"环境保护管理制度.doc", "/files/env/环境保护管理制度.doc"}, {"生产安全操作规程.pdf", "/files/production/生产安全操作规程.pdf"}, {"安全培训教材.pptx", "/files/training/安全培训教材.pptx"}, {"应急预案模板.docx", "/files/emergency/应急预案模板.docx"}, {"安全检查表.xlsx", "/files/inspection/安全检查表.xlsx"}, {"事故调查报告.pdf", "/files/incident/事故调查报告.pdf"}, {"安全技术规范.pdf", "/files/tech/安全技术规范.pdf"}, } // 测试用例 testCases := []struct { query string expected string }{ {"安全生产管理", "安全生产管理制度.pdf"}, {"消防安全", "消防安全管理规定.docx"}, {"职业健康", "职业健康安全管理手册.pdf"}, {"环境保护", "环境保护管理制度.doc"}, {"生产安全", "生产安全操作规程.pdf"}, {"安全培训", "安全培训教材.pptx"}, {"应急预案", "应急预案模板.docx"}, {"安全检查", "安全检查表.xlsx"}, {"事故调查", "事故调查报告.pdf"}, {"安全技术", "安全技术规范.pdf"}, {"完全不匹配的文件", ""}, // 应该返回相似度最低的 } for i, tc := range testCases { fmt.Printf("\n测试用例 %d:\n", i+1) fmt.Printf("查询: %s\n", tc.query) // 提取文件名作为候选列表 var candidates []string for _, file := range mockFiles { candidates = append(candidates, file.fileName) } // 使用编辑距离算法找到最相似的文件名 bestMatch, bestScore := FindBestMatch(tc.query, candidates) // 找到对应的文件路径 var matchedPath string for _, file := range mockFiles { if file.fileName == bestMatch { matchedPath = file.filePath break } } fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch, bestScore) fmt.Printf("文件路径: %s\n", matchedPath) // 显示所有匹配结果(按相似度排序) allMatches := FindBestMatches(tc.query, candidates) fmt.Println("所有匹配结果(前5个):") for j, match := range allMatches { if j >= 5 { // 只显示前5个 break } fmt.Printf(" %d. %s (相似度: %.3f)\n", j+1, match.Text, match.Score) } // 设置阈值测试 threshold := 0.3 if bestScore >= threshold { fmt.Printf("✅ 相似度 %.3f >= %.1f,匹配成功\n", bestScore, threshold) } else { fmt.Printf("❌ 相似度 %.3f < %.1f,匹配失败\n", bestScore, threshold) } } } // TestSimilarityThresholds 测试不同相似度阈值的效果 func TestSimilarityThresholds() { fmt.Println("\n=== 相似度阈值测试 ===") query := "安全生产管理" candidates := []string{ "安全生产管理制度.pdf", "消防安全管理规定.docx", "职业健康安全管理手册.pdf", "环境保护管理制度.doc", "生产安全操作规程.pdf", "安全培训教材.pptx", "应急预案模板.docx", "安全检查表.xlsx", "事故调查报告.pdf", "安全技术规范.pdf", } thresholds := []float64{0.1, 0.3, 0.5, 0.7, 0.9} for _, threshold := range thresholds { fmt.Printf("\n阈值 %.1f 的匹配结果:\n", threshold) allMatches := FindBestMatches(query, candidates) count := 0 for _, match := range allMatches { if match.Score >= threshold { fmt.Printf(" %s (相似度: %.3f)\n", match.Text, match.Score) count++ } } if count == 0 { fmt.Printf(" 没有找到相似度 >= %.1f 的匹配\n", threshold) } } } // RunAllFileTests 运行所有文件匹配测试 func RunAllFileTests() { TestFileMatching() TestSimilarityThresholds() }