-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaho_automaton.go
102 lines (96 loc) · 1.87 KB
/
aho_automaton.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package textalg
import "github.com/eapache/queue"
// TrieNode ac自动机节点
type TrieNode struct {
value rune
next map[rune]*TrieNode
fail *TrieNode
emit string
}
func newNode(ch rune) (node *TrieNode) {
node = new(TrieNode)
node.value = ch
node.next = map[rune]*TrieNode{}
return node
}
// AcTrie ac自动机匹配字符串算法
type AcTrie struct {
root *TrieNode
}
// Search 返回匹配的字符串
func (ac *AcTrie) Search(s string) (list []string, index []int) {
node := ac.root
for i, c := range []rune(s) {
matched := true
for {
_, ok := node.next[c]
if ok {
break
}
if node.fail == nil {
matched = false
node = ac.root
break
}
node = node.fail
}
if !matched {
continue
}
node = node.next[c]
p := node
for p != nil {
if p.emit != "" {
list = append(list, p.emit)
index = append(index, i+1)
}
p = p.fail
}
}
return list, index
}
//BuildAcTrie 构建一个 ac 自动机
func BuildAcTrie(words []string) (acTrie *AcTrie) {
acTrie = new(AcTrie)
acTrie.root = newNode(rune('r'))
for _, word := range words {
node := acTrie.root
for _, ch := range []rune(word) {
if _, ok := node.next[ch]; !ok {
node.next[ch] = newNode(ch)
}
node = node.next[ch]
}
node.emit = word
}
queue := queue.New()
queue.Add([]*TrieNode{acTrie.root, nil})
for queue.Length() > 0 {
nodeParent := queue.Remove().([]*TrieNode)
curr, parent := nodeParent[0], nodeParent[1]
for _, sub := range curr.next {
queue.Add([]*TrieNode{sub, curr})
}
if parent == nil {
continue
}
if parent == acTrie.root {
curr.fail = acTrie.root
} else {
fail := parent.fail
for fail != nil {
_, ok := fail.next[curr.value]
if ok {
break
}
fail = fail.fail
}
if fail != nil {
curr.fail = fail.next[curr.value]
} else {
curr.fail = acTrie.root
}
}
}
return acTrie
}