diff --git a/dictionary.go b/dictionary.go index 4709de4..e30e8d9 100644 --- a/dictionary.go +++ b/dictionary.go @@ -2,20 +2,25 @@ package fname import ( - "bufio" - "embed" - "fmt" + _ "embed" + "strings" ) -const ( - adjectiveFilePath = "data/adjective" - adverbFilePath = "data/adverb" - nounFilePath = "data/noun" - verbFilePath = "data/verb" -) +//go:embed data/adjective +var _adjective string +var adjective = strings.Split(_adjective, "\n") + +//go:embed data/adverb +var _adverb string +var adverb = strings.Split(_adverb, "\n") -//go:embed data/* -var dataFS embed.FS +//go:embed data/noun +var _noun string +var noun = strings.Split(_noun, "\n") + +//go:embed data/verb +var _verb string +var verb = strings.Split(_verb, "\n") // Dictionary is a collection of words. type Dictionary struct { @@ -28,61 +33,12 @@ type Dictionary struct { // NewDictionary creates a new dictionary. func NewDictionary() *Dictionary { // TODO: allow for custom dictionary - a, err := loadFile(adjectiveFilePath) - if err != nil { - panic(err) - } - av, err := loadFile(adverbFilePath) - if err != nil { - panic(err) - } - n, err := loadFile(nounFilePath) - if err != nil { - panic(err) - } - v, err := loadFile(verbFilePath) - if err != nil { - panic(err) - } - return &Dictionary{ - adectives: a, - adverbs: av, - nouns: n, - verbs: v, - } -} - -// Adjective returns a random adjective. -func (d *Dictionary) Adjective(idx int) (string, error) { - if idx < 0 || idx >= len(d.adectives) { - return "", fmt.Errorf("index out of range: %d", idx) - } - return d.adectives[idx], nil -} - -// Adverb returns a random adverb. -func (d *Dictionary) Adverb(idx int) (string, error) { - if idx < 0 || idx >= len(d.adverbs) { - return "", fmt.Errorf("index out of range: %d", idx) + adectives: adjective, + adverbs: adverb, + nouns: noun, + verbs: verb, } - return d.adverbs[idx], nil -} - -// Noun returns a random noun. -func (d *Dictionary) Noun(idx int) (string, error) { - if idx < 0 || idx >= len(d.nouns) { - return "", fmt.Errorf("index out of range: %d", idx) - } - return d.nouns[idx], nil -} - -// Verb returns a random verb. -func (d *Dictionary) Verb(idx int) (string, error) { - if idx < 0 || idx >= len(d.verbs) { - return "", fmt.Errorf("index out of range: %d", idx) - } - return d.verbs[idx], nil } // LengthAdjective returns the number of adjectives in the dictionary. @@ -104,26 +60,3 @@ func (d *Dictionary) LengthNoun() int { func (d *Dictionary) LengthVerb() int { return len(d.verbs) } - -// loadFile loads a file from the embedded filesystem, and returns a slice of strings containing each line. -func loadFile(path string) ([]string, error) { - f, err := dataFS.Open(path) - if err != nil { - return nil, err - } - defer f.Close() - - var words []string - scanner := bufio.NewScanner(f) - for scanner.Scan() { - w := scanner.Text() - if w != "" { - words = append(words, scanner.Text()) - } - } - if scanner.Err() != nil { - return nil, scanner.Err() - } - - return words, nil -} diff --git a/dictionary_test.go b/dictionary_test.go index 95f970f..f540957 100644 --- a/dictionary_test.go +++ b/dictionary_test.go @@ -32,72 +32,6 @@ func TestNewDictionary(t *testing.T) { t.Error("\t\tShould be able to load the verb file.") } - adj, err := d.Adjective(0) - if err != nil { - t.Error("\t\tShould be able to get an adjective from the dictionary.") - } - t.Log("\t\tShould be able to get an adjective from the dictionary.") - if adj != "able" { - t.Error("\t\tShould be able to get the first adjective from the dictionary.") - } - t.Log("\t\tShould be able to get the first adjective from the dictionary.") - - _, err = d.Adjective(-1) - if err == nil { - t.Error("\t\tShould not be able to get an adjective from the dictionary with a negative index.") - } - t.Log("\t\tShould not be able to get an adjective from the dictionary with a negative index.") - - adv, err := d.Adverb(0) - if err != nil { - t.Error("\t\tShould be able to get an adverb from the dictionary.") - } - t.Log("\t\tShould be able to get an adverb from the dictionary.") - if adv != "abnormally" { - t.Error("\t\tShould be able to get the first adverb from the dictionary.") - } - t.Log("\t\tShould be able to get the first adverb from the dictionary.") - - _, err = d.Adverb(-1) - if err == nil { - t.Error("\t\tShould not be able to get an adverb from the dictionary with a negative index.") - } - t.Log("\t\tShould not be able to get an adverb from the dictionary with a negative index.") - - noun, err := d.Noun(0) - if err != nil { - t.Error("\t\tShould be able to get a noun from the dictionary.") - } - t.Log("\t\tShould be able to get a noun from the dictionary.") - - if noun != "aardvark" { - t.Error("\t\tShould be able to get the first noun from the dictionary.") - } - t.Log("\t\tShould be able to get the first noun from the dictionary.") - - _, err = d.Noun(-1) - if err == nil { - t.Error("\t\tShould not be able to get a noun from the dictionary with a negative index.") - } - t.Log("\t\tShould not be able to get a noun from the dictionary with a negative index.") - - verb, err := d.Verb(0) - if err != nil { - t.Error("\t\tShould be able to get a verb from the dictionary.") - } - t.Log("\t\tShould be able to get a verb from the dictionary.") - - if verb != "abandoned" { - t.Error("\t\tShould be able to get the first verb from the dictionary.") - } - t.Log("\t\tShould be able to get the first verb from the dictionary.") - - _, err = d.Verb(-1) - if err == nil { - t.Error("\t\tShould not be able to get a verb from the dictionary with a negative index.") - } - t.Log("\t\tShould not be able to get a verb from the dictionary with a negative index.") - if len(d.adectives) != d.LengthAdjective() { t.Error("\t\tShould be able to get the length of the adjective file.") } diff --git a/generator.go b/generator.go index 34b06a6..1c4fe8a 100644 --- a/generator.go +++ b/generator.go @@ -12,7 +12,7 @@ import ( type Generator struct { dict *Dictionary delimiter string - seed int64 + rand *rand.Rand size uint } @@ -29,7 +29,7 @@ func WithDelimiter(delimiter string) GeneratorOption { // WithSeed sets the seed used to generate random numbers. func WithSeed(seed int64) GeneratorOption { return func(r *Generator) { - r.seed = seed + r.rand.Seed(seed) } } @@ -45,48 +45,32 @@ func NewGenerator(opts ...GeneratorOption) *Generator { r := &Generator{ dict: NewDictionary(), delimiter: "-", - seed: time.Now().UnixNano(), + rand: rand.New(rand.NewSource(time.Now().UnixNano())), size: 2, } for _, opt := range opts { opt(r) } - rand.Seed(r.seed) return r } // Generate generates a random name. func (r *Generator) Generate() (string, error) { // TODO: address case where adjective and noun are the same, such as "orange-orange" or "sound-sound" - adjective, err := r.dict.Adjective(rand.Intn(r.dict.LengthAdjective())) - if err != nil { - return "", err - } - noun, err := r.dict.Noun(rand.Intn(r.dict.LengthNoun())) - if err != nil { - return "", err - } + adjective := r.dict.adectives[r.rand.Intn(r.dict.LengthAdjective())] + noun := r.dict.nouns[r.rand.Intn(r.dict.LengthNoun())] words := []string{adjective, noun} switch r.size { case 2: return strings.Join(words, r.delimiter), nil case 3: - verb, err := r.dict.Verb(rand.Intn(r.dict.LengthVerb())) - if err != nil { - return "", err - } + verb := r.dict.verbs[r.rand.Intn(r.dict.LengthVerb())] words = append(words, verb) case 4: - verb, err := r.dict.Verb(rand.Intn(r.dict.LengthVerb())) - if err != nil { - return "", err - } + verb := r.dict.verbs[r.rand.Intn(r.dict.LengthVerb())] words = append(words, verb) - adverb, err := r.dict.Adverb(rand.Intn(r.dict.LengthAdverb())) - if err != nil { - return "", err - } + adverb := r.dict.adverbs[r.rand.Intn(r.dict.LengthAdverb())] words = append(words, adverb) default: return "", fmt.Errorf("invalid size: %d", r.size) diff --git a/generator_test.go b/generator_test.go index 4734eb4..63a6cd8 100644 --- a/generator_test.go +++ b/generator_test.go @@ -39,11 +39,6 @@ func TestNewGenerator(t *testing.T) { t.Fatal("\t\tShould be able to set the delimiter of the phrase.") } t.Log("\t\tShould be able to set the delimiter of the phrase.") - - if g.seed != 12345 { - t.Fatal("\t\tShould be able to set the seed of the phrase.") - } - t.Log("\t\tShould be able to set the seed of the phrase.") } } }