22package fname
33
44import (
5- "bufio"
6- "embed"
7- "fmt"
5+ _ "embed"
6+ "strings"
87)
98
10- const (
11- adjectiveFilePath = "data/adjective"
12- adverbFilePath = "data/adverb"
13- nounFilePath = "data/noun"
14- verbFilePath = "data/verb"
15- )
9+ //go:embed data/adjective
10+ var _adjective string
11+ var adjective = strings .Split (_adjective , "\n " )
12+
13+ //go:embed data/adverb
14+ var _adverb string
15+ var adverb = strings .Split (_adverb , "\n " )
1616
17- //go:embed data/*
18- var dataFS embed.FS
17+ //go:embed data/noun
18+ var _noun string
19+ var noun = strings .Split (_noun , "\n " )
20+
21+ //go:embed data/verb
22+ var _verb string
23+ var verb = strings .Split (_verb , "\n " )
1924
2025// Dictionary is a collection of words.
2126type Dictionary struct {
@@ -28,61 +33,12 @@ type Dictionary struct {
2833// NewDictionary creates a new dictionary.
2934func NewDictionary () * Dictionary {
3035 // TODO: allow for custom dictionary
31- a , err := loadFile (adjectiveFilePath )
32- if err != nil {
33- panic (err )
34- }
35- av , err := loadFile (adverbFilePath )
36- if err != nil {
37- panic (err )
38- }
39- n , err := loadFile (nounFilePath )
40- if err != nil {
41- panic (err )
42- }
43- v , err := loadFile (verbFilePath )
44- if err != nil {
45- panic (err )
46- }
47-
4836 return & Dictionary {
49- adectives : a ,
50- adverbs : av ,
51- nouns : n ,
52- verbs : v ,
53- }
54- }
55-
56- // Adjective returns a random adjective.
57- func (d * Dictionary ) Adjective (idx int ) (string , error ) {
58- if idx < 0 || idx >= len (d .adectives ) {
59- return "" , fmt .Errorf ("index out of range: %d" , idx )
60- }
61- return d .adectives [idx ], nil
62- }
63-
64- // Adverb returns a random adverb.
65- func (d * Dictionary ) Adverb (idx int ) (string , error ) {
66- if idx < 0 || idx >= len (d .adverbs ) {
67- return "" , fmt .Errorf ("index out of range: %d" , idx )
37+ adectives : adjective ,
38+ adverbs : adverb ,
39+ nouns : noun ,
40+ verbs : verb ,
6841 }
69- return d .adverbs [idx ], nil
70- }
71-
72- // Noun returns a random noun.
73- func (d * Dictionary ) Noun (idx int ) (string , error ) {
74- if idx < 0 || idx >= len (d .nouns ) {
75- return "" , fmt .Errorf ("index out of range: %d" , idx )
76- }
77- return d .nouns [idx ], nil
78- }
79-
80- // Verb returns a random verb.
81- func (d * Dictionary ) Verb (idx int ) (string , error ) {
82- if idx < 0 || idx >= len (d .verbs ) {
83- return "" , fmt .Errorf ("index out of range: %d" , idx )
84- }
85- return d .verbs [idx ], nil
8642}
8743
8844// LengthAdjective returns the number of adjectives in the dictionary.
@@ -104,26 +60,3 @@ func (d *Dictionary) LengthNoun() int {
10460func (d * Dictionary ) LengthVerb () int {
10561 return len (d .verbs )
10662}
107-
108- // loadFile loads a file from the embedded filesystem, and returns a slice of strings containing each line.
109- func loadFile (path string ) ([]string , error ) {
110- f , err := dataFS .Open (path )
111- if err != nil {
112- return nil , err
113- }
114- defer f .Close ()
115-
116- var words []string
117- scanner := bufio .NewScanner (f )
118- for scanner .Scan () {
119- w := scanner .Text ()
120- if w != "" {
121- words = append (words , scanner .Text ())
122- }
123- }
124- if scanner .Err () != nil {
125- return nil , scanner .Err ()
126- }
127-
128- return words , nil
129- }
0 commit comments