keywordwieght

This commit is contained in:
jiangyong 2025-08-28 19:33:15 +08:00
parent 2fac8d13fc
commit 24f1bb014c
1 changed files with 8 additions and 2 deletions

10
util.go
View File

@ -145,8 +145,6 @@ func Reverse(s interface{}) {
}
func WeightKeyword(keywords string) string {
keywords = strings.ReplaceAll(keywords, "", ",")
keywords = strings.ReplaceAll(keywords, "", ":")
fields := strings.Split(keywords, ",")
if len(fields) == 1 {
return keywords
@ -155,6 +153,7 @@ func WeightKeyword(keywords string) string {
sumWeight := int(0)
arrWeight := make([]int, 0)
arrKey := make([]string, 0)
zeroWeightKey := make([]string, 0)
for _, field := range fields {
field = strings.Trim(field, "\r\t\n ")
@ -164,13 +163,20 @@ func WeightKeyword(keywords string) string {
if len(kvs) > 1 {
weight, _ = strconv.Atoi(strings.Trim(kvs[1], "\r\t\n "))
}
if weight <= 0 {
zeroWeightKey = append(zeroWeightKey, key)
continue
}
sumWeight += weight
arrWeight = append(arrWeight, sumWeight)
arrKey = append(arrKey, key)
}
if sumWeight == 0 && len(zeroWeightKey) > 0 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return zeroWeightKey[r.Intn(len(zeroWeightKey))]
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randWeight := r.Intn(sumWeight)