score.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // score
  2. package extract
  3. import (
  4. "fmt"
  5. ju "jy/util"
  6. "log"
  7. qu "qfw/util"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "unicode/utf8"
  13. )
  14. var (
  15. lockscore sync.RWMutex
  16. SoreConfig map[string]map[string]interface{}
  17. TagConfig map[string]map[string]float64
  18. TagConfigDesc map[string]string
  19. RepeatScore, BlockScore float64
  20. CommonScore map[string]float64
  21. FieldsScore map[string]map[string]float64
  22. )
  23. func init() {
  24. qu.ReadConfig("./res/tagscoredesc.json", &TagConfigDesc)
  25. qu.ReadConfig("./res/tagscore.json", &TagConfig)
  26. qu.ReadConfig("./res/fieldscore.json", &SoreConfig)
  27. if repeat, ok := SoreConfig["other"]["repeat"].(map[string]interface{}); ok {
  28. RepeatScore = qu.Float64All(repeat["score"])
  29. }
  30. if block, ok := SoreConfig["other"]["block"].(map[string]interface{}); ok {
  31. BlockScore = qu.Float64All(block["score"])
  32. }
  33. //通用抽取属性打分配置
  34. if tmp, ok := SoreConfig["extractype"]["common"].(map[string]interface{}); ok {
  35. CommonScore = map[string]float64{}
  36. for k, v := range tmp {
  37. CommonScore[k] = qu.Float64All(v)
  38. }
  39. }
  40. //指定抽取属性打分配置
  41. if tmp, ok := SoreConfig["extractype"]["fields"].(map[string]interface{}); ok {
  42. FieldsScore = map[string]map[string]float64{}
  43. for key, fieldmap := range tmp {
  44. fieldscore := map[string]float64{}
  45. if field, ok := fieldmap.(map[string]interface{}); ok {
  46. for k, score := range field {
  47. fieldscore[k] = qu.Float64All(score)
  48. }
  49. }
  50. FieldsScore[key] = fieldscore
  51. }
  52. }
  53. //实例化正则
  54. for _, tmp := range SoreConfig {
  55. //log.Println(tmp)
  56. if tmp["type"] == "string" {
  57. if positions, ok := tmp["positivewords"].([]interface{}); ok {
  58. for _, position := range positions {
  59. if p, ok := position.(map[string]interface{}); ok {
  60. qu.Try(func() {
  61. strReq, _ := p["regstr"].(string)
  62. if strings.Contains(strReq, "\\u") {
  63. strReq = strings.Replace(strReq, "\\", "\\\\", -1)
  64. strReq = strings.Replace(strReq, "\\\\u", "\\u", -1)
  65. strReq, _ = strconv.Unquote(`"` + strReq + `"`)
  66. p["regexp"] = regexp.MustCompile(strReq)
  67. } else {
  68. p["regexp"] = regexp.MustCompile(strReq)
  69. }
  70. }, func(err interface{}) {
  71. log.Println(err)
  72. })
  73. }
  74. }
  75. }
  76. if positions, ok := tmp["negativewords"].([]interface{}); ok {
  77. for _, position := range positions {
  78. if p, ok := position.(map[string]interface{}); ok {
  79. qu.Try(func() {
  80. strReq, _ := p["regstr"].(string)
  81. if strings.Contains(strReq, "\\u") {
  82. strReq = strings.Replace(strReq, "\\", "\\\\", -1)
  83. strReq = strings.Replace(strReq, "\\\\u", "\\u", -1)
  84. strReq, _ = strconv.Unquote(`"` + strReq + `"`)
  85. p["regexp"] = regexp.MustCompile(strReq)
  86. } else {
  87. p["regexp"] = regexp.MustCompile(strReq)
  88. }
  89. }, func(err interface{}) {
  90. log.Println(err)
  91. })
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. //结果打分
  99. func ScoreFields(j *ju.Job, ftag map[string][]*Tag) map[string][]*ju.ExtField {
  100. qu.Catch()
  101. result := j.Result
  102. for field, tmps := range result {
  103. if field == "projectcode" {
  104. tmps = projectWeightClear(tmps)
  105. }
  106. locktag.Lock()
  107. taglength := len(ftag[field])
  108. locktag.Unlock()
  109. for tmpsindex, tmpsvalue := range tmps {
  110. //没有抽取到值,不打分
  111. if string_value := fmt.Sprint(tmpsvalue.Value); string_value == "" || string_value == "0" || string_value == "<nil>" {
  112. tmps[tmpsindex].Score = -10
  113. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: `value结果为空直接-10分`, Code: field, Value: tmpsvalue.Value, Score: -10})
  114. continue
  115. }
  116. lockscore.Lock()
  117. describe := qu.ObjToString(SoreConfig["extractype"]["describe"])
  118. lockscore.Unlock()
  119. //是否有段标签
  120. if len(tmpsvalue.BlockTag) > 0 {
  121. //有标签段
  122. var qz float64 = 0.0 //取权重最高的
  123. for key := range tmpsvalue.BlockTag {
  124. //key = "其他"//TODO 测试用
  125. lockscore.Lock()
  126. if TagConfig[key][field] > qz {
  127. qz = TagConfig[key][field]
  128. }
  129. lockscore.Unlock()
  130. }
  131. tmps[tmpsindex].Score += ju.FloatFormat(BlockScore*qz, 4) //乘以权重系数
  132. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "匹配段标签权重", Code: "权重系数乘以2", RuleText: "BlockTag", ScoreFrom: "tagscore.json", Value: tmpsvalue.Value, Score: BlockScore * qz})
  133. } else {
  134. //没有段标签,走其他
  135. //qz := TagConfig["其他"][field]
  136. //tmps[tmpsindex].Score += 2 * qz //乘以权重系数
  137. }
  138. //抽取类型打分
  139. lockscore.Lock()
  140. fieldscore := FieldsScore[field]
  141. typescore := float64(0)
  142. titlescore := float64(0)
  143. if fieldscore != nil { //指定抽取属性打分配置
  144. if tmpsvalue.ExtFrom == "title" { //标题打分初始化
  145. titlescore = fieldscore["title"]
  146. }
  147. typescore = fieldscore[tmpsvalue.Type]
  148. } else { //通用抽取属性打分配置
  149. if tmpsvalue.ExtFrom == "title" { //标题打分初始化
  150. titlescore = CommonScore["title"]
  151. }
  152. typescore = CommonScore[tmpsvalue.Type]
  153. }
  154. lockscore.Unlock()
  155. if titlescore > 0 {
  156. tmps[tmpsindex].Score += titlescore
  157. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "title初始化", Code: "fieldscore.title", RuleText: describe, ScoreFrom: "fieldscore.json", Value: tmpsvalue.Value, Score: titlescore})
  158. }
  159. tmps[tmpsindex].Score += typescore
  160. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: tmpsvalue.Type, Code: "fieldscore." + tmpsvalue.Type, RuleText: describe, ScoreFrom: "fieldscore.json", Value: tmpsvalue.Value, Score: typescore})
  161. //kv权重打分
  162. if fieldscore != nil { //指定抽取属性打分配置
  163. if tmpsvalue.Type == "colon" || tmpsvalue.Type == "space" || tmpsvalue.Type == "table" {
  164. if taglength == 0 {
  165. continue
  166. }
  167. weightscore := ju.FloatFormat(float64(qu.Float64All(fieldscore["kvweight"]))+float64(tmps[tmpsindex].Weight)/float64(taglength), 4)
  168. tmps[tmpsindex].Score += weightscore
  169. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "kv权重打分", Code: "kv-weight", RuleText: describe, ScoreFrom: "fieldscore.json", Value: tmpsvalue.Value, Score: weightscore})
  170. } else {
  171. //正则权重,暂不考虑
  172. }
  173. } else {
  174. if tmpsvalue.Type == "colon" || tmpsvalue.Type == "space" || tmpsvalue.Type == "table" {
  175. if taglength == 0 {
  176. continue
  177. }
  178. weightscore := ju.FloatFormat(float64(qu.Float64All(CommonScore["kvweight"]))+float64(tmps[tmpsindex].Weight)/float64(taglength), 4)
  179. tmps[tmpsindex].Score += weightscore
  180. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "kv权重打分", Code: "kv-weight", RuleText: describe, ScoreFrom: "fieldscore.json", Value: tmpsvalue.Value, Score: weightscore})
  181. } else {
  182. //正则权重,暂不考虑
  183. }
  184. }
  185. lockscore.Lock()
  186. scoreRule := SoreConfig[field]
  187. lockscore.Unlock()
  188. if scoreRule == nil {
  189. continue
  190. }
  191. //配置打分
  192. if scoreRule["type"] == "string" {
  193. //1.长度打分
  194. valueLen := utf8.RuneCountInString(fmt.Sprint(tmpsvalue.Value))
  195. if valueLen < 1 {
  196. tmps[tmpsindex].Score = -10
  197. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: `valueLen < 1 && field != "projectscope"直接-10分`, Code: field, Value: tmpsvalue.Value, Score: -10})
  198. continue
  199. }
  200. if valueLen > 100 && field != "projectscope" {
  201. tmps[tmpsindex].Score = -99
  202. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: `valueLen > 100 && field != "projectscope"直接-99分`, Code: field, Value: tmpsvalue.Value, Score: -99})
  203. }
  204. if lengths, ok := scoreRule["length"].([]interface{}); ok {
  205. for _, tmp := range lengths {
  206. if length, ok := tmp.(map[string]interface{}); ok {
  207. if ranges, ok := length["range"].([]interface{}); ok {
  208. gt := qu.IntAll(ranges[0])
  209. lte := qu.IntAll(ranges[1])
  210. if lte < 0 { //∞
  211. lte = 999999
  212. }
  213. score := qu.Float64All(ranges[2])
  214. if valueLen > gt && valueLen <= lte {
  215. tmps[tmpsindex].Score += score
  216. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "长度打分", Code: fmt.Sprint(gt, "<", valueLen, "<=", lte), ScoreFrom: "fieldscore.json.length", Value: tmpsvalue.Value, Score: score})
  217. break
  218. }
  219. }
  220. }
  221. }
  222. }
  223. //2.负面词打分
  224. if positions, ok := scoreRule["negativewords"].([]interface{}); ok {
  225. for _, position := range positions {
  226. if p, ok := position.(map[string]interface{}); ok {
  227. qu.Try(func() {
  228. if p["regexp"] != nil {
  229. reg := p["regexp"].(*regexp.Regexp)
  230. if reg.MatchString(qu.ObjToString(tmpsvalue.Value)) {
  231. tmps[tmpsindex].Score += qu.Float64All(p["score"])
  232. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "负面词打分" + fmt.Sprint(p["describe"]), Code: "negativewords", RuleText: reg.String(), ScoreFrom: "fieldscore.json.negativewords", Value: tmpsvalue.Value, Score: qu.Float64All(p["score"])})
  233. }
  234. }
  235. }, func(err interface{}) {
  236. log.Println(err)
  237. })
  238. }
  239. }
  240. }
  241. //3.正面词打分
  242. if positions, ok := scoreRule["positivewords"].([]interface{}); ok {
  243. for _, position := range positions {
  244. if p, ok := position.(map[string]interface{}); ok {
  245. qu.Try(func() {
  246. if p["regexp"] != nil {
  247. reg := p["regexp"].(*regexp.Regexp)
  248. if reg.MatchString(qu.ObjToString(tmpsvalue.Value)) {
  249. tmps[tmpsindex].Score += qu.Float64All(p["score"])
  250. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "正面词打分" + fmt.Sprint(p["describe"]), Code: "positivewords", RuleText: reg.String(), ScoreFrom: "fieldscore.json.positivewords", Value: tmpsvalue.Value, Score: qu.Float64All(p["score"])})
  251. }
  252. }
  253. }, func(err interface{}) {
  254. log.Println(err)
  255. })
  256. }
  257. }
  258. }
  259. }
  260. //4.数据范围打分
  261. if scoreRule["type"] == "float" {
  262. min := qu.IntAll(scoreRule["min"])
  263. max := qu.IntAll(scoreRule["max"])
  264. val := qu.IntAll(tmpsvalue.Value)
  265. scores, _ := scoreRule["score"].([]interface{})
  266. if len(scores) < 3 || val == 0 {
  267. continue
  268. }
  269. if val < min && 0 < val {
  270. tmps[tmpsindex].Score += qu.Float64All(scores[0])
  271. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "数据范围打分", Code: field + ".float", RuleText: fmt.Sprint(val, "<", min, "&&", 0, "<", val), ScoreFrom: "fieldscore.json." + field, Value: tmpsvalue.Value, Score: qu.Float64All(scores[0])})
  272. } else if val > max {
  273. tmps[tmpsindex].Score += qu.Float64All(scores[2])
  274. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "数据范围打分", Code: field + ".float", RuleText: fmt.Sprint(val, ">", max), ScoreFrom: "fieldscore.json." + field, Value: tmpsvalue.Value, Score: qu.Float64All(scores[2])})
  275. } else if val <= max && val >= min {
  276. tmps[tmpsindex].Score += qu.Float64All(scores[1])
  277. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "数据范围打分", Code: field + ".float", RuleText: fmt.Sprintln(val, "<=", max, "&&", val, ">=", min), ScoreFrom: "fieldscore.json." + field, Value: tmpsvalue.Value, Score: qu.Float64All(scores[1])})
  278. }
  279. }
  280. //其他打分配置
  281. // decimal
  282. if scoreRule["type"] == "decimal" {
  283. min := qu.IntAll(scoreRule["min"])
  284. max := qu.IntAll(scoreRule["max"])
  285. val := qu.IntAll(tmpsvalue.Value)
  286. scores, _ := scoreRule["score"].([]interface{})
  287. if len(scores) < 3 {
  288. continue
  289. }
  290. if val > max {
  291. tmps[tmpsindex].Score += qu.Float64All(scores[2])
  292. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "其他打分配置decimal", Code: field + ".decimal", RuleText: fmt.Sprint(val, ">", max), ScoreFrom: "fieldscore.json." + field, Value: tmpsvalue.Value, Score: qu.Float64All(scores[2])})
  293. } else if val <= max && val > min {
  294. tmps[tmpsindex].Score += qu.Float64All(scores[1])
  295. tmps[tmpsindex].ScoreItem = append(tmps[tmpsindex].ScoreItem, &ju.ScoreItem{Des: "其他打分配置decimal", Code: field + ".decimal", RuleText: fmt.Sprint(val, "<=", max, "&&", val, ">", min), ScoreFrom: "fieldscore.json." + field, Value: tmpsvalue.Value, Score: qu.Float64All(scores[1])})
  296. }
  297. }
  298. }
  299. //计算重复值,并加分=重复数量*乘系数
  300. valrepeat := map[string]int{}
  301. for _, v := range tmps {
  302. valrepeat[fmt.Sprint(v.Value)] += 1
  303. }
  304. for index, v := range tmps {
  305. v.ValRepeat = valrepeat[fmt.Sprint(v.Value)] - 1
  306. if v.ValRepeat > 0 {
  307. score := RepeatScore * float64(v.ValRepeat)
  308. v.Score += score
  309. tmps[index].ScoreItem = append(tmps[index].ScoreItem, &ju.ScoreItem{Des: "重复次数打分repeat", Code: field + ".repeat", RuleText: "repeat:" + fmt.Sprint(v.ValRepeat), ScoreFrom: "fieldscore.json." + field, Value: v.Value, Score: score})
  310. }
  311. v.Score = ju.FloatFormat(v.Score, 4)
  312. }
  313. }
  314. return result
  315. }
  316. //项目编号权重清理
  317. func projectWeightClear(tmps []*ju.ExtField) []*ju.ExtField {
  318. newList := make([]*ju.ExtField, 0)
  319. if len(tmps) < 1 {
  320. return newList
  321. }
  322. ju.Sort(tmps)
  323. tmpWeight := -999 //记录最大权重
  324. tmpIndex := -999 //记录最大权重下标
  325. vmap := make(map[string]int, 0)
  326. for i, v := range tmps {
  327. if v.Weight == 0 {
  328. newList = append(newList, v)
  329. continue
  330. } else if v.Weight > tmpWeight {
  331. tmpWeight = v.Weight
  332. tmpIndex = i
  333. } else if v.Weight == tmpWeight {
  334. if utf8.RuneCountInString(qu.ObjToString(v.Value)) >= 5 && utf8.RuneCountInString(qu.ObjToString(v.Value)) <= 38 && v.Value != tmps[tmpIndex].Value {
  335. vmap[qu.ObjToString(v.Value)] = i
  336. }
  337. }
  338. }
  339. if tmpIndex != -999 {
  340. newList = append(newList, tmps[tmpIndex])
  341. }
  342. if len(vmap) > 0 {
  343. for _, v := range vmap {
  344. newList = append(newList, tmps[v])
  345. }
  346. }
  347. return newList
  348. }