-
Notifications
You must be signed in to change notification settings - Fork 5
/
dictionary.go
73 lines (61 loc) · 1.51 KB
/
dictionary.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package fname contains functions for generating random, human-friendly names.
package fname
import (
"bufio"
_ "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.
func NewDictionary() *Dictionary {
// TODO: allow for custom dictionary
return &Dictionary{
adjectives: adjective,
adverbs: adverb,
nouns: noun,
verbs: verb,
}
}
// 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 {
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}