Skip to main content

Python port of Kate Compton's tracery text generation library

Project description

https://img.shields.io/travis/aparrish/pytracery.svg

This is a (rough) port of Kate Compton’s wonderful Tracery to Python. The port is by Allison Parrish.

I’m always writing little one-off recursive template/grammar expansion functions in my Python generative text projects. And I love working with Tracery! So I figured: why not save myself (and potentially others) some time and just make it possible to use Tracery from within Python? This port is the result!

The port is a fairly literal Python translation of this version of tracery.js in the official repository’s tracery2 branch.

Installation

Install with pip like so:

pip install tracery3

You can also download the source code and install manually:

python -m pip install "."

Usage

See Kate Compton’s Tracery tutorial for information about how Tracery works. In the Python port, you use Python dictionaries instead of JavaScript objects for the rules, but the concept is the same otherwise.

import tracery
from tracery.modifiers import base_english

rules = {
    'origin': '#hello.capitalize#, #location#!',
    'hello': ['hello', 'greetings', 'howdy', 'hey'],
    'location': ['world', 'solar system', 'galaxy', 'universe']
}

grammar = tracery.Grammar(rules)
grammar.add_modifiers(base_english)
print(grammar.flatten("#origin#"))  # prints, e.g., "Hello, world!"

Any valid Tracery grammar should work in this port. The base_english modifiers in tracery.modifiers are a port of the modifiers in the JavaScript package. Many aspects of Tracery are not standardized, so in some edge cases you may get output that doesn’t exactly conform to what you would get if you used the same grammar with the JavaScript version. (e.g., “None” in strings where in JavaScript you might see “undefined”)

Command line

You can run the module on a JSON Tracery grammar file on the command line to output example lines:

positional arguments:
    json        Input JSON file
    number      Number of lines to generate (default: 1)

For example:

$ python -m tracery potterpapers.json
Harry Potter and the Tchebyshev transforms of the first and second kind

$ python -m tracery potterpapers.json 3
Harry Potter and the Impact of Extremes in Outdoor Temperature and Sunshine Exposure on Birth Weight.
Harry Potter and the Chromatin Remodelling Enzymes SNF2H and SNF2L Position Nucleosomes adjacent to CTCF and Other Transcription Factors.
Harry Potter and the Model Organism Hermissenda crassicornis (Gastropoda: Heterobranchia) Is a Species Complex.

License

This port inherits Tracery’s original Apache License 2.0.

Copyright 2016 Allison Parrish
Based on code by Kate Compton

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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

tracery3-1.0.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

tracery3-1.0.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file tracery3-1.0.1.tar.gz.

File metadata

  • Download URL: tracery3-1.0.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for tracery3-1.0.1.tar.gz
Algorithm Hash digest
SHA256 47e084c817a286e7436b0173fcb7a5405045dbbf299af4b20f7584232eecfbab
MD5 fd1b384d6b1ef5a4bd6340d128c85a0e
BLAKE2b-256 e0221d325423f64469af8e2232fa60272f05bca6ef4ddd15fc05feb7208f604f

See more details on using hashes here.

File details

Details for the file tracery3-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tracery3-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for tracery3-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e68b57a8dcfbdc57aaf778e1d3540b0d539e2b0b401ae23b02041e07e04c4b9
MD5 461720ead9d2ea83e0a502038fa175df
BLAKE2b-256 c32222ea59b07e801c9148840dd2c376a2d0dbf4177945d7c33e2fdda77727ca

See more details on using hashes here.

Supported by

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