Skip to main content

Adds pinyin to lists of chinese strings (utf-8 only)

Project description

Pinyiniser

PyPI version License: MIT Python 3.9+

A Python library that converts Chinese strings (UTF-8) to pinyin using rjieba for word segmentation and CC-CEDICT for pinyin lookup.

Installation

pip install pinyiniser

Quick Example

import pinyiniser as pyer

# Load the dictionary (True = numeral tones, False = diacritic tones)
zh_dict = pyer.get_dictionary(True)

# Get pinyin as a list
pinyin = pyer.get_pinyin('你好,世界!', zh_dict)
print(pinyin)
# ['ni3hao3', ',', 'shi4jie4', '!']

# Get word segments and pinyin together
segments, pinyin = pyer.get_segments_and_pinyin('你好,世界!', zh_dict)
print(segments)
# ['你好', ',', '世界', '!']
print(pinyin)
# ['ni3hao3', ',', 'shi4jie4', '!']

API

pyer.get_dictionary(numeric=True)

Loads the CC-CEDICT pinyin dictionary.

zh_dict = pyer.get_dictionary(True)

True for numerals e.g. shuo1
False for diacritics e.g. shuō

Numerals are useful for language learning as they introduce friction to reading solely pinyin, while diacritics are more natural to read and more professional. Choose whichever suits your application.

zh_dict details

zh_dict is a dictionary of dictionaries, where the first key is the character, and the second key is 'pinyin' e.g. zh_dict[zh_char]['pinyin']

Any dictionary that has this structure will work, allowing you flexibility in what you use — for example, you could add English definitions: zh_dict[zh_char]['english']

pyer.get_pinyin(zh_string, zh_dict, punctuation=special_tokens)

Returns pinyin as a flat list. Punctuation is preserved in place.

pinyin = pyer.get_pinyin('你好,世界!', zh_dict)
# ['ni3hao3', ',', 'shi4jie4', '!']

pyer.get_segments_and_pinyin(zh_string, zh_dict, punctuation=special_tokens)

Returns a tuple of (segments, pinyin), both list[str]:

  • segments — the word-level tokens as segmented by rjieba, with punctuation preserved as individual elements.
  • pinyin — a list of pinyin strings, one per segment.
segments, pinyin = pyer.get_segments_and_pinyin('你好,世界!', zh_dict)
print(segments)
# ['你好', ',', '世界', '!']
print(pinyin)
# ['ni3hao3', ',', 'shi4jie4', '!']

punctuation / special_tokens

Both get_pinyin and get_segments_and_pinyin accept a punctuation parameter. This is a set of characters that are split on and passed through as-is rather than being looked up in the dictionary.

The default set (pyer.special_tokens) includes Chinese punctuation, standard ASCII punctuation, maths operators, and currency symbols.

To extend the default set, union your own set with the built-in one:

my_punctuation = pyer.special_tokens | {'†', '‡'}
pinyin = pyer.get_pinyin('你好', zh_dict, punctuation=my_punctuation)

Performance

Benchmarked against v1.0.3 (PyPI) on the Shadowrun Returns Chinese corpus (~100k words):

Default punctuation (precompiled regex):

v2.0.0 v1.0.3 Speedup
Avg over 10 runs 0.071s 0.305s 4.3x faster
Per entry 0.011ms 0.046ms 4.2x faster

Custom punctuation (regex built per call):

v2.0.0 v1.0.3 Speedup
Avg over 10 runs 0.128s 0.305s 2.4x faster
Per entry 0.019ms 0.046ms 2.4x faster

Dependencies

  • rjieba (>= 0.2.0) — Chinese text segmentation (Rust implementation of Jieba)

Attribution

This project uses dictionary data derived from CC-CEDICT, licensed under the Creative Commons Attribution-ShareAlike 4.0 International 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

pinyiniser-2.0.3.tar.gz (11.0 MB view details)

Uploaded Source

Built Distribution

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

pinyiniser-2.0.3-py3-none-any.whl (11.0 MB view details)

Uploaded Python 3

File details

Details for the file pinyiniser-2.0.3.tar.gz.

File metadata

  • Download URL: pinyiniser-2.0.3.tar.gz
  • Upload date:
  • Size: 11.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pinyiniser-2.0.3.tar.gz
Algorithm Hash digest
SHA256 20748ff5253ff0dc9900927256ffdfa961f27f7b8602a1f7af539494de0d5c5e
MD5 23db469ca8f342ad14d2289193dc7b52
BLAKE2b-256 cc89aa7ea3b3eab33ebc59422d2416e5abfdd257ce8e8d9cb27d29d6dee0775c

See more details on using hashes here.

File details

Details for the file pinyiniser-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: pinyiniser-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pinyiniser-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8a478d41de83d8f0c671a1005dfd5e6d5e9b98b6e59ffd0c48b3b4f1f72b0eb1
MD5 0f71135185d6476bb1e35bf8de5b2752
BLAKE2b-256 fd09684632de95cd6672cda1ced9c064c7f3e6d4b88f0d43f05ec5dad0b7893d

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