I have an array of strings and I need to create a suffix tree out of it in Golang. SuffixArray in Golang does not suffice my needs, because it only accepts byte array (i.e of a single string). Could anybody provide pointers for implementation. Thanks in advance.
Here is an example of how to use suffix array to do auto completion. (playground).
Note that I joined all the strings together with a prefix of \x00
which can't occur in the strings first.
package main
import (
"fmt"
"index/suffixarray"
"regexp"
"strings"
)
func main() {
words := []string{
"aardvark",
"happy",
"hello",
"hero",
"he",
"hotel",
}
// use \x00 to start each string
joinedStrings := "\x00" + strings.Join(words, "\x00")
sa := suffixarray.New([]byte(joinedStrings))
// User has typed in "he"
match, err := regexp.Compile("\x00he[^\x00]*")
if err != nil {
panic(err)
}
ms := sa.FindAllIndex(match, -1)
for _, m := range ms {
start, end := m[0], m[1]
fmt.Printf("match = %q\n", joinedStrings[start+1:end])
}
}
Prints
match = "hello"
match = "hero"
match = "he"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With