modify similarity.go

This commit is contained in:
Nite07 2024-12-03 23:38:54 +08:00
parent f358a74079
commit 901785525f
3 changed files with 17 additions and 16 deletions

2
go.mod
View File

@ -25,7 +25,7 @@ require (
)
require (
git.nite07.com/nite/ccs v0.0.0-20241202053845-550f69c19ba2 // indirect
git.nite07.com/nite/ccs v0.0.0-20241203153655-eff00a6afd7e // indirect
github.com/Danny-Dasilva/CycleTLS/cycletls v1.0.26 // indirect
github.com/Danny-Dasilva/fhttp v0.0.0-20240217042913-eeeb0b347ce1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect

2
go.sum
View File

@ -11,6 +11,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
git.nite07.com/nite/ccs v0.0.0-20241202053845-550f69c19ba2 h1:hsnRzTS+6Dd+ZFPe486Dg59X0azNOHviDabChc8dKKk=
git.nite07.com/nite/ccs v0.0.0-20241202053845-550f69c19ba2/go.mod h1:+kZxYKbZJ3igYXdgCStq+SocI2Wy0fE0RaGqW6YD71w=
git.nite07.com/nite/ccs v0.0.0-20241203153655-eff00a6afd7e h1:SJVt97JqleQiRNP6HpH5OWSneXhBs8bhaQ0Em20KDl4=
git.nite07.com/nite/ccs v0.0.0-20241203153655-eff00a6afd7e/go.mod h1:+kZxYKbZJ3igYXdgCStq+SocI2Wy0fE0RaGqW6YD71w=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Danny-Dasilva/CycleTLS/cycletls v1.0.26 h1:6fexoGmvzoXMSk14BZ0AirapVm5c3KUsEjE0jLlVKi8=
github.com/Danny-Dasilva/CycleTLS/cycletls v1.0.26/go.mod h1:QFi/EVO7qqru3Ftxz1LR+96jIc91Tifv0DnskF/gWQ8=

View File

@ -1,19 +1,10 @@
package utils
import (
"regexp"
"strings"
)
func minInt(nums ...int) int {
m := nums[0]
for _, num := range nums {
if num < m {
m = num
}
}
return m
}
func levenshteinDistance(str1, str2 string) int {
str1 = strings.ToLower(str1)
str2 = strings.ToLower(str2)
@ -44,28 +35,36 @@ func levenshteinDistance(str1, str2 string) int {
if s1[i-1] != s2[j-1] {
cost = 1
}
d[i][j] = minInt(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost)
d[i][j] = min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost)
}
}
return d[lenS1][lenS2]
}
var noneAlphaNumericRegex = regexp.MustCompile("[^a-zA-Z0-9]+")
func removeSpacesAndNoneAlphaNumeric(str string) string {
str = strings.ToLower(str)
str = noneAlphaNumericRegex.ReplaceAllString(str, "")
return str
}
func Similarity(str1, str2 string) float64 {
str1 = strings.ReplaceAll(str1, " ", "")
str2 = strings.ReplaceAll(str2, " ", "")
str1 = removeSpacesAndNoneAlphaNumeric(str1)
str2 = removeSpacesAndNoneAlphaNumeric(str2)
distance := levenshteinDistance(str1, str2)
maxLength := len(str1)
if len(str2) > maxLength {
maxLength = len(str2)
}
djustedMaxLength := maxLength + (len(str1) + len(str2))
adjustedLength := float64(maxLength + (len(str1)+len(str2))/2)
if maxLength == 0 {
return 1.0
}
similarity := 1.0 - float64(distance)/float64(djustedMaxLength)
similarity := 1.0 - float64(distance)/float64(adjustedLength)
return similarity
}