| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package utils
- import (
- "fmt"
- )
- // TestStringMatching 测试字符串匹配算法
- func TestStringMatching() {
- fmt.Println("=== 字符串模糊匹配测试 ===")
- // 测试用例1:安全生产相关
- fmt.Println("\n测试用例1:安全生产相关")
- target1 := "安全生产管理"
- candidates1 := []string{
- "安全生产管理制度",
- "安全管理规定",
- "生产安全规范",
- "安全生产标准",
- "安全管理制度",
- "生产管理规定",
- "安全生产条例",
- }
- bestMatch1, bestScore1 := FindBestMatch(target1, candidates1)
- fmt.Printf("目标: %s\n", target1)
- fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch1, bestScore1)
- allMatches1 := FindBestMatches(target1, candidates1)
- fmt.Println("所有匹配结果(按相似度排序):")
- for i, match := range allMatches1 {
- fmt.Printf(" %d. %s (相似度: %.3f)\n", i+1, match.Text, match.Score)
- }
- // 测试用例2:消防安全相关
- fmt.Println("\n测试用例2:消防安全相关")
- target2 := "消防安全"
- candidates2 := []string{
- "消防安全管理制度",
- "消防管理规定",
- "消防安全规范",
- "消防设备管理",
- "消防安全检查",
- "消防应急预案",
- "消防安全培训",
- }
- bestMatch2, bestScore2 := FindBestMatch(target2, candidates2)
- fmt.Printf("目标: %s\n", target2)
- fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch2, bestScore2)
- allMatches2 := FindBestMatches(target2, candidates2)
- fmt.Println("所有匹配结果(按相似度排序):")
- for i, match := range allMatches2 {
- fmt.Printf(" %d. %s (相似度: %.3f)\n", i+1, match.Text, match.Score)
- }
- // 测试用例3:完全不同的字符串
- fmt.Println("\n测试用例3:完全不同的字符串")
- target3 := "环境保护"
- candidates3 := []string{
- "安全生产管理",
- "消防安全制度",
- "职业健康安全",
- "环境保护管理",
- "环境监测制度",
- "环境保护规定",
- "环境安全管理",
- }
- bestMatch3, bestScore3 := FindBestMatch(target3, candidates3)
- fmt.Printf("目标: %s\n", target3)
- fmt.Printf("最佳匹配: %s (相似度: %.3f)\n", bestMatch3, bestScore3)
- allMatches3 := FindBestMatches(target3, candidates3)
- fmt.Println("所有匹配结果(按相似度排序):")
- for i, match := range allMatches3 {
- fmt.Printf(" %d. %s (相似度: %.3f)\n", i+1, match.Text, match.Score)
- }
- }
- // TestSimilarityCalculation 测试相似度计算
- func TestSimilarityCalculation() {
- fmt.Println("\n=== 相似度计算测试 ===")
- testCases := []struct {
- s1, s2 string
- desc string
- }{
- {"安全生产管理", "安全生产管理制度", "部分匹配"},
- {"安全管理", "安全生产管理", "包含关系"},
- {"生产安全", "安全生产", "顺序不同"},
- {"完全不同的字符串", "另一个完全不同的字符串", "完全不同"},
- {"相同的字符串", "相同的字符串", "完全相同"},
- {"", "", "空字符串"},
- {"短", "很长的字符串", "长度差异很大"},
- {"安全生产", "安全生产", "完全相同"},
- {"安全", "安全生产管理", "子字符串"},
- }
- for _, tc := range testCases {
- similarity := StringSimilarity(tc.s1, tc.s2)
- fmt.Printf("%s: '%s' vs '%s' = %.3f\n", tc.desc, tc.s1, tc.s2, similarity)
- }
- }
- // TestWithThreshold 测试带阈值的匹配
- func TestWithThreshold() {
- fmt.Println("\n=== 带阈值的匹配测试 ===")
- target := "安全管理"
- candidates := []string{
- "安全生产管理制度",
- "安全管理规定",
- "生产安全规范",
- "安全生产标准",
- "安全管理制度",
- "生产管理规定",
- "安全生产条例",
- "职业健康安全管理",
- "消防安全管理",
- "环境安全管理",
- "完全不同的内容",
- "其他不相关的内容",
- }
- thresholds := []float64{0.3, 0.5, 0.7, 0.9}
- for _, threshold := range thresholds {
- fmt.Printf("\n阈值 %.1f 的匹配结果:\n", threshold)
- allMatches := FindBestMatches(target, 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)
- }
- }
- }
- // RunAllTests 运行所有测试
- func RunAllTests() {
- TestStringMatching()
- TestSimilarityCalculation()
- TestWithThreshold()
- }
|