Skip to main content

py-pinyin-split

A Python library for splitting Hanyu Pinyin words into syllables. Built on NLTK's tokenizer interface, it handles standard syllables defined in the Pinyin Table and supports tone marks.

Based originally on pinyinsplit by @tomlee.

PyPI: https://pypi.org/project/py-pinyin-split/

Installation

Version 6 requires Python 3.10 or newer.

pip install py-pinyin-split

Python 3.8 and 3.9 users should stay on the 5.x series:

pip install "py-pinyin-split<6"

Usage

Instantiate a tokenizer and split away.

The tokenizer can handle standard Hanyu Pinyin with whitespaces and punctuation. However, invalid pinyin syllables will raise a ValueError

The tokenizer uses some basic heuristics to determine the most likely split - number of syllables, presence of vowels, and syllable frequency data.

from py_pinyin_split import PinyinTokenizer

tokenizer = PinyinTokenizer()

# Basic splitting
tokenizer.tokenize("nǐhǎo")  # ['nǐ', 'hǎo']
tokenizer.tokenize("Běijīng")  # ['Běi', 'jīng']

# Handles whitespace and punctuation
tokenizer.tokenize("Nǐ hǎo ma?")  # ['Nǐ', 'hǎo', 'ma', '?']
tokenizer.tokenize("Wǒ hěn hǎo!")  # ['Wǒ', 'hěn', 'hǎo', '!']

# Handles ambiguous splits using heuristics
tokenizer.tokenize("kěnéng") == ["kě", "néng"]
tokenizer.tokenize("rènào") == ["rè", "nào"]
tokenizer.tokenize("xīan") == ["xī", "an"]
tokenizer.tokenize("xián") == ["xián"]
tokenizer.tokenize("wǎn'ān") == ["wǎn", "'", "ān"]

# Tone marks or punctuation help resolve ambiguity
tokenizer.tokenize("xīān")  # ['xī', 'ān']
tokenizer.tokenize("xián")  # ['xián']
tokenizer.tokenize("Xī'ān")  # ["Xī", "'", "ān"]

# Raises ValueError for invalid pinyin
tokenizer.tokenize("hello")  # ValueError

# Optional support for non-standard syllables
tokenizer = PinyinTokenizer(include_nonstandard=True)
tokenizer.tokenize("duang")  # ['duang']

Development

Use a current version of uv to install the locked development environment. The default development interpreter is Python 3.14; CI covers Python 3.10 through 3.14, plus macOS and Windows on Python 3.14.

uv sync --locked
uv run --locked pytest
uv run --locked ruff check .
uv run --locked ruff format --check .
uv run --locked mypy src
uv build --no-sources

To update dependencies, run uv lock --upgrade and commit uv.lock along with any changes to pyproject.toml. Dependabot also checks Python dependencies and GitHub Actions weekly. Dependency resolution keeps a three-day delay before accepting new package releases.

Releases

Every push and pull request runs code checks and tests against the built wheel. Publishing only proceeds once these checks pass:

  • Default-branch pushes publish to TestPyPI, skipping versions already uploaded.
  • Tags matching the version in src/py_pinyin_split/__about__.py (with an optional v prefix) publish to PyPI and create a GitHub release with the distributions and Sigstore signatures.

Publishing uses the existing pypi and testpypi GitHub environments and PyPI trusted publishers configured for .github/workflows/publish.yml.

Related Projects

Metadata

Release files for py-pinyin-split 6.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-pinyin-split 6.0.0
File Size Uploaded
py_pinyin_split-6.0.0.tar.gz 69.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-pinyin-split 6.0.0
File Interpreter ABI Platform
py_pinyin_split-6.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 79.7 kB

Release files / py_pinyin_split-6.0.0.tar.gz

Download URL py_pinyin_split-6.0.0.tar.gz
Size 69.1 kB
Tags Source
SHA-256 checksum
How to use checksums
39c2f8394139c15416daac5c96ac6743a48d6733a424762041c9778833980307
BLAKE2b-256 checksum
How to use checksums
92c73cdd0b7265d2b7056440b500dfb3a2dbe65df979002c7eff6c19115128c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.

Transparency log

Release files / py_pinyin_split-6.0.0-py3-none-any.whl

Download URL py_pinyin_split-6.0.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7db54a41491e792f4b78f616e2dab43b14d9d1d6a92485f107f64769abee9b0d
BLAKE2b-256 checksum
How to use checksums
45b7ee13f83e317bcd231acedf5c134625d0ec8b4a0c8dba05853c74529fa2a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

6.0.0 This release

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.0.0

2 release files

3.0.2

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page