Skip to main content

Parse dramatic play text into ordered dramatic events.

Project description

play-parser

play-parser parses theatrical play text into a canonical JSON document and assembles canonical documents back into normalised play text.

Canonical text uses a stable output format. For example, speech labels are emitted in colon form such as Hamlet: ..., even when the source text used another supported layout.

Features

  • Parse raw .txt play files into structured JSON.
  • Assemble canonical JSON documents into normalised .txt output.
  • Read and validate existing canonical .json documents.
  • Work with explicit parsing profiles for different source formats.
  • Preserve speeches, stage directions, acts, scenes, metadata, characters, and document statistics.
  • Use the package from Python or through the play-parser command line interface.

Supported inputs

  • Raw .txt play files.
  • Canonical .json documents produced by this package.

The package does not parse PDFs, DOCX files, HTML pages, scans, images, or audio directly. Convert those sources to text first.

Installation

pip install play-parser

Python quick start

from play_parser import Play, PlayIngestor

ingestor = PlayIngestor("Hamlet.txt", profile="colon_inline")
play = Play(ingestor.data)

print(play.title)
print(play.author)
print(len(play.acts))
print(len(play.scenes))
print(len(play.characters))
print(len(play.speeches))

play.save_json("Hamlet.json")
play.save_text("Hamlet.canonical.txt")

Parse text that is already in memory:

from play_parser import PlayIngestor

text = "ACT I\n\nSCENE I.\n\nHAMLET: Who's there?"
ingestor = PlayIngestor.from_text(text, source_name="Hamlet.txt", profile="colon_inline")
document = ingestor.data

Assemble a canonical document:

from play_parser import assemble_play_text

canonical_text = assemble_play_text(document)

Command line usage

Show help and version information:

play-parser --help
play-parser --version

Parse one file:

play-parser parse Hamlet.txt \
  --profile colon_inline \
  --json-output Hamlet.json \
  --text-output Hamlet.canonical.txt

Parse a folder recursively:

play-parser parse \
  --input-root data/text/raw \
  --recursive \
  --profile colon_inline \
  --json-output-root data/json/generated

Assemble canonical JSON files into text:

play-parser assemble \
  --input-root data/json/generated \
  --recursive \
  --output-root data/text/canonical

Public API

Stable top-level imports:

from play_parser import (
    Play,
    PlayIngestor,
    assemble_play_text,
    get_format_profile,
    list_format_profiles,
    load_format_profile_config,
    load_format_profile_file,
    validate_play_document,
)

Domain classes such as Act, Scene, Speech, Character, Monologue, and Dialogue are also available from the top-level package.

Format profiles

Built-in profiles are available through list_format_profiles() and can be passed to PlayIngestor or the CLI by name.

from play_parser import list_format_profiles

print(list_format_profiles())

See docs/FORMAT_PROFILES.md for the profile schema and examples.

Documentation

Development

Install development dependencies:

python -m pip install -e .[dev]

Run local checks:

python -m ruff check .
python -m ruff format --check .
python -m unittest discover -s tests
python -m build
python -m twine check dist/*

Release steps are documented in RELEASE.md.

Licence

MIT

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

play_parser-1.0.0.tar.gz (4.3 MB view details)

Uploaded Source

Built Distribution

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

play_parser-1.0.0-py3-none-any.whl (41.8 kB view details)

Uploaded Python 3

File details

Details for the file play_parser-1.0.0.tar.gz.

File metadata

  • Download URL: play_parser-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for play_parser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8b799cf5ba56cfe254d108c548a21455d3b3abd03796d19d919f10a13f1d714b
MD5 99d3b1c527b3f9e5c4c8a25b31884efd
BLAKE2b-256 421edef90e954da5a4082dd243a7fa33c671cab62003bc5a096774eeaa464fb0

See more details on using hashes here.

File details

Details for the file play_parser-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: play_parser-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 41.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for play_parser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0d097285da495485e78171807829a8552f5c8bb7de2cbebd27f339f7e863bf8
MD5 6db9eaf9f4483dc4f7c284de04641bc3
BLAKE2b-256 23c2a054b448cef57efa6d6b0ed3faf3f2e6dd1e77136e67f20e58b465aa6eec

See more details on using hashes here.

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