prefixtree.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package backend
  2. import (
  3. "strings"
  4. "unicode/utf8"
  5. )
  6. // TrieNode represents each node in the Trie
  7. type TrieNode struct {
  8. children map[rune]*TrieNode
  9. isEnd bool
  10. }
  11. // NewTrieNode creates a new TrieNode
  12. func NewTrieNode() *TrieNode {
  13. return &TrieNode{
  14. children: make(map[rune]*TrieNode),
  15. isEnd: false,
  16. }
  17. }
  18. // Trie represents the Trie data structure
  19. type Trie struct {
  20. root *TrieNode
  21. }
  22. // NewTrie creates a new Trie
  23. func NewTrie() *Trie {
  24. return &Trie{root: NewTrieNode()}
  25. }
  26. // Insert inserts a word into the Trie
  27. func (t *Trie) Insert(word string) {
  28. node := t.root
  29. for _, char := range word {
  30. if _, ok := node.children[char]; !ok {
  31. node.children[char] = NewTrieNode()
  32. }
  33. node = node.children[char]
  34. }
  35. node.isEnd = true
  36. }
  37. // SearchInText searches for words in the text that are present in the Trie
  38. func (t *Trie) SearchInText(text string) []string {
  39. var foundWords []string
  40. node := t.root
  41. var wordBuilder []rune
  42. for i, w := 0, 0; i < len(text); i += w {
  43. runeValue, width := utf8.DecodeRuneInString(text[i:])
  44. w = width
  45. if runeValue == utf8.RuneError {
  46. continue
  47. }
  48. if _, ok := node.children[runeValue]; !ok {
  49. node = t.root
  50. wordBuilder = wordBuilder[:0]
  51. continue
  52. }
  53. node = node.children[runeValue]
  54. wordBuilder = append(wordBuilder, runeValue)
  55. if node.isEnd {
  56. foundWords = append(foundWords, string(wordBuilder))
  57. // Continue to search for other words starting from this node
  58. node = t.root
  59. wordBuilder = wordBuilder[:0]
  60. }
  61. }
  62. return foundWords
  63. }
  64. // SearchInText searches for words in the text that are present in the Trie
  65. func (t *Trie) HasKeyword(text string) bool {
  66. node := t.root
  67. var wordBuilder []rune
  68. for i, w := 0, 0; i < len(text); i += w {
  69. runeValue, width := utf8.DecodeRuneInString(text[i:])
  70. w = width
  71. if runeValue == utf8.RuneError {
  72. continue
  73. }
  74. if _, ok := node.children[runeValue]; !ok {
  75. node = t.root
  76. wordBuilder = wordBuilder[:0]
  77. continue
  78. }
  79. node = node.children[runeValue]
  80. wordBuilder = append(wordBuilder, runeValue)
  81. if node.isEnd {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. // BatchInsert
  88. func (t *Trie) BatchInsert(words string) {
  89. for _, s := range strings.Split(words, ";") {
  90. t.Insert(s)
  91. }
  92. }