fakeword

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 5 Imported by: 0

README

fakeword

Go Reference test Go Report Card

Go package fakeword allows to generate fake words.

Example

Adding some English words, will generate fake words that sound english.

Try the example online on pkg.go.dev.

package main

import "thde.io/fakeword"

func main() {
    words := []string{
        "Psychotomimetic",
        "Pulchritudinous",
        "Consanguineous",
        "Trichotillomania",
    }

    dict := fakeword.Dictionary{}
    dict.Add(words...)

    gen := dict.Generator()
    fmt.Println(gen.Word())
}

The library was inspired by nwtgck/go-fakelish.

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

View Source
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

func (g Generator) Word() string

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())
}
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

func (g Generator) WordWithDistance(minLen, maxLen int) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL