Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a suffix tree in golang

Tags:

go

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.

like image 668
BigDataLearner Avatar asked Jan 10 '23 23:01

BigDataLearner


1 Answers

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"
like image 132
Nick Craig-Wood Avatar answered Jan 30 '23 18:01

Nick Craig-Wood