test_string_match.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package utils
  2. import (
  3. "fmt"
  4. )
  5. // TestStringMatching 测试字符串匹配算法
  6. func TestStringMatching() {
  7. fmt.Println("=== 字符串模糊匹配测试 ===")
  8. // 测试用例1:安全生产相关
  9. fmt.Println("\n测试用例1:安全生产相关")
  10. target1 := "安全生产管理"
  11. candidates1 := []string{
  12. "安全生产管理制度",
  13. "安全管理规定",
  14. "生产安全规范",
  15. "安全生产标准",
  16. "安全管理制度",
  17. "生产管理规定",
  18. "安全生产条例",
  19. }
  20. bestMatch1, bestScore1 := FindBestMatch(target1, candidates1)
  21. fmt.Printf("目标: %s\n", target1)
  22. fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch1, bestScore1)
  23. allMatches1 := FindBestMatches(target1, candidates1)
  24. fmt.Println("所有匹配结果(按相似度排序):")
  25. for i, match := range allMatches1 {
  26. fmt.Printf(" %d. %s (相似度: %.3f)\n", i+1, match.Text, match.Score)
  27. }
  28. // 测试用例2:消防安全相关
  29. fmt.Println("\n测试用例2:消防安全相关")
  30. target2 := "消防安全"
  31. candidates2 := []string{
  32. "消防安全管理制度",
  33. "消防管理规定",
  34. "消防安全规范",
  35. "消防设备管理",
  36. "消防安全检查",
  37. "消防应急预案",
  38. "消防安全培训",
  39. }
  40. bestMatch2, bestScore2 := FindBestMatch(target2, candidates2)
  41. fmt.Printf("目标: %s\n", target2)
  42. fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch2, bestScore2)
  43. allMatches2 := FindBestMatches(target2, candidates2)
  44. fmt.Println("所有匹配结果(按相似度排序):")
  45. for i, match := range allMatches2 {
  46. fmt.Printf(" %d. %s (相似度: %.3f)\n", i+1, match.Text, match.Score)
  47. }
  48. // 测试用例3:完全不同的字符串
  49. fmt.Println("\n测试用例3:完全不同的字符串")
  50. target3 := "环境保护"
  51. candidates3 := []string{
  52. "安全生产管理",
  53. "消防安全制度",
  54. "职业健康安全",
  55. "环境保护管理",
  56. "环境监测制度",
  57. "环境保护规定",
  58. "环境安全管理",
  59. }
  60. bestMatch3, bestScore3 := FindBestMatch(target3, candidates3)
  61. fmt.Printf("目标: %s\n", target3)
  62. fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch3, bestScore3)
  63. allMatches3 := FindBestMatches(target3, candidates3)
  64. fmt.Println("所有匹配结果(按相似度排序):")
  65. for i, match := range allMatches3 {
  66. fmt.Printf(" %d. %s (相似度: %.3f)\n", i+1, match.Text, match.Score)
  67. }
  68. }
  69. // TestSimilarityCalculation 测试相似度计算
  70. func TestSimilarityCalculation() {
  71. fmt.Println("\n=== 相似度计算测试 ===")
  72. testCases := []struct {
  73. s1, s2 string
  74. desc string
  75. }{
  76. {"安全生产管理", "安全生产管理制度", "部分匹配"},
  77. {"安全管理", "安全生产管理", "包含关系"},
  78. {"生产安全", "安全生产", "顺序不同"},
  79. {"完全不同的字符串", "另一个完全不同的字符串", "完全不同"},
  80. {"相同的字符串", "相同的字符串", "完全相同"},
  81. {"", "", "空字符串"},
  82. {"短", "很长的字符串", "长度差异很大"},
  83. {"安全生产", "安全生产", "完全相同"},
  84. {"安全", "安全生产管理", "子字符串"},
  85. }
  86. for _, tc := range testCases {
  87. similarity := StringSimilarity(tc.s1, tc.s2)
  88. fmt.Printf("%s: '%s' vs '%s' = %.3f\n", tc.desc, tc.s1, tc.s2, similarity)
  89. }
  90. }
  91. // TestWithThreshold 测试带阈值的匹配
  92. func TestWithThreshold() {
  93. fmt.Println("\n=== 带阈值的匹配测试 ===")
  94. target := "安全管理"
  95. candidates := []string{
  96. "安全生产管理制度",
  97. "安全管理规定",
  98. "生产安全规范",
  99. "安全生产标准",
  100. "安全管理制度",
  101. "生产管理规定",
  102. "安全生产条例",
  103. "职业健康安全管理",
  104. "消防安全管理",
  105. "环境安全管理",
  106. "完全不同的内容",
  107. "其他不相关的内容",
  108. }
  109. thresholds := []float64{0.3, 0.5, 0.7, 0.9}
  110. for _, threshold := range thresholds {
  111. fmt.Printf("\n阈值 %.1f 的匹配结果:\n", threshold)
  112. allMatches := FindBestMatches(target, candidates)
  113. count := 0
  114. for _, match := range allMatches {
  115. if match.Score >= threshold {
  116. fmt.Printf(" %s (相似度: %.3f)\n", match.Text, match.Score)
  117. count++
  118. }
  119. }
  120. if count == 0 {
  121. fmt.Printf(" 没有找到相似度 >= %.1f 的匹配\n", threshold)
  122. }
  123. }
  124. }
  125. // RunAllTests 运行所有测试
  126. func RunAllTests() {
  127. TestStringMatching()
  128. TestSimilarityCalculation()
  129. TestWithThreshold()
  130. }