"A small Python package for splitting text into dialogue and narrative."
Project description
narrative
A small Python package for splitting text into dialogue and narrative.
Installation
narrative
is available on PyPI. Simply install it with pip
:
pip install narrative
You can also install it from source:
$ git clone https://github.com/prosegrinder/python-narrative.git
Cloning into 'python-narrative'...
...
$ cd python-narrative
$ python setup.py install
...
Usage
narrative
splits a piece of prose into narrative and dialogue components. The
main function split()
will return a dict containing both narrative
and
dialogue
components:
>>> import narrative
>>> text = '"Hello," he said. "How are you today?"'
>>> narrative.split(text)
{'dialogue': ['"Hello,"', '"How are you today?"'], 'narrative': ['', ' he said. ', '']}
There are two other helper functions as well.
get_dialogue()
returns only the dialogue components:
>>> narrative.get_dialogue(text)
['"Hello,"', '"How are you today?"']
get_narrative()
returns only the narrative components:
>>> narrative.get_narrative(text)
['', ' he said. ', '']
Note: The empty strings are a feature of Python's split()
function. See
Why are empty strings returned in split() results?
for an explanation.
British Style
Each function accepts a second parameter of a regular expression used to parse
out the dialogue. This defaults to narrative.DIALOGUE_RE
, which follows the
American standard of using double quotes for initial quotes. narrative
now
includes a second regular expression, narrative.BRITISH_DIALOGUE_RE
, which
follows the British style of using single quotes for initial quotes. Simply use
it as the second parameter for any function:
>>> import narrative
>>> narrative.split(text, narrative.BRITISH_DIALOGUE_RE)
>>> …
>>> narrative.get_dialogue(text, narrative.BRITISH_DIALOGUE_RE)
>>> …
>>> narrative.get_narrative(text, narrative.BRITISH_DIALOGUE_RE)
>>> …
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
Built Distribution
Hashes for narrative-1.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 906652b309b33b82a21d1bc1c13ad1fd1a74a222e278376c9e2cd98e118dd3bc |
|
MD5 | 12ad0839d619535bcee5e1b88e655bf1 |
|
BLAKE2b-256 | ab75b7dc18e75920642cb3d5ad775d1eab4a3ea5648f7a1f9270cc559300b09a |