How to group words in a string by the first N letters in Go

2 Answers

0 votes
package main

import (
    "fmt"
    "regexp"
    "strings"
)

func groupByFirstNLetters(s string, n int) map[string][]string {
    re := regexp.MustCompile(`[A-Za-z]+`)
    words := re.FindAllString(strings.ToLower(s), -1)

    groups := make(map[string][]string)

    for _, w := range words {
        if len(w) >= n {
            prefix := w[:n]
            groups[prefix] = append(groups[prefix], w)
        }
    }

    return groups
}

func main() {
    s := "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees."

    groups := groupByFirstNLetters(s, 3)

    // Print version 1
    for prefix, words := range groups {
        fmt.Println(prefix, ":", words)
    }

    fmt.Println()

    // Print version 2
    for prefix, words := range groups {
        fmt.Printf("%s: %s\n", prefix, strings.Join(words, ", "))
    }
}



/*
run:

wer : [were]
sur : [surprised]
see : [see]
bra : [branches]
tre : [trees]
the : [the the the the]
low : [lowly lowland lower]
inh : [inhabitants]

wer: were
sur: surprised
see: see
bra: branches
tre: trees
the: the, the, the, the
low: lowly, lowland, lower
inh: inhabitants

*/

 



answered Mar 13 by avibootz
0 votes
package main

import (
    "fmt"
    "regexp"
    "strings"
)

func groupByFirstNLetters(s string, n int) map[string][]string {
    re := regexp.MustCompile(`[A-Za-z]+`)
    return func(words []string) map[string][]string {
        groups := make(map[string][]string)
        for _, w := range words {
            if len(w) >= n {
                groups[w[:n]] = append(groups[w[:n]], w)
            }
        }
        return groups
    }(re.FindAllString(strings.ToLower(s), -1))
}

func main() {
    s := "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees."

    groups := groupByFirstNLetters(s, 3)

    // Print version 1
    for prefix, words := range groups {
        fmt.Println(prefix, ":", words)
    }

    fmt.Println()

    // Print version 2
    for prefix, words := range groups {
        fmt.Printf("%s: %s\n", prefix, strings.Join(words, ", "))
    }
}



/*
run:

wer : [were]
sur : [surprised]
see : [see]
bra : [branches]
tre : [trees]
the : [the the the the]
low : [lowly lowland lower]
inh : [inhabitants]

tre: trees
the: the, the, the, the
low: lowly, lowland, lower
inh: inhabitants
wer: were
sur: surprised
see: see
bra: branches

*/

 



answered Mar 13 by avibootz
...