@@ -19,46 +19,7 @@ func FindBestMatchOne(s string, targets []string, opts ...Option) *similarity.Ma
1919 return r .Match
2020}
2121
22- // 比较两个字符串内部函数
23- func compare (s1 , s2 string , o * option ) float64 {
24- if s , e := modifyStrAndCheck (o , & s1 , & s2 ); e {
25- return s
26- }
27-
28- return o .cmp (s1 , s2 )
29- }
30-
31- // 前处理主要涉及,修改字符串,和边界判断
32- func modifyStrAndCheck (o * option , s1 , s2 * string ) (score float64 , exit bool ) {
33- modifyString (o , s1 )
34- modifyString (o , s2 )
35-
36- return check (* s1 , * s2 )
37- }
38-
39- // 记录每个targets子串的相似度打分,并且返回相似度最高的那个字符串, 内部函数
40- func findBestMatch (s string , targets []string , opts ... Option ) * similarity.MatchResult {
41-
42- var opt option
43- opt .fillOption (opts ... )
44-
45- match := make ([]* similarity.Match , 0 , len (targets ))
46- bestIndex := 0
47- for k , s2 := range targets {
48-
49- score := compare (s , s2 , & opt )
50-
51- //fmt.Printf("score:%f(%s)(%s)\n", score, s, s2)
52- match = append (match , & similarity.Match {S : s2 , Score : score })
53-
54- if k == 0 {
55- continue
56- }
57-
58- if score > match [bestIndex ].Score {
59- bestIndex = k
60- }
61- }
62-
63- return & similarity.MatchResult {AllResult : match , Match : match [bestIndex ], BestIndex : bestIndex }
22+ // 返回相似度最高的那个字符串, 以及索引位置
23+ func FindBestMatch (s string , targets []string , opts ... Option ) * similarity.MatchResult {
24+ return findBestMatch (s , targets , opts ... )
6425}
0 commit comments