-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
58 lines (47 loc) · 1.44 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package wotd
import (
"errors"
"math/rand"
"strings"
)
//go:generate go-bindata -o assets/assets.go -pkg assets -ignore assets.go -prefix assets/ assets
var (
// ErrAdjectiveNotFound is returned when an adjective with the given first letter cannot be found.
ErrAdjectiveNotFound = errors.New("adjective not found")
// ErrTooShort is returned when the input to Generate() is empty.
ErrTooShort = errors.New("too short")
)
// Generator represents a word generator that returns a phrase based on a given word.
type Generator struct {
words map[string][]string // words by first letter
}
// NewGenerator returns a new instance of Generator with the given word set.
func NewGenerator(words []string) *Generator {
// Group words by first letter.
m := make(map[string][]string)
for _, w := range words {
// Skip blank words.
if len(w) == 0 {
continue
}
// Append word by initial letter.
letter := string(w[0])
m[letter] = append(m[letter], w)
}
return &Generator{words: m}
}
// Generate returns a phrase based on word.
func (g *Generator) Generate(word string) (string, error) {
// Return an error if a blank string is passed in.
if len(word) == 0 {
return "", ErrTooShort
}
// Retrieve a list of adjectives starting with the first letter.
a := g.words[strings.ToLower(string(word[0]))]
if len(a) == 0 {
return "", ErrAdjectiveNotFound
}
// Randomly choose a word.
adj := a[rand.Intn(len(a))]
return strings.ToLower(adj), nil
}