words.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "sensitiveWords.udp/util"
  6. "strings"
  7. "unicode"
  8. "unicode/utf8"
  9. )
  10. func dealWithNameScoreRules(name string) (string,bool) {
  11. new_name,new_score,isok :="",float64(0),false
  12. old_name := escapeNew(name)
  13. if old_name=="" {
  14. return "",false
  15. }
  16. query := `{"query":{"bool":{"must":[{"query_string":{"default_field":"unique_qy.name_word","query":"`+old_name+`"}}],"must_not":[],"should":[]}},"from":"0","size":"1"}`
  17. tmp := make(map[string]interface{})
  18. json.Unmarshal([]byte(query),&tmp)
  19. searchResult, err := Client_Es.Search().Index(es_index).Type(es_type).Source(tmp).Do()
  20. if err != nil {
  21. log.Println("ES查询出错",name,old_name)
  22. return "",false
  23. }
  24. resNum := len(searchResult.Hits.Hits)
  25. res := make([]map[string]interface{}, resNum)
  26. if searchResult.Hits != nil {
  27. if resNum < 5000 {
  28. for i, hit := range searchResult.Hits.Hits {
  29. data := make(map[string]interface{},0)
  30. json.Unmarshal(*hit.Source, &data)
  31. res[i] = map[string]interface{}{
  32. "name":data["name"],
  33. "score":*hit.Score,
  34. }
  35. }
  36. } else {
  37. log.Println("查询结果太多,查询到:", resNum, "条")
  38. }
  39. }
  40. if len(res)>0 && res != nil {
  41. //分析分数...取最大
  42. new_name = util.ObjToString(res[0]["name"])
  43. new_score = util.Float64All(res[0]["score"])
  44. }
  45. if new_name!="" { //分析hit比例
  46. total,hit := dealWithWordsRules(name,new_name)
  47. proportion := float64(hit)/float64(total)
  48. if proportion >=1.0 {
  49. isok = true
  50. }else {
  51. if float64(hit)/float64(total)>=0.8 && new_score> 4.0{
  52. isok = true
  53. }
  54. }
  55. }
  56. return new_name,isok
  57. }
  58. //击中数量以及比例
  59. func dealWithWordsRules(info_name string ,source_name string) (int,int){
  60. total,hit :=0,0
  61. //字符串处理,替换指定字符
  62. info_name = strings.ReplaceAll(info_name,"(","")
  63. info_name = strings.ReplaceAll(info_name,")","")
  64. info_name = strings.ReplaceAll(info_name,"(","")
  65. info_name = strings.ReplaceAll(info_name,")","")
  66. source_name = strings.ReplaceAll(source_name,"(","")
  67. source_name = strings.ReplaceAll(source_name,")","")
  68. source_name = strings.ReplaceAll(source_name,"(","")
  69. source_name = strings.ReplaceAll(source_name,")","")
  70. nameArr,_ := calculateWordCount(info_name)
  71. _,total = calculateWordCount(source_name)
  72. for _,v1 := range nameArr {
  73. if strings.Contains(source_name,v1) {
  74. hit++
  75. }
  76. }
  77. return total,hit
  78. }
  79. //分词结果
  80. func calculateWordCount(name string) ([]string,int) {
  81. arr ,space:= make([]string,0),2
  82. total := utf8.RuneCountInString(name)-(space-1)
  83. if name == "" || total<=0 {
  84. return arr,0
  85. }
  86. nameRune := []rune(name)
  87. for i:=0;i<total ;i++ {
  88. new_str := string(nameRune[i:space+i])
  89. arr = append(arr,new_str)
  90. }
  91. return arr,len(arr)
  92. }
  93. //func escape(s string) string {
  94. // news := ""
  95. // s = strings.ReplaceAll(s," ","")
  96. // for _, c := range s {
  97. // //if unicode.Is(unicode.Han, c) || unicode.IsNumber(c) || unicode.IsLetter(c) {
  98. // // news = news + string(c)
  99. // //}else if c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' || c == '|' || c == '&' || c == '/' || c == '#' || c == '@' || c == '(' || c == ')' || c == '>' || c == '<' || c == '“' || c == '”' || c == '?' || c == '、' || c == '.' {
  100. // // a := string([]rune{os.PathSeparator, '\\'})
  101. // // news = news + a + string(c)
  102. // //} else {
  103. // // return ""
  104. // //}
  105. // if c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' || c == '|' || c == '&' || c == '/' || c == '#' || c == '@' || c == '(' || c == ')' || c == '>' || c == '<' || c == '“' || c == '”' || c == '?' || c == '、' || c == '.' {
  106. // a := string([]rune{os.PathSeparator,'\\'})
  107. // //news = news + a + `\` + string(c)
  108. // news = news + a + string(c)
  109. // } else {
  110. // news = news + string(c)
  111. // }
  112. //
  113. // }
  114. // return news
  115. //}
  116. func escapeNew(s string) string {
  117. news := ""
  118. s = strings.ReplaceAll(s," ","")
  119. for _, c := range s {
  120. if unicode.Is(unicode.Han, c) || unicode.IsNumber(c) || unicode.IsLetter(c) {
  121. news = news + string(c)
  122. }
  123. }
  124. return news
  125. }