|
@@ -9,6 +9,7 @@ import (
|
|
|
"cmplatform/util"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "github.com/antonmedv/expr"
|
|
|
"github.com/lauyoume/gopinyin"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.uber.org/zap"
|
|
@@ -315,12 +316,14 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
//获取数据
|
|
|
title := this.GetString("title")
|
|
|
detail := this.GetString("detail")
|
|
|
- fmt.Println(title, detail)
|
|
|
+ exactRule, exactResult := "", false
|
|
|
if title == "" && detail == "" {
|
|
|
this.ServeJson(map[string]interface{}{
|
|
|
- "status": false,
|
|
|
- "data": "",
|
|
|
- "message": "标题和正文至少要有一个",
|
|
|
+ "status": false,
|
|
|
+ "data": "",
|
|
|
+ "message": "标题和正文至少要有一个",
|
|
|
+ "exactResult": exactResult,
|
|
|
+ "exactRule": exactRule,
|
|
|
})
|
|
|
return
|
|
|
}
|
|
@@ -340,6 +343,7 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
s_globalnotkeymatch := common.ObjToString(c["s_globalnotkeymatch"])
|
|
|
s_globalclearkey := common.ObjToString(c["s_globalclearkey"])
|
|
|
s_globalclearkeymatch := common.ObjToString(c["s_globalclearkeymatch"])
|
|
|
+ exactRule = common.ObjToString(c["s_exactRule"])
|
|
|
|
|
|
//清理词匹配方式
|
|
|
cwmArr := []string{}
|
|
@@ -384,7 +388,9 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
"info": "全局附加词没有匹配成功",
|
|
|
"data": s_globaladdkey,
|
|
|
},
|
|
|
- "message": "测试成功",
|
|
|
+ "message": "测试成功",
|
|
|
+ "exactResult": exactResult,
|
|
|
+ "exactRule": exactRule,
|
|
|
})
|
|
|
return
|
|
|
} else {
|
|
@@ -401,13 +407,15 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
"info": "被全局排除词排除",
|
|
|
"data": gNKey,
|
|
|
},
|
|
|
- "message": "测试成功",
|
|
|
+ "message": "测试成功",
|
|
|
+ "exactResult": exactResult,
|
|
|
+ "exactRule": exactRule,
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- var resultList []interface{}
|
|
|
+ var resultList []map[string]interface{}
|
|
|
for _, v := range o_rules {
|
|
|
matchKey = ""
|
|
|
key := common.ObjToString(v["s_matchkey"])
|
|
@@ -416,6 +424,8 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
addkeymatch := common.ObjToString(v["s_addkeymatch"])
|
|
|
notkey := common.ObjToString(v["s_notkey"])
|
|
|
notkeymatch := common.ObjToString(v["s_notkeymatch"])
|
|
|
+ s_group := common.ObjToString(v["s_group"])
|
|
|
+
|
|
|
var tempData = map[string]interface{}{
|
|
|
"key": key,
|
|
|
"keymatch": keymatch,
|
|
@@ -423,6 +433,7 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
"addkeymatch": addkeymatch,
|
|
|
"notkey": notkey,
|
|
|
"notkeymatch": notkeymatch,
|
|
|
+ "s_group": s_group,
|
|
|
}
|
|
|
if notkey != "" {
|
|
|
nKey := TestMactchKeys(notkeymatch, notkey, article)
|
|
@@ -487,25 +498,85 @@ func (this *CustomerRule) DataTest(id string) {
|
|
|
"info": "全局附加词匹配成功" + matchKey,
|
|
|
"data": "",
|
|
|
},
|
|
|
- "message": "测试成功",
|
|
|
+ "message": "测试成功",
|
|
|
+ "exactResult": exactResult,
|
|
|
+ "exactRule": exactRule,
|
|
|
})
|
|
|
return
|
|
|
|
|
|
} else {
|
|
|
+ nameArr := []string{}
|
|
|
+ data, ok := util.Mgo.Find("groups", map[string]interface{}{"ruleId": id}, nil, nil, false, -1, -1)
|
|
|
+ if ok && data != nil && len(*data) > 0 {
|
|
|
+ for _, v := range *data {
|
|
|
+ nameArr = append(nameArr, common.ObjToString(v["name"]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ exactResult = exactMatch(exactRule, resultList, nameArr)
|
|
|
this.ServeJson(map[string]interface{}{
|
|
|
- "status": true,
|
|
|
- "data": resultList,
|
|
|
- "message": "测试成功",
|
|
|
+ "status": true,
|
|
|
+ "data": resultList,
|
|
|
+ "message": "测试成功",
|
|
|
+ "exactResult": exactResult,
|
|
|
+ "exactRule": exactRule,
|
|
|
})
|
|
|
}
|
|
|
return
|
|
|
} else {
|
|
|
this.ServeJson(map[string]interface{}{
|
|
|
- "status": true,
|
|
|
- "data": "",
|
|
|
- "message": "初始化客户信息失败",
|
|
|
+ "status": true,
|
|
|
+ "data": "",
|
|
|
+ "message": "初始化客户信息失败",
|
|
|
+ "exactResult": exactResult,
|
|
|
+ "exactRule": exactRule,
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
log.Debug("测试数据是否匹配结束...")
|
|
|
}
|
|
|
+func exactMatch(rule string, data []map[string]interface{}, nameArr []string) bool {
|
|
|
+ realdata := map[string]float64{}
|
|
|
+ for _, v := range nameArr {
|
|
|
+ realdata["title_"+v] = 0
|
|
|
+ realdata["content_"+v] = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ mapping := map[string]string{
|
|
|
+ " and ": " && ",
|
|
|
+ " or ": " || ",
|
|
|
+ " not ": " ! ",
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, v := range mapping {
|
|
|
+ rule = strings.ReplaceAll(rule, k, v)
|
|
|
+ }
|
|
|
+ //可以将编译后的表达式,存放在缓存中
|
|
|
+ program, err := expr.Compile(rule, expr.Env(realdata))
|
|
|
+ if err != nil {
|
|
|
+ log.Error("表达式错误 ", zap.Error(err))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range data {
|
|
|
+ if v["status"].(bool) {
|
|
|
+ keymatch := common.ObjToString(v["keymatch"])
|
|
|
+ group := common.ObjToString(v["s_group"])
|
|
|
+ markkey := strings.Split(common.ObjToString(v["markkey"]), ",")
|
|
|
+ if group != "" {
|
|
|
+ if strings.Contains(keymatch, "1") {
|
|
|
+ realdata["title_"+group] = realdata["title_"+group] + float64(len(markkey))
|
|
|
+ }
|
|
|
+ if strings.Contains(keymatch, "2") {
|
|
|
+ realdata["content_"+group] = realdata["content_"+group] + float64(len(markkey))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.Debug("匹配结果 ", zap.Any("realdata", realdata))
|
|
|
+ output, err := expr.Run(program, realdata)
|
|
|
+ if err != nil {
|
|
|
+ log.Error("表达式执行错误 ", zap.Error(err))
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return output.(bool)
|
|
|
+}
|