Skip to main content

Composable, deterministic-friendly text-generation components for Python.

Project description

The Wordsmith Engine

Wordsmith Engine banner

Build Status Supports Python versions 3.12+

The Wordsmith Engine is a composable, deterministic-friendly text-generation toolkit for Python. It started as a port of the Swift package Wordsmith (by the same author) and focuses on building rich, varied text with small, reusable components.

What it is for

  • Generating names for people, towns, ships, gangs, literary works, movies, albums, and fictional materials.
  • Building simple language generators from reusable parts.
  • Producing repeatable output with a seeded RNG.

Installation

Install the package from PyPI:

pip install wordsmith-engine

Use PDM to install dependencies for development:

pdm install --group dev

Quickstart

from wordsmith import LiteraryTitle

print(LiteraryTitle()())

Core concepts

The Wordsmith Engine is centered around the Component type. Each component can render text with a provided random number generator.

Operator DSL (preferred):

  • left | right: Join components with a space.
  • left + right: Join components with no separator (useful for punctuation).

Key combinators:

  • text(*parts, sep=""): Join components with a custom separator.
  • one_of(*options): Pick a random option.
  • weighted_one_of((weight, option), ...): Weighted choice across many options.
  • either(a, b, first_probability=0.5): Weighted choice between two options.
  • maybe(*parts, probability=0.5): Optionally include text.

Common decorators on components:

  • .title_case() for title casing (with small-word rules).
  • .capitalized() to capitalize all words.
  • .first_upper() for first-letter capitalization.
  • .prefixed_by_article() / .prefixed_by_determiner() for grammar helpers.
  • .possessive_form() for possessives.

Usage examples

import random

from wordsmith import Adjective, AlbumTitle, LiteraryTitle, Noun, one_of, maybe

rng = random.Random(42)

title = (
    "The"
    | maybe(Adjective())
    | one_of("Journey", "Voyage", "Chronicles")
    | "of"
    | Noun().prefixed_by_article()
).title_case()

line = ("Once" | maybe("upon a time", probability=0.5)) + "."

print(title(rng))
print(line(rng))
print(LiteraryTitle()(rng))
print(AlbumTitle()(rng))

Generators included

The Wordsmith Engine ships with a growing set of generators:

  • Names: GivenName, Surname, PersonName, AlienName, AncientGivenName, FantasyName
    • GivenName supports gender and cultural grouping with BinaryGender and GivenNameCulture.
  • Locations: TownName
  • Groups: CriminalGangName, BandName
  • Vessels: NauticalShipName
  • Literary titles: SimpleLiteraryTitle, UnusualLiteraryTitle, LiteraryTitle
  • Movie titles: SimpleMovieTitle, HighConceptMovieTitle, MovieTitle
  • Album titles: AlbumTitle
  • Materials: FictionalElementName, FictionalMineralName, ChemicalCompoundName
    • ChemicalCompoundName samples real compound names; the element and mineral generators produce fictional names.
  • Specials: ReadableUniqueIdentifier, ExoticCharacter

Deterministic output

All components accept a random.Random instance. If you want repeatable results, pass a seeded RNG.

import random
from wordsmith import LiteraryTitle

rng = random.Random(1234)
print(LiteraryTitle()(rng))
print(LiteraryTitle()(rng))

Examples

Run any script under examples/ with PDM, for example:

pdm run python examples/character_names.py
pdm run python examples/fictional_materials.py
pdm run python examples/literary_titles.py
pdm run python examples/movie_titles.py
pdm run python examples/album_titles.py

Project docs

Repo-local architecture, asset, and quality notes live in docs/.

Development

  • Install: pdm install --group dev
  • Run tests: pdm run pytest
  • Run lint: pdm run lint
  • Build: pdm build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wordsmith_engine-0.3.0.tar.gz (145.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wordsmith_engine-0.3.0-py3-none-any.whl (154.5 kB view details)

Uploaded Python 3

File details

Details for the file wordsmith_engine-0.3.0.tar.gz.

File metadata

  • Download URL: wordsmith_engine-0.3.0.tar.gz
  • Upload date:
  • Size: 145.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wordsmith_engine-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1b0bac5e37058fafa492151683e78064d3a514c4d061bb74f74d76ee35a2e828
MD5 f5a031efedcd85cd1fbc094feb333b99
BLAKE2b-256 59e01fc4d7bd863a02875e1aec36278131d57972e6331150cea46da17b0f611b

See more details on using hashes here.

Provenance

The following attestation bundles were made for wordsmith_engine-0.3.0.tar.gz:

Publisher: python-publish.yml on btfranklin/wordsmith-engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wordsmith_engine-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wordsmith_engine-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3245a1b45ea89bfec2ad5706330ec8f233989320a122e257c731226eccab6b77
MD5 734bd37bad9f543ec97d4cd5f5c85a44
BLAKE2b-256 788def06c87f74f3d6354ac4d7c2ab004e3b276c68962c93aeff39ecc81b2433

See more details on using hashes here.

Provenance

The following attestation bundles were made for wordsmith_engine-0.3.0-py3-none-any.whl:

Publisher: python-publish.yml on btfranklin/wordsmith-engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page