From 24f1bb014cc67aeb5e6d1516d614fe53a301f07b Mon Sep 17 00:00:00 2001 From: jiangyong Date: Thu, 28 Aug 2025 19:33:15 +0800 Subject: [PATCH] keywordwieght --- util.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/util.go b/util.go index 89193d4..92cdf20 100644 --- a/util.go +++ b/util.go @@ -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)