-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaton.go
153 lines (125 loc) · 2.63 KB
/
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package automaton
import (
"container/list"
// "fmt"
)
type CheckResult struct {
StartIndex int
EndIndex int
TokenID int
}
type Automaton interface {
Check(src []byte) (result []CheckResult)
// Print()
}
func NewAutomaton(worlds []string) Automaton {
wordMap := make(map[int][]byte)
for i, w := range worlds {
wordMap[i] = []byte(w)
}
instance := &engine{
rootNode: newNode(),
wordMap: wordMap,
}
instance.buildPrefixTree()
instance.buildMismatchPointer()
return instance
}
var (
//default TokenID = -1
defaultTokenID = -1
nodeID = 0
)
type node struct {
id int
fail *node
nextNodeMap map[byte]*node
tokenID int
worldLen int
}
func newNode() (n *node) {
n = &node{
nextNodeMap: make(map[byte]*node),
tokenID: defaultTokenID,
id: nodeID + 1,
}
nodeID++
return
}
type engine struct {
rootNode *node
wordMap map[int][]byte //tokenID => world
}
func (e *engine) buildPrefixTree() {
for tokenID, world := range e.wordMap {
curNode := e.rootNode
for _, c := range world {
n, ok := curNode.nextNodeMap[c]
if !ok {
n = newNode()
curNode.nextNodeMap[c] = n
}
curNode = n
}
curNode.tokenID = tokenID
curNode.worldLen = len(world)
}
}
func (e *engine) buildMismatchPointer() {
ll := list.New()
ll.PushFront(e.rootNode)
for ll.Len() > 0 {
curNode := ll.Remove(ll.Back()).(*node)
for c, n := range curNode.nextNodeMap {
n.fail = e.rootNode
if curNode != e.rootNode {
for p := curNode.fail; p != nil; p = p.fail {
if n1, ok := p.nextNodeMap[c]; ok {
n.fail = n1
break
}
}
}
ll.PushFront(n)
}
}
}
func (e *engine) Check(src []byte) (result []CheckResult) {
result = e.check(src)
return
}
func (e *engine) check(src []byte) (result []CheckResult) {
curNode := e.rootNode
for k, v := range src {
for curNode.nextNodeMap[v] == nil && curNode != e.rootNode {
curNode = curNode.fail
}
curNode = curNode.nextNodeMap[v]
if curNode == nil {
curNode = e.rootNode
}
if curNode != e.rootNode {
if curNode.tokenID != defaultTokenID {
//hit world
result = append(result, CheckResult{
StartIndex: k - curNode.worldLen + 1,
EndIndex: k + 1,
TokenID: curNode.tokenID,
})
}
}
}
return
}
// func (e *engine) Print() {
// ll := list.New()
// ll.PushFront(e.rootNode)
// for ll.Len() > 0 {
// curNode := ll.Remove(ll.Back()).(*node)
// fmt.Printf("[%d],%d,%+v,%d\n", curNode.id, curNode.tokenID, curNode.nextNodeMap, len(curNode.nextNodeMap))
// for c, v := range curNode.nextNodeMap {
// ll.PushFront(v)
// fmt.Printf("{%c}\n", c)
// }
// }
// }