pypinyin Documentation

repository·master·Indexed 26 days ago

https://github.com/mozillazg/python-pinyin

A Python tool for converting Chinese characters into pinyin, zhuyin (bopomofo), and Cyrillic. It supports heteronyms, multiple tone styles, phrase-based disambiguation, and URL-friendly slug generation. The library includes core API functions like pinyin() and lazy_pinyin(), a CLI tool, and the pypinyin.contrib.tone_convert module for transforming Pinyin strings between different formats.

Tokens
8.1K
Snippets
17
Records
57
Agent score
86%

What's inside pypinyin

  1. Overview of pypinyin features

    master

    pypinyin is a tool for converting Chinese characters to pinyin. It is suitable for character annotation, sorting, and retrieval.

    Key features include:

    • Intelligent pinyin matching based on words/phrases.
    • Support for polyphones (characters with multiple pronunciations).
    • Simple support for Traditional Chinese and Zhuyin (Bopomofo).
    • Support for various pinyin styles.
  2. Release a new version

    master

    The release process involves rebasing, generating data, testing, updating the changelog, and publishing. Use the following make commands for specific steps:

    1. Prepare: Rebase master branch using make rebase_master.
    2. Data Generation: Generate latest data files using make gen_data.
    3. Testing: Run tests using make test.
    4. Merging: Merge the develop branch into master using make merge_dev.
    5. Versioning:
      • For minor changes (e.g., 1.1.x -> 1.2.x): make bump_minor
      • For patch changes (e.g., 1.1.1 -> 1.1.2): make bump_patch
    6. Publishing:
      • To Test PyPI: make publish_test
      • To PyPI: make publish
    7. Cleanup: Prepare for the next development cycle using make start_next.
  3. Run unit tests

    master

    You can run unit tests using make test for the current Python version, or use tox to run tests across multiple Python versions (this is also typically run via CI during Pull Requests).

    To run tests in the current environment:

    (venv) $ make test

    To run tests across multiple versions using tox:

    (venv) $ tox
  4. Improve pinyin accuracy with custom dictionaries

    master

    If the pinyin results are incorrect, you can improve accuracy by loading custom phrase or single-character dictionaries.

    Customizing via code:

    from pypinyin import load_phrases_dict, load_single_dict
    
    # Add a custom phrase
    load_phrases_dict({'桔子': [['jú'], ['zǐ']]})
    
    # Adjust or override single character pinyin
    load_single_dict({ord('还'): 'hái,huán'})

    Using pypinyin-dict:

    You can also use the pypinyin-dict project to load optimized data from cc_cedict.txt or kXHC1983.txt.

    from pypinyin_dict.phrase_pinyin_data import cc_cedict
    cc_cedict.load()
    
    from pypinyin_dict.pinyin_data import kxhc1983
    kxhc1983.load()
    from pypinyin import load_phrases_dict, load_single_dict
    
    load_phrases_dict({'桔子': [['jú'], ['zǐ']]})
    load_single_dict({ord('还'): 'hái,huán'})
  5. Basic Usage of pinyin and lazy_pinyin

    master

    Use pinyin() to get a list of lists containing pinyin for each character, or lazy_pinyin() to get a flat list of pinyin strings.

    Key features:

    • Heteronyms: Set heteronym=True to include multiple possible pronunciations for polyphonic characters.
    • Styles: Use the style parameter with pypinyin.Style constants (e.g., Style.TONE3, Style.FIRST_LETTER, Style.BOPOMOFO).
    • Ü handling: By default, ü is represented as v. Use v_to_u=True to use ü instead.
    • Neutral tones: Use neutral_tone_with_five=True to represent neutral tones with the number 5 (e.g., Style.TONE3).
    • Tone Sandhi: Set tone_sandhi=True to apply tone sandhi rules (e.g., converting nǐ hǎo to ní hǎo).
  6. Set up the development environment

    master

    To develop for pypinyin, use Python 3.6+ and follow these steps to create a virtual environment, install development dependencies, and install the package in editable mode:

    $ virtualenv venv
    $ . venv/bin/activate
    (venv) $ pip install -U -r requirements_dev.txt
    (venv) $ pip install -e .
    (venv) $ pre-commit install