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.
Install the package with go command:
go get github.com/arkadijs/ahocorasick-goand start using it by importing the dependency:
import "github.com/arkadijs/ahocorasick-go"type Tree struct {
// contains filtered or unexported fields
}The Tree is a root objects that represents compiled state of searched terms.
func New() *TreeAllocates new empty Tree object.
func (tree *Tree) Add(term string) errorAdds search term to the Tree object. The only error returned is TreeAlreadyPrepared.
func (tree *Tree) Search(content string) <-chan stringPrepares the tree and 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 complete result set or you'd like to cancel the search -- please use SearchContext().
func (tree *Tree) SearchContext(ctx context.Context, content string) <-chan stringSame as Search() but with context.Context.
ctx, cancel := context.WithCancel(context.Background())
ch := tree.SearchContext(ctx, "...")
_, found := <- ch
cancel()