A Go implementation of the Aho-Corasick multiple pattern string search algorithm.
- Supports searching for multiple keywords simultaneously
- Trie structure with automatic fail link construction
- Find all matches or the longest match
- Unicode support
go get github.com/raaaaaaaay86/ahocorasick
package main
import (
"fmt"
"github.com/raaaaaaaay86/ahocorasick"
)
func main() {
dictionary := []string{"hello", "world", "hell", "helloworld", "hellow"}
trie := ahocorasick.NewTrie(dictionary)
matches := trie.FindAllMatches("helloworld")
for _, m := range matches {
fmt.Println(m)
}
}go test -v
trie.go: Main trie structure and construction logicnode.go: Trie node and matching logictrie_test.go: Unit tests