Skip to content

Commit 57d9730

Browse files
committed
refactor: simplify dictionary data embedding
Directly access embedded dictionary data. Remove unused file utility function.
1 parent b559a85 commit 57d9730

1 file changed

Lines changed: 20 additions & 54 deletions

File tree

dictionary.go

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
package fname
33

44
import (
5-
"bufio"
6-
"embed"
5+
_ "embed"
6+
"strings"
77
)
88

9-
const (
10-
adjectiveFilePath = "data/adjective"
11-
adverbFilePath = "data/adverb"
12-
nounFilePath = "data/noun"
13-
verbFilePath = "data/verb"
14-
)
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")
1516

16-
//go:embed data/*
17-
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")
1824

1925
// Dictionary is a collection of words.
2026
type Dictionary struct {
@@ -27,28 +33,11 @@ type Dictionary struct {
2733
// NewDictionary creates a new dictionary.
2834
func NewDictionary() *Dictionary {
2935
// TODO: allow for custom dictionary
30-
a, err := loadFile(adjectiveFilePath)
31-
if err != nil {
32-
panic(err)
33-
}
34-
av, err := loadFile(adverbFilePath)
35-
if err != nil {
36-
panic(err)
37-
}
38-
n, err := loadFile(nounFilePath)
39-
if err != nil {
40-
panic(err)
41-
}
42-
v, err := loadFile(verbFilePath)
43-
if err != nil {
44-
panic(err)
45-
}
46-
4736
return &Dictionary{
48-
adectives: a,
49-
adverbs: av,
50-
nouns: n,
51-
verbs: v,
37+
adectives: adjective,
38+
adverbs: adverb,
39+
nouns: noun,
40+
verbs: verb,
5241
}
5342
}
5443

@@ -71,26 +60,3 @@ func (d *Dictionary) LengthNoun() int {
7160
func (d *Dictionary) LengthVerb() int {
7261
return len(d.verbs)
7362
}
74-
75-
// loadFile loads a file from the embedded filesystem, and returns a slice of strings containing each line.
76-
func loadFile(path string) ([]string, error) {
77-
f, err := dataFS.Open(path)
78-
if err != nil {
79-
return nil, err
80-
}
81-
defer f.Close()
82-
83-
var words []string
84-
scanner := bufio.NewScanner(f)
85-
for scanner.Scan() {
86-
w := scanner.Text()
87-
if w != "" {
88-
words = append(words, scanner.Text())
89-
}
90-
}
91-
if scanner.Err() != nil {
92-
return nil, scanner.Err()
93-
}
94-
95-
return words, nil
96-
}

0 commit comments

Comments
 (0)