words.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "sensitiveWords.udp/util"
  7. "strings"
  8. "unicode/utf8"
  9. )
  10. func dealWithNameScoreRules(name string) (string,bool) {
  11. new_name,new_score,isok :="",float64(0),false
  12. old_name := escape(name)
  13. if old_name=="" {
  14. return "",false
  15. }
  16. query := `{"query":{"bool":{"must":[{"query_string":{"default_field":"azktest.name_2","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. new_name = util.ObjToString(res[0]["name"])
  42. new_score = util.Float64All(res[0]["score"])
  43. }
  44. if new_name!="" { //分析hit比例
  45. total,hit := dealWithWordsRules(name,new_name)
  46. proportion := float64(hit)/float64(total)
  47. if proportion >=1.0 {
  48. isok = true
  49. }else {
  50. if float64(hit)/float64(total)>=0.8 && new_score> 4.0{
  51. isok = true
  52. }
  53. }
  54. }
  55. return new_name,isok
  56. }
  57. //击中数量以及比例
  58. func dealWithWordsRules(info_name string ,source_name string) (int,int){
  59. total,hit :=0,0
  60. //字符串处理,替换指定字符
  61. info_name = strings.ReplaceAll(info_name,"(","")
  62. info_name = strings.ReplaceAll(info_name,")","")
  63. info_name = strings.ReplaceAll(info_name,"(","")
  64. info_name = strings.ReplaceAll(info_name,")","")
  65. source_name = strings.ReplaceAll(source_name,"(","")
  66. source_name = strings.ReplaceAll(source_name,")","")
  67. source_name = strings.ReplaceAll(source_name,"(","")
  68. source_name = strings.ReplaceAll(source_name,")","")
  69. nameArr,_ := calculateWordCount(info_name)
  70. _,total = calculateWordCount(source_name)
  71. for _,v1 := range nameArr {
  72. if strings.Contains(source_name,v1) {
  73. hit++
  74. }
  75. }
  76. return total,hit
  77. }
  78. //分词结果
  79. func calculateWordCount(name string) ([]string,int) {
  80. arr ,space:= make([]string,0),2
  81. total := utf8.RuneCountInString(name)-(space-1)
  82. if name == "" || total<=0 {
  83. return arr,0
  84. }
  85. nameRune := []rune(name)
  86. for i:=0;i<total ;i++ {
  87. new_str := string(nameRune[i:space+i])
  88. arr = append(arr,new_str)
  89. }
  90. return arr,len(arr)
  91. }
  92. func escape(s string) string {
  93. news := ""
  94. s = strings.ReplaceAll(s," ","")
  95. for _, c := range s {
  96. //if unicode.Is(unicode.Han, c) || unicode.IsNumber(c) || unicode.IsLetter(c) {
  97. // news = news + string(c)
  98. //}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 == '.' {
  99. // a := string([]rune{os.PathSeparator, '\\'})
  100. // news = news + a + string(c)
  101. //} else {
  102. // return ""
  103. //}
  104. 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 == '.' {
  105. a := string([]rune{os.PathSeparator, '\\'})
  106. news = news + a + string(c)
  107. } else {
  108. news = news + string(c)
  109. }
  110. }
  111. return news
  112. }