-
Notifications
You must be signed in to change notification settings - Fork 3
/
ac_automachine.go
184 lines (160 loc) · 2.84 KB
/
ac_automachine.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package lad
import (
"bufio"
"container/list"
"errors"
"fmt"
"io"
"os"
"strings"
)
const rootRaw = "/"
type acNode struct {
raw string
children map[string]*acNode
isEnd bool
length int
fail *acNode
}
func NewAcNode(raw string) *acNode {
return &acNode{
raw: raw,
children: make(map[string]*acNode),
}
}
func (an *acNode) view() {
fmt.Println(an.raw, ",", an.length, ",fail:", an.fail)
if len(an.children) == 0 {
return
}
for _, node := range an.children {
node.view()
}
}
type acMachine struct {
root *acNode
}
func New() *acMachine {
root := NewAcNode(rootRaw)
return &acMachine{
root: root,
}
}
// Add 添加模式串
func (ac *acMachine) Add(pattern string) {
p := ac.root
tok := newToken(pattern)
length := 0
for str := tok.next(); str != ""; str = tok.next() {
if _, ok := p.children[str]; !ok {
newNode := NewAcNode(str)
p.children[str] = newNode
}
p = p.children[str]
length++
}
p.length = length
p.isEnd = true
}
// Load 加载文件
func (ac *acMachine) Load(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
ac.Add(line)
}
}
// Build 构建自动机
func (ac *acMachine) Build() {
l := list.New()
l.PushBack(ac.root)
for l.Len() > 0 {
e := l.Front()
l.Remove(e)
p := e.Value.(*acNode)
for _, pc := range p.children {
if p == ac.root {
pc.fail = ac.root
} else {
q := p.fail
for q != nil {
if qc, ok := q.children[pc.raw]; ok {
pc.fail = qc
break
}
q = q.fail
}
if q == nil {
pc.fail = ac.root
}
}
l.PushBack(pc)
}
}
}
// Find 查找
func (ac *acMachine) Find(text string) []string {
rs := make([]string, 0)
ac.match(text, func(tok *token, node *acNode) {
rs = append(rs, tok.prevNStr(tok.index, node.length))
})
return rs
}
// Match 匹配
func (ac *acMachine) Match(text string) bool {
rs := false
ac.match(text, func(tok *token, node *acNode) {
rs = true
})
return rs
}
// Replace 替换
func (ac *acMachine) Replace(text, target string) string {
rs := ""
ac.match(text, func(tok *token, node *acNode) {
if rs == "" {
rs = string(tok.origin)
}
rs = strings.Replace(rs, tok.prevNStr(tok.index, node.length), target, -1)
})
return rs
}
func (ac *acMachine) match(text string, fn func(tok *token, node *acNode)) {
p := ac.root
tok := newToken(text)
for {
str := tok.next()
if str == "" {
break
}
for {
if _, ok := p.children[str]; !ok && p != ac.root {
p = p.fail
continue
}
break
}
p = p.children[str]
if p == nil {
p = ac.root
}
tmp := p
for tmp != ac.root {
if tmp.isEnd {
fn(tok, tmp)
}
tmp = tmp.fail
}
}
}