-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdictionary.go
More file actions
85 lines (72 loc) · 1.91 KB
/
dictionary.go
File metadata and controls
85 lines (72 loc) · 1.91 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Package fname contains functions for generating random, human-friendly names.
package fname
import (
_ "embed"
"strings"
)
//go:embed data/adjective
var _adjective string
var adjective = split(_adjective)
//go:embed data/adverb
var _adverb string
var adverb = split(_adverb)
//go:embed data/noun
var _noun string
var noun = split(_noun)
//go:embed data/verb
var _verb string
var verb = split(_verb)
// Dictionary is a collection of words.
type Dictionary struct {
adjectives []string
adverbs []string
nouns []string
verbs []string
}
// NewDictionary creates a new Dictionary backed by the default embedded word lists.
// To use custom word lists, use NewCustomDictionary and pass it via WithDictionary.
func NewDictionary() *Dictionary {
return &Dictionary{
adjectives: adjective,
adverbs: adverb,
nouns: noun,
verbs: verb,
}
}
// NewCustomDictionary creates a Dictionary with caller-supplied word lists.
// Any nil slice falls back to the corresponding default embedded word list.
func NewCustomDictionary(adjectives, adverbs, nouns, verbs []string) *Dictionary {
d := NewDictionary()
if adjectives != nil {
d.adjectives = adjectives
}
if adverbs != nil {
d.adverbs = adverbs
}
if nouns != nil {
d.nouns = nouns
}
if verbs != nil {
d.verbs = verbs
}
return d
}
// LengthAdjective returns the number of adjectives in the dictionary.
func (d *Dictionary) LengthAdjective() int {
return len(d.adjectives)
}
// LengthAdverb returns the number of adverbs in the dictionary.
func (d *Dictionary) LengthAdverb() int {
return len(d.adverbs)
}
// LengthNoun returns the number of nouns in the dictionary.
func (d *Dictionary) LengthNoun() int {
return len(d.nouns)
}
// LengthVerb returns the number of verbs in the dictionary.
func (d *Dictionary) LengthVerb() int {
return len(d.verbs)
}
func split(s string) []string {
return strings.Split(strings.TrimRight(s, "\n"), "\n")
}