words.go 3.9 KB

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