WeightKeyword

This commit is contained in:
jiangyong27 2025-04-09 18:55:33 +08:00
parent 845a8a40e8
commit 84bd7614b8
1 changed files with 39 additions and 0 deletions

39
util.go
View File

@ -3,11 +3,13 @@ package goutil
import (
"errors"
"fmt"
"math/rand"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// FormatMoney 格式化商品价格
@ -141,3 +143,40 @@ func Reverse(s interface{}) {
return true
})
}
func WeightKeyword(keywords string) string {
fields := strings.Split(keywords, ",")
if len(fields) == 1 {
return keywords
}
sumWeight := int(0)
arrWeight := make([]int, 0)
arrKey := make([]string, 0)
for _, field := range fields {
field = strings.Trim(field, "\r\t\n ")
kvs := strings.SplitN(field, ":", 2)
key := strings.Trim(kvs[0], "\r\t\n ")
weight := int(0)
if len(kvs) > 1 {
weight, _ = strconv.Atoi(strings.Trim(kvs[1], "\r\t\n "))
}
if weight <= 0 {
continue
}
sumWeight += weight
arrWeight = append(arrWeight, sumWeight)
arrKey = append(arrKey, key)
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randWeight := r.Intn(sumWeight)
for i := 0; i < len(arrWeight); i++ {
if randWeight < arrWeight[i] {
return arrKey[i]
}
}
return ""
}