test_file_match.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package utils
  2. import (
  3. "fmt"
  4. )
  5. // TestFileMatching 测试文件匹配功能
  6. func TestFileMatching() {
  7. fmt.Println("=== 文件匹配测试 ===")
  8. // 模拟数据库中的文件列表
  9. mockFiles := []struct {
  10. fileName string
  11. filePath string
  12. }{
  13. {"安全生产管理制度.pdf", "/files/safety/安全生产管理制度.pdf"},
  14. {"消防安全管理规定.docx", "/files/fire/消防安全管理规定.docx"},
  15. {"职业健康安全管理手册.pdf", "/files/health/职业健康安全管理手册.pdf"},
  16. {"环境保护管理制度.doc", "/files/env/环境保护管理制度.doc"},
  17. {"生产安全操作规程.pdf", "/files/production/生产安全操作规程.pdf"},
  18. {"安全培训教材.pptx", "/files/training/安全培训教材.pptx"},
  19. {"应急预案模板.docx", "/files/emergency/应急预案模板.docx"},
  20. {"安全检查表.xlsx", "/files/inspection/安全检查表.xlsx"},
  21. {"事故调查报告.pdf", "/files/incident/事故调查报告.pdf"},
  22. {"安全技术规范.pdf", "/files/tech/安全技术规范.pdf"},
  23. }
  24. // 测试用例
  25. testCases := []struct {
  26. query string
  27. expected string
  28. }{
  29. {"安全生产管理", "安全生产管理制度.pdf"},
  30. {"消防安全", "消防安全管理规定.docx"},
  31. {"职业健康", "职业健康安全管理手册.pdf"},
  32. {"环境保护", "环境保护管理制度.doc"},
  33. {"生产安全", "生产安全操作规程.pdf"},
  34. {"安全培训", "安全培训教材.pptx"},
  35. {"应急预案", "应急预案模板.docx"},
  36. {"安全检查", "安全检查表.xlsx"},
  37. {"事故调查", "事故调查报告.pdf"},
  38. {"安全技术", "安全技术规范.pdf"},
  39. {"完全不匹配的文件", ""}, // 应该返回相似度最低的
  40. }
  41. for i, tc := range testCases {
  42. fmt.Printf("\n测试用例 %d:\n", i+1)
  43. fmt.Printf("查询: %s\n", tc.query)
  44. // 提取文件名作为候选列表
  45. var candidates []string
  46. for _, file := range mockFiles {
  47. candidates = append(candidates, file.fileName)
  48. }
  49. // 使用编辑距离算法找到最相似的文件名
  50. bestMatch, bestScore := FindBestMatch(tc.query, candidates)
  51. // 找到对应的文件路径
  52. var matchedPath string
  53. for _, file := range mockFiles {
  54. if file.fileName == bestMatch {
  55. matchedPath = file.filePath
  56. break
  57. }
  58. }
  59. fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch, bestScore)
  60. fmt.Printf("文件路径: %s\n", matchedPath)
  61. // 显示所有匹配结果(按相似度排序)
  62. allMatches := FindBestMatches(tc.query, candidates)
  63. fmt.Println("所有匹配结果(前5个):")
  64. for j, match := range allMatches {
  65. if j >= 5 { // 只显示前5个
  66. break
  67. }
  68. fmt.Printf(" %d. %s (相似度: %.3f)\n", j+1, match.Text, match.Score)
  69. }
  70. // 设置阈值测试
  71. threshold := 0.3
  72. if bestScore >= threshold {
  73. fmt.Printf("✅ 相似度 %.3f >= %.1f,匹配成功\n", bestScore, threshold)
  74. } else {
  75. fmt.Printf("❌ 相似度 %.3f < %.1f,匹配失败\n", bestScore, threshold)
  76. }
  77. }
  78. }
  79. // TestSimilarityThresholds 测试不同相似度阈值的效果
  80. func TestSimilarityThresholds() {
  81. fmt.Println("\n=== 相似度阈值测试 ===")
  82. query := "安全生产管理"
  83. candidates := []string{
  84. "安全生产管理制度.pdf",
  85. "消防安全管理规定.docx",
  86. "职业健康安全管理手册.pdf",
  87. "环境保护管理制度.doc",
  88. "生产安全操作规程.pdf",
  89. "安全培训教材.pptx",
  90. "应急预案模板.docx",
  91. "安全检查表.xlsx",
  92. "事故调查报告.pdf",
  93. "安全技术规范.pdf",
  94. }
  95. thresholds := []float64{0.1, 0.3, 0.5, 0.7, 0.9}
  96. for _, threshold := range thresholds {
  97. fmt.Printf("\n阈值 %.1f 的匹配结果:\n", threshold)
  98. allMatches := FindBestMatches(query, candidates)
  99. count := 0
  100. for _, match := range allMatches {
  101. if match.Score >= threshold {
  102. fmt.Printf(" %s (相似度: %.3f)\n", match.Text, match.Score)
  103. count++
  104. }
  105. }
  106. if count == 0 {
  107. fmt.Printf(" 没有找到相似度 >= %.1f 的匹配\n", threshold)
  108. }
  109. }
  110. }
  111. // RunAllFileTests 运行所有文件匹配测试
  112. func RunAllFileTests() {
  113. TestFileMatching()
  114. TestSimilarityThresholds()
  115. }