Skip to content

added support for multiple dictionaries #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
**petname** \[-w|--words INT\] \[-l|--letters INT\] \[-s|--separator STR\] \[-d|--dir STR\] \[-c|--complexity INT\] \[-u|--ubuntu\]

## OPTIONS []()

- -w|--words number of words in the name, default is 2
- -l|--letters maximum number of letters in each word, default is unlimited
- -s|--separator string used to separate name words, default is ’-’
- -d|--dir directory containing adverbs.txt, adjectives.txt, names.txt, default is */usr/share/petname/*
- -d|--dir directory containing adverbs.txt, adjectives.txt, names.txt, default is _/usr/share/petname/_
- -c|--complexity \[0, 1, 2\]; 0 = easy words, 1 = standard words, 2 = complex words, default=1
- -u|--ubuntu generate ubuntu-style names, alliteration of first character of each word

Expand Down Expand Up @@ -59,13 +60,15 @@ massive-colt
Besides this shell utility, there are also native libraries: python-petname, python3-petname, and golang-petname. Here are some programmatic examples in code:

**Golang Example**

```golang
package main

import (
"flag"
"fmt"
"github.com/dustinkirkland/golang-petname"
"github.com/dustinkirkland/golang-petname/dict/small"
)

var (
Expand All @@ -75,7 +78,7 @@ var (

func main() {
flag.Parse()
fmt.Println(petname.Generate(\*words, \*separator))
fmt.Println(petname.Generate(small.Dict, \*words, \*separator))
}
```

Expand Down Expand Up @@ -104,6 +107,6 @@ print petname.Generate(int(parser.options.words), parser.options.separator)

This manpage and the utility were written by Dustin Kirkland <[email protected]> for Ubuntu systems (but may be used by others). Permission is granted to copy, distribute and/or modify this document and the utility under the terms of the Apache2 License.

The complete text of the Apache2 License can be found in */usr/share/common-licenses/Apache-2.0* on Debian/Ubuntu systems.
The complete text of the Apache2 License can be found in _/usr/share/common-licenses/Apache-2.0_ on Debian/Ubuntu systems.

------------------------------------------------------------------------
---
63 changes: 63 additions & 0 deletions dict/dict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dict

import (
"math/rand"
"strings"
)

// DIct implements petname.Dict by providing random adverbs, adjectives and names
// which are combined to compose a pet name.
type Dict struct {
Adverbs WordSource
Adjectives WordSource
Names WordSource
}

// Adverb returns a random adverb from the dictionary.
func (d *Dict) Adverb() string {
return d.Adverbs.Rand()
}

// Adjective returns a random adjective from the dictionary.
func (d *Dict) Adjective() string {
return d.Adjectives.Rand()
}

// Name returns a random name from the dictionary.
func (d *Dict) Name() string {
return d.Names.Rand()
}

// WordSource represents a collection of line-separated words with a count.
//
// Why not a slice? Well, petname has large dictionaries, some with over 50,000 words.
// Storing 50,000 strings in a slice would allocate 50,000 pointers to those strings,
// which adds up to 400 KB of wasted memory on 64-bit architectures. Additionally,
// allocating this many pointers may slow down the GC as it looks for unused pointers
// to dispose of.
type WordSource struct {
Words string
Count int
}

// Word returns the word at the given index from the collection,
// or an empty string if index is out of bounds.
func (s WordSource) Word(index int) string {
pos := 0
for i, r := range s.Words {
if pos == index {
// Return current word beginning at `i` until the next line ending at `end`.
end := i + strings.IndexRune(s.Words[i:], '\n')
return s.Words[i:end]
}
if r == '\n' {
pos++
}
}
return ""
}

// Rand returns a random word from the collection.
func (s WordSource) Rand() string {
return s.Word(rand.Intn(s.Count))
}
Loading