This is an implementation of Aho-Corasick string matching algorithm in Google Go. Based on original BSD licensed implementation by Danny Yoo from UC Berkeley.
BSD license.
Start using it by importing the dependency:
import "github.com/arkadijs/ahocorasick-go"
then install the package with go
command:
$ go get
The name of the package is ahocorasick
.
type Tree struct {
// contains filtered or unexported fields
}
The Tree
is a root objects that represents compiled state of searched terms.
func New() *Tree
Allocates new empty Tree
object.
func (tree *Tree) Add(term string) error
Adds search term
to the Tree
object.
func (tree *Tree) Search(content string) <-chan string
Starts search of all Tree
terms in the content
. Returns Go channel the found terms could be read from.
tree := ahocorasick.New()
tree.Add("moo")
tree.Add("one")
for term := range tree.Search("one moon ago") {
fmt.Printf("found %v\n", term)
}
In case you don't need the results, please close the channel -- or search goroutine may stuck:
ch := tree.Search("...")
_, found := <-ch
fmt.Printf("found? %v\n", found)
close(ch)