pypinyin-dict

raw JSON →
0.9.0 verified Mon Apr 27 auth: no python

pypinyin-dict provides custom pinyin data files from pinyin-data and phrase-pinyin-data to override or supplement the built-in pinyin data in pypinyin. It allows users to use only specific pinyin data sources. Current version 0.9.0, with stable but infrequent releases.

pip install pypinyin-dict
error ImportError: cannot import name 'load' from 'pypinyin_dict'
cause Version 0.9.0 removed the top-level load() function.
fix
Use 'from pypinyin_dict.pinyin_data import kxhc1983' and call kxhc1983.load()
error AttributeError: module 'pypinyin_dict' has no attribute 'pinyin_data'
cause Old version of pypinyin-dict, before the module restructuring (pre-0.9.0).
fix
Upgrade to the latest version: pip install --upgrade pypinyin-dict
breaking In version 0.9.0, the load() function was removed from the top-level pypinyin_dict module. You must import from specific submodules like pypinyin_dict.pinyin_data or pypinyin_dict.phrase_pinyin_data.
fix Change 'from pypinyin_dict import load' to 'from pypinyin_dict.pinyin_data import kxhc1983' and call kxhc1983.load()
breaking The module pypinyin_dict.pinyin_data has replaced the old pypinyin_dict.data. If you used pypinyin_dict.data in versions prior to 0.9.0, it no longer exists.
fix Use pypinyin_dict.pinyin_data instead of pypinyin_dict.data
gotcha Calling load() multiple times on the same data file can cause unexpected behavior. It is recommended to load only once at the start of your application.
fix Ensure that load() is called only once before any pypinyin calls.

Load a custom pinyin data file (kxhc1983) and use pypinyin normally.

import pypinyin
from pypinyin_dict.pinyin_data import kxhc1983

# Load the kxhc1983 pinyin data
kxhc1983.load()

# Now pypinyin will use the loaded data
result = pypinyin.pinyin('你好', style=pypinyin.Style.NORMAL)
print(result)  # [['ni'], ['hao']]