-
Notifications
You must be signed in to change notification settings - Fork 48
/
ahocorasick_test.go
115 lines (100 loc) · 2.35 KB
/
ahocorasick_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
package goahocorasick
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"testing"
)
func Read(filename string) ([][]rune, error) {
dict := [][]rune{}
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, bytes.Runes(l))
}
return dict, nil
}
func TestBuild(t *testing.T) {
keywords, err := Read("test_keywords_eng")
if err != nil {
t.Error(err)
}
m := new(Machine)
m.Build(keywords)
//m.PrintFailure()
//m.PrintOutput()
}
func TestMultiPatternSearchEnglish(t *testing.T) {
fmt.Printf("===> MultiPattern Search For English \n")
keywords, err := Read("test_keywords_eng")
if err != nil {
t.Error(err)
}
m := new(Machine)
m.Build(keywords)
//m.PrintFailure()
//m.PrintOutput()
content := []rune("ushers")
terms := m.MultiPatternSearch(content, false)
for _, term := range terms {
fmt.Printf("find %s @%d in %s\n", string(term.Word), term.Pos, string(content))
}
fmt.Printf("\n")
}
func TestMultiPatternSearchChinese(t *testing.T) {
fmt.Printf("===> MultiPattern Search For Chinese \n")
keywords, err := Read("test_keywords_chn")
if err != nil {
t.Error(err)
}
m := new(Machine)
m.Build(keywords)
//m.PrintFailure()
//m.PrintOutput()
content := []rune("你不会想到阿拉伯人会踢出阿根廷风格的足球更何况是埃及风格")
terms := m.MultiPatternSearch(content, false)
for _, term := range terms {
fmt.Printf("find %s @%d in %s\n", string(term.Word), term.Pos, string(content))
}
fmt.Printf("\n")
}
func TestExactSearchEnglish(t *testing.T) {
fmt.Printf("===> Exact Search For English\n")
keywords, err := Read("test_keywords_eng")
if err != nil {
t.Error(err)
}
m := new(Machine)
m.Build(keywords)
for _, k := range keywords {
if m.ExactSearch(k) == nil {
t.Error("exact search chinese failed")
}
}
fmt.Printf("Test total:%d words\n\n", len(keywords))
}
func TestExactSearchChinese(t *testing.T) {
fmt.Printf("===> Exact Search For Chinese\n")
keywords, err := Read("test_keywords_chn")
if err != nil {
t.Error(err)
}
m := new(Machine)
m.Build(keywords)
for _, k := range keywords {
if m.ExactSearch(k) == nil {
t.Error("exact search chinese failed")
}
}
fmt.Printf("Test total:%d words\n\n", len(keywords))
}