sensitive.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package service
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/anknown/ahocorasick"
  6. "one-api/constant"
  7. "strings"
  8. )
  9. // SensitiveWordContains 是否包含敏感词,返回是否包含敏感词和敏感词列表
  10. func SensitiveWordContains(text string) (bool, []string) {
  11. if len(constant.SensitiveWords) == 0 {
  12. return false, nil
  13. }
  14. checkText := strings.ToLower(text)
  15. // 构建一个AC自动机
  16. m := initAc()
  17. hits := m.MultiPatternSearch([]rune(checkText), false)
  18. if len(hits) > 0 {
  19. words := make([]string, 0)
  20. for _, hit := range hits {
  21. words = append(words, string(hit.Word))
  22. }
  23. return true, words
  24. }
  25. return false, nil
  26. }
  27. // SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本
  28. func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) {
  29. if len(constant.SensitiveWords) == 0 {
  30. return false, nil, text
  31. }
  32. checkText := strings.ToLower(text)
  33. m := initAc()
  34. hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
  35. if len(hits) > 0 {
  36. words := make([]string, 0)
  37. for _, hit := range hits {
  38. pos := hit.Pos
  39. word := string(hit.Word)
  40. text = text[:pos] + "**###**" + text[pos+len(word):]
  41. words = append(words, word)
  42. }
  43. return true, words, text
  44. }
  45. return false, nil, text
  46. }
  47. func initAc() *goahocorasick.Machine {
  48. m := new(goahocorasick.Machine)
  49. dict := readRunes()
  50. if err := m.Build(dict); err != nil {
  51. fmt.Println(err)
  52. return nil
  53. }
  54. return m
  55. }
  56. func readRunes() [][]rune {
  57. var dict [][]rune
  58. for _, word := range constant.SensitiveWords {
  59. word = strings.ToLower(word)
  60. l := bytes.TrimSpace([]byte(word))
  61. dict = append(dict, bytes.Runes(l))
  62. }
  63. return dict
  64. }