Debian Codegraph @

README.md

raw symbols SCIP
1 # Aho-Corasick
2
3 [![Build Status](https://travis-ci.com/BobuSumisu/aho-corasick.svg?token=eGRFn5xdQ7p9yby3GVvc&branch=master)](https://travis-ci.com/BobuSumisu/aho-corasick)
4 ![Go Version](https://img.shields.io/github/go-mod/go-version/BobuSumisu/aho-corasick)
5 ![Latest Tag](https://img.shields.io/github/v/tag/BobuSumisu/aho-corasick)
6
7 Implementation of the Aho-Corasick string-search algorithm in Go.
8
9 Licensed under MIT License.
10
11 ## Details
12
13 This implementation does not use a [Double-Array Trie](https://linux.thai.net/~thep/datrie/datrie.html) as in my
14 [implementation](https://github.com/BobuSumisu/go-ahocorasick) from a couple of years back.
15
16 This reduces the build time drastically, but at the cost of higher memory consumption.
17
18 The search time is still fast, and comparable to other Go implementations I have found on github that claims to be fast
19 (see [performance](#Performance)).
20
21 ## Documentation
22
23 Can be found at [godoc.org](https://godoc.org/github.com/BobuSumisu/aho-corasick).
24
25 ## Example Usage
26
27 Use a `TrieBuilder` to build a `Trie`:
28
29 ```go
30 trie := NewTrieBuilder().
31 AddStrings([]string{"or", "amet"}).
32 Build()
33 ```
34
35 Then go and match something interesting:
36
37 ```go
38 matches := trie.MatchString("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
39 fmt.Printf("Got %d matches.\n", len(matches))
40
41 // => Got 3 matches.
42 ```
43
44 What did we match?
45
46 ```go
47 for _, match := range matches {
48 fmt.Printf("Matched pattern %d %q at position %d.\n", match.Match(),
49 match.Pattern(), match.Pos())
50 }
51
52 // => Matched pattern 0 "or" at position 1.
53 // => Matched pattern 0 "or" at position 15.
54 // => Matched patterh 1 "amet" at position 22.
55 ```
56
57 ## Building
58
59 You can easily load patterns from file:
60
61 ```go
62 builder := NewTrieBuilder()
63 builder.LoadPatterns("patterns.txt")
64 builder.LoadStrings("strings.txt")
65 ```
66
67 Both functions expects a text file with one pattern per line. `LoadPatterns` expects the pattern to
68 be in hexadecimal form.
69
70 ## Storing
71
72 Use `Encode` to store a `Trie` in gzip compressed binary format:
73
74 ```go
75 f, err := os.Create("trie.gz")
76 err := Encode(f, trie)
77 ```
78
79 And `Decode` to load it from binary format:
80
81 ```go
82 f, err := os.Open("trie.gz")
83 trie, err := Decode(f)
84 ```
85
86 ## Performance
87
88 Some simple benchmarking on my machine (Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, 32 GiB RAM).
89
90 Build and search time grows quite linearly with regards to number of patterns and input text length.
91
92 ### Building
93
94 BenchmarkTrieBuild/100-12 10000 0.1460 ms/op
95 BenchmarkTrieBuild/1000-12 1000 2.1643 ms/op
96 BenchmarkTrieBuild/10000-12 100 14.3305 ms/op
97 BenchmarkTrieBuild/100000-12 10 131.2442 ms/op
98
99 ### Searching
100
101 BenchmarkMatchIbsen/100-12 2000000 0.0006 ms/op
102 BenchmarkMatchIbsen/1000-12 300000 0.0042 ms/op
103 BenchmarkMatchIbsen/10000-12 30000 0.0436 ms/op
104 BenchmarkMatchIbsen/100000-12 3000 0.4310 ms/op
105
106 ### Compared to Other Implementation
107
108 See
109 [aho-corasick-benchmark](https://github.com/Bobusumisu/aho-corasick-benchmark).
110
111 ### Memory Usage
112
113 As mentioned, the memory consumption will be quite high compared to a double-array trie
114 implementation. Especially during the build phase (which currently contains a lot of object
115 allocations).