keywordwieght
This commit is contained in:
parent
2fac8d13fc
commit
24f1bb014c
10
util.go
10
util.go
|
@ -145,8 +145,6 @@ func Reverse(s interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func WeightKeyword(keywords string) string {
|
func WeightKeyword(keywords string) string {
|
||||||
keywords = strings.ReplaceAll(keywords, ",", ",")
|
|
||||||
keywords = strings.ReplaceAll(keywords, ":", ":")
|
|
||||||
fields := strings.Split(keywords, ",")
|
fields := strings.Split(keywords, ",")
|
||||||
if len(fields) == 1 {
|
if len(fields) == 1 {
|
||||||
return keywords
|
return keywords
|
||||||
|
@ -155,6 +153,7 @@ func WeightKeyword(keywords string) string {
|
||||||
sumWeight := int(0)
|
sumWeight := int(0)
|
||||||
arrWeight := make([]int, 0)
|
arrWeight := make([]int, 0)
|
||||||
arrKey := make([]string, 0)
|
arrKey := make([]string, 0)
|
||||||
|
zeroWeightKey := make([]string, 0)
|
||||||
|
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
field = strings.Trim(field, "\r\t\n ")
|
field = strings.Trim(field, "\r\t\n ")
|
||||||
|
@ -164,13 +163,20 @@ func WeightKeyword(keywords string) string {
|
||||||
if len(kvs) > 1 {
|
if len(kvs) > 1 {
|
||||||
weight, _ = strconv.Atoi(strings.Trim(kvs[1], "\r\t\n "))
|
weight, _ = strconv.Atoi(strings.Trim(kvs[1], "\r\t\n "))
|
||||||
}
|
}
|
||||||
|
|
||||||
if weight <= 0 {
|
if weight <= 0 {
|
||||||
|
zeroWeightKey = append(zeroWeightKey, key)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sumWeight += weight
|
sumWeight += weight
|
||||||
arrWeight = append(arrWeight, sumWeight)
|
arrWeight = append(arrWeight, sumWeight)
|
||||||
arrKey = append(arrKey, key)
|
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()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
randWeight := r.Intn(sumWeight)
|
randWeight := r.Intn(sumWeight)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue