Documentation
¶
Overview ¶
Package fakeword generates fake words. Adding words of a certain language allows generating language-like words.
The generator is character-based and assumes ASCII input. Multibyte runes will be processed byte-wise and produce nonsensical contexts.
Index ¶
Examples ¶
Constants ¶
const (
// MaxSequencesDefault is the default value of [Generator.MaxSequences].
MaxSequencesDefault = 2
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dictionary ¶
type Dictionary struct {
PrefixLength int
// contains filtered or unexported fields
}
Dictionary stores words to be used to create a Generator.
func (*Dictionary) Add ¶
func (w *Dictionary) Add(words ...string) *Dictionary
Add adds words to the dictionary.
func (*Dictionary) Generator ¶
func (w *Dictionary) Generator() Generator
Generator returns a new Generator based on the words added to the dictionary.
func (*Dictionary) Read ¶
func (w *Dictionary) Read(in io.Reader) *Dictionary
Read reads from r and adds those words to the dictionary. Lines prefixed with # are skipped.
type Generator ¶
type Generator struct {
// Probabilities stores the probabilities of characters following on a string.
Probabilities map[string]map[string]float32
// MaxSequences defines how far back the algorithm looks
// to predict the next character. A smaller value increases randomness
// and a higher value creates words that are closer to the dictionary words.
// The default value is [MaxSequencesDefault].
MaxSequences int
// Random should return a 32-bit value as a uint32.
// If nil, [math/rand/v2.Uint32] is used.
Random func() uint32
// contains filtered or unexported fields
}
Generator generates fake words from a probability model.
func (Generator) Word ¶
Word generates a fake word with arbitrary length.
Example ¶
package main
import (
"fmt"
"thde.io/fakeword"
)
func main() {
words := []string{
"Psychotomimetic",
"Pulchritudinous",
"Consanguineous",
"Trichotillomania",
}
dict := fakeword.Dictionary{}
dict.Add(words...)
gen := dict.Generator()
fmt.Println(gen.Word())
}
Output:
Example (Concurrent) ¶
ExampleGenerator_Word_concurrent shows how to generate words from multiple goroutines. The default RNG is safe for concurrent use, so sharing a Generator across goroutines requires no extra setup.
package main
import (
"fmt"
"sync"
"thde.io/fakeword"
)
func main() {
words := []string{
"Psychotomimetic",
"Pulchritudinous",
"Consanguineous",
"Trichotillomania",
}
dict := fakeword.Dictionary{}
dict.Add(words...)
base := dict.Generator()
const workers = 4
var wg sync.WaitGroup
results := make(chan string, workers)
for range workers {
wg.Add(1)
go func() {
defer wg.Done()
results <- base.Word()
}()
}
go func() {
wg.Wait()
close(results)
}()
var generated []string
for w := range results {
generated = append(generated, w)
}
fmt.Printf("generated %d fake words\n", len(generated))
}
Output: generated 4 fake words
func (Generator) WordWithDistance ¶
WordWithDistance returns a fake word whose length is in [min, max]. It conditions termination on length: the suffix marker is suppressed while the word is shorter than min, and the loop hard-stops at max.
If a context has no non-suffix outcome before min is reached the word ends early; if no context terminates naturally before max the word is truncated.