Create a suffix tree in golang

I have an array of strings and I need to create a suffix tree from this in the Golang. SuffixArray in Golang does not satisfy my needs because it accepts only an array of bytes (i.e. One line). Can anyone provide pointers for implementation. Thanks in advance.

+4
source share
2 answers

Here is an example of how to use a suffix array for automatic completion. ( playground ).

Note that I appended all lines along with a prefix \x00that cannot happen on the first lines.

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])
    }
}

Print

match = "hello"
match = "hero"
match = "he"
+5
source

, , . (, ), . "hello world" :

match, err := regexp.Compile("[^\x00]*wor[^\x00]*")

"wor". , joinedStrings[start:end].

+1

Source: https://habr.com/ru/post/1532924/


All Articles