str.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "bytes"
  4. "fmt"
  5. goahocorasick "github.com/anknown/ahocorasick"
  6. "strings"
  7. )
  8. func SundaySearch(text string, pattern string) bool {
  9. // 计算偏移表
  10. offset := make(map[rune]int)
  11. for i, c := range pattern {
  12. offset[c] = len(pattern) - i
  13. }
  14. // 文本串长度和模式串长度
  15. n, m := len(text), len(pattern)
  16. // 主循环,i表示当前对齐的文本串位置
  17. for i := 0; i <= n-m; {
  18. // 检查子串
  19. j := 0
  20. for j < m && text[i+j] == pattern[j] {
  21. j++
  22. }
  23. // 如果完全匹配,返回匹配位置
  24. if j == m {
  25. return true
  26. }
  27. // 如果还有剩余字符,则检查下一位字符在偏移表中的值
  28. if i+m < n {
  29. next := rune(text[i+m])
  30. if val, ok := offset[next]; ok {
  31. i += val // 存在于偏移表中,进行跳跃
  32. } else {
  33. i += len(pattern) + 1 // 不存在于偏移表中,跳过整个模式串长度
  34. }
  35. } else {
  36. break
  37. }
  38. }
  39. return false // 如果没有找到匹配,返回-1
  40. }
  41. func RemoveDuplicate(s []string) []string {
  42. result := make([]string, 0, len(s))
  43. temp := map[string]struct{}{}
  44. for _, item := range s {
  45. if _, ok := temp[item]; !ok {
  46. temp[item] = struct{}{}
  47. result = append(result, item)
  48. }
  49. }
  50. return result
  51. }
  52. func InitAc(words []string) *goahocorasick.Machine {
  53. m := new(goahocorasick.Machine)
  54. dict := readRunes(words)
  55. if err := m.Build(dict); err != nil {
  56. fmt.Println(err)
  57. return nil
  58. }
  59. return m
  60. }
  61. func readRunes(words []string) [][]rune {
  62. var dict [][]rune
  63. for _, word := range words {
  64. word = strings.ToLower(word)
  65. l := bytes.TrimSpace([]byte(word))
  66. dict = append(dict, bytes.Runes(l))
  67. }
  68. return dict
  69. }
  70. func AcSearch(findText string, dict []string, stopImmediately bool) (bool, []string) {
  71. if len(dict) == 0 {
  72. return false, nil
  73. }
  74. if len(findText) == 0 {
  75. return false, nil
  76. }
  77. m := InitAc(dict)
  78. if m == nil {
  79. return false, nil
  80. }
  81. hits := m.MultiPatternSearch([]rune(findText), stopImmediately)
  82. if len(hits) > 0 {
  83. words := make([]string, 0)
  84. for _, hit := range hits {
  85. words = append(words, string(hit.Word))
  86. }
  87. return true, words
  88. }
  89. return false, nil
  90. }