-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmatcher_test.go
173 lines (155 loc) · 3.48 KB
/
matcher_test.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
package cedar
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"testing"
"time"
)
func TestDumpMatcher(t *testing.T) {
fmt.Printf("\ntesting in simple case...\n")
m := NewMatcher()
words := []string{
"she", "he", "her", "hers",
}
for i, word := range words {
m.Insert([]byte(word), i)
}
m.Cedar().DumpGraph("trie.gv")
m.Compile()
m.DumpGraph("dfa.gv")
seq := []byte("hershertongher")
fmt.Printf("searching %s\n", string(seq))
resp := m.Match(seq)
for resp.HasNext() {
items := resp.NextMatchItem(seq)
for _, itr := range items {
key := m.Key(seq, itr)
fmt.Printf("key:%s value:%d\n", key, itr.Value.(int))
}
}
resp.Release()
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
fmt.Printf("%s took %s\n", name, elapsed)
}
func readBytes(filename string) ([][]byte, error) {
dict := [][]byte{}
f, err := os.OpenFile(filename, os.O_RDONLY, 0660)
if err != nil {
return nil, err
}
r := bufio.NewReader(f)
for {
l, err := r.ReadBytes('\n')
if err != nil || err == io.EOF {
break
}
l = bytes.TrimSpace(l)
dict = append(dict, l)
}
return dict, nil
}
func calcTime(start time.Time, name string) {
elapsed := time.Since(start)
fmt.Printf("%s took %s\n", name, elapsed)
}
func testIohub(t *testing.T, dictName, textName string) {
t.Helper()
dict, err := readBytes(dictName)
if err != nil {
t.Fatal(err)
}
content, err := ioutil.ReadFile(textName)
if err != nil {
t.Fatal(err)
}
m := NewMatcher()
func() {
defer calcTime(time.Now(), "iohub/ahocorasick [build]")
for i, b := range dict {
m.Insert(b, i)
}
m.Compile()
}()
func() {
defer calcTime(time.Now(), "iohub/ahocorasick [match]")
clen := len(content)
tlen := 0
for s := 0; clen > 0; s += tlen {
tlen := DefaultTokenBufferSize / 2
if clen < tlen {
tlen = clen
}
text := content[s:tlen]
m.Match(text)
clen -= tlen
}
}()
}
func TestWithDict(t *testing.T) {
zhDict := "./benchmark/cn/dictionary.txt"
zhText := "./benchmark/cn/text.txt"
testIohub(t, zhDict, zhText)
}
func TestMatcher(t *testing.T) {
fmt.Printf("\ntesting Insert & Compile & Search in big dictionary...\n")
m := NewMatcher()
fmt.Println("Loading...")
dict := loadDict(t)
size := len(dict)
fmt.Printf("%d key-value pairs in dictionary\n", size)
fmt.Println("Inserting...")
func() {
defer timeTrack(time.Now(), "Insert")
for i := 0; i < size; i++ {
m.Insert(dict[i].key, i)
}
}()
// m.da.DumpGraph("bigtrie.py")
fmt.Println("Compile...")
func() {
defer timeTrack(time.Now(), "Compile")
m.Compile()
}()
// m.DumpGraph("bigdfa.py")
seq := []byte("一丁点C++的T桖中华人民共和国人民解放军军队看守南京长江大桥")
fmt.Printf("Searching %s\n", string(seq))
resp := m.Match(seq)
for resp.HasNext() {
for _, item := range resp.NextMatchItem(seq) {
key := m.Key(seq, item)
fmt.Printf("key:%s value:%d\n", key, item.Value.(int))
}
}
resp.Release()
}
func TestHugeMatching(t *testing.T) {
m := NewMatcher()
cLen := 1024 * 12
fmt.Printf("\ntesting in near [%v]^2 matchs case...\n", cLen)
content := "a"
for ix := 0; ix < cLen; ix++ {
m.Insert([]byte(content), ix)
content += "a"
}
m.Compile()
seq := []byte(content)
resp := m.Match(seq)
ix := 0
for resp.HasNext() {
for _, item := range resp.NextMatchItem(seq) {
ix++
key := m.Key(seq, item)
if ix%1000 == 0 && len(key) != 0 {
// fmt.Printf("key Len:%v value:%d\n", len(key), item.Value.(int))
}
}
}
resp.Release()
fmt.Println("done")
}