VOOZH about

URL: https://pypi.org/project/pypinyin/

⇱ pypinyin · PyPI


Skip to main content

pypinyin 0.55.0

pip install pypinyin

Latest release

Released:

汉字拼音转换模块/工具.

Navigation

Verified details

These details have been verified by PyPI
Maintainers
👁 Avatar for mozillazg from gravatar.com
mozillazg

Project description

👁 Build
👁 GitHubAction
👁 Coverage
👁 PyPI version
👁 PyPI downloads
👁 DOI

将汉字转为拼音。可以用于汉字注音、排序、检索(Russian translation) 。

最初版本的代码参考了 hotoo/pinyin 的实现。

特性

  • 根据词组智能匹配最正确的拼音。

  • 支持多音字。

  • 简单的繁体支持,注音支持,威妥玛拼音支持。

  • 支持多种不同拼音/注音风格。

安装

pipinstallpypinyin

使用示例

>>> frompypinyinimport pinyin, lazy_pinyin, Style>>> pinyin('中心') # or pinyin(['中心']),参数值为列表时表示输入的是已分词后的数据[['zhōng'], ['xīn']]>>> pinyin('中心', heteronym=True) # 启用多音字模式[['zhōng', 'zhòng'], ['xīn']]>>> pinyin('中心', style=Style.FIRST_LETTER) # 设置拼音风格[['z'], ['x']]>>> pinyin('中心', style=Style.TONE2, heteronym=True)[['zho1ng', 'zho4ng'], ['xi1n']]>>> pinyin('中心', style=Style.TONE3, heteronym=True)[['zhong1', 'zhong4'], ['xin1']]>>> pinyin('中心', style=Style.BOPOMOFO) # 注音风格[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]>>> lazy_pinyin('威妥玛拼音', style=Style.WADEGILES)['wei', "t'o", 'ma', "p'in", 'yin']>>> lazy_pinyin('中心') # 不考虑多音字的情况['zhong', 'xin']>>> lazy_pinyin('战略', v_to_u=True) # 不使用 v 表示 ü['zhan', 'lüe']# 使用 5 标识轻声>>> lazy_pinyin('衣裳', style=Style.TONE3, neutral_tone_with_five=True)['yi1', 'shang5']# 变调 nǐ hǎo -> ní hǎo>>> lazy_pinyin('你好', style=Style.TONE2, tone_sandhi=True)['ni2', 'ha3o']

注意事项

  • 默认情况下拼音结果不会标明哪个韵母是轻声,轻声的韵母没有声调或数字标识(可以通过参数 neutral_tone_with_five=True 开启使用 5 标识轻声 )。

  • 默认情况下无声调相关拼音风格下的结果会使用 v 表示 ü (可以通过参数 v_to_u=True 开启使用 ü 代替 v )。

  • 默认情况下会原样输出没有拼音的字符(自定义处理没有拼音的字符的方法见 文档 )。

  • 的拼音并不是大部分人以为的 en 以及存在既没有声母也没有韵母的拼音,详见下方 FAQ 中的说明。

命令行工具:

$ pypinyin音乐yīn yuè

$ python-mpypinyin.tools.toneconvertto-tone'zhong4 xin1'zhòng xīn

文档

详细文档请访问:https://pypinyin.readthedocs.io/

项目代码开发方面的问题可以看看 开发文档

FAQ

拼音有误?

可以通过下面的方法提高拼音准确性:

  • 可以通过自定义词组拼音库或者单字拼音库的方式修正拼音结果, 详见 文档

>> frompypinyinimport load_phrases_dict, load_single_dict>> load_phrases_dict({'桔子': [['jú'], ['zǐ']]}) # 增加 "桔子" 词组>> load_single_dict({ord('还'): 'hái,huán'}) # 调整 "还" 字的拼音顺序或覆盖默认拼音
  • 也可以使用 pypinyin-dict 项目提供的自定义拼音库来纠正结果。

# 使用 phrase-pinyin-data 项目中 cc_cedict.txt 文件中的拼音数据优化结果>>> frompypinyin_dict.phrase_pinyin_dataimport cc_cedict>>> cc_cedict.load()# 使用 pinyin-data 项目中 kXHC1983.txt 文件中的拼音数据优化结果>>> frompypinyin_dict.pinyin_dataimport kxhc1983>>> kxhc1983.load()
  • 如果是分词导致的拼音有误的话,可以先使用其他的分词模块对数据进行分词处理, 然后将分词后的词组结果列表作为函数的参数即可:

>>> # 使用其他分词模块分词,比如 jieba 之类,>>> #或者基于 phrases_dict.py 里的词语数据使用其他分词算法分词>>> words = list(jieba.cut('每股24.67美元的确定性协议'))>>> pinyin(words)
  • 如果你希望能通过训练模型的方式提高拼音准确性的话,可以看一下 pypinyin-g2pW 这个项目。

为什么没有 y, w, yu 几个声母?

>>> frompypinyinimport Style, pinyin>>> pinyin('下雨天', style=Style.INITIALS)[['x'], [''], ['t']]

因为根据 《汉语拼音方案》 , y,w,ü (yu) 都不是声母。

声母风格(INITIALS)下,“雨”、“我”、“圆”等汉字返回空字符串,因为根据 《汉语拼音方案》 , y,w,ü (yu) 都不是声母,在某些特定韵母无声母时,才加上 y 或 w,而 ü 也有其特定规则。 —— @hotoo

如果你觉得这个给你带来了麻烦,那么也请小心一些无声母的汉字(如“啊”、“饿”、“按”、“昂”等)。 这时候你也许需要的是首字母风格(FIRST_LETTER)。 —— @hotoo

参考: hotoo/pinyin#57, #22, #27, #44

如果觉得这个行为不是你想要的,就是想把 y 当成声母的话,可以指定 strict=False , 这个可能会符合你的预期:

>>> frompypinyinimport Style, pinyin>>> pinyin('下雨天', style=Style.INITIALS)[['x'], [''], ['t']]>>> pinyin('下雨天', style=Style.INITIALS, strict=False)[['x'], ['y'], ['t']]

详见 strict 参数的影响

存在既没有声母也没有韵母的拼音?

是的,strict=True 模式下存在极少数既没有声母也没有韵母的拼音。 比如下面这些拼音(来自汉字 ):

ń ńg ňg ǹg ň ǹ m̄ ḿ m̀

尤其需要注意的是 的所有拼音都既没有声母也没有韵母, 的默认拼音既没有声母也没有韵母。 详见 #109 #259 #284

如何将某一风格的拼音转换为其他风格的拼音?

可以通过 pypinyin.contrib.tone_convert 模块提供的辅助函数对标准拼音进行转换,得到不同风格的拼音。 比如将 zhōng 转换为 zhong,或者获取拼音中的声母或韵母数据:

>>> frompypinyin.contrib.tone_convertimport to_normal, to_tone, to_initials, to_finals>>> to_normal('zhōng')'zhong'>>> to_tone('zhong1')'zhōng'>>> to_initials('zhōng')'zh'>>> to_finals('zhōng')'ong'

更多拼音转换的辅助函数,详见 pypinyin.contrib.tone_convert 模块的 文档

如何减少内存占用?

如果对拼音的准确性不是特别在意的话,可以通过设置环境变量 PYPINYIN_NO_PHRASESPYPINYIN_NO_DICT_COPY 来节省内存。 详见 文档

更多 FAQ 详见文档中的 FAQ 部分。

拼音数据

Related Projects

Project details

Verified details

These details have been verified by PyPI
Maintainers
👁 Avatar for mozillazg from gravatar.com
mozillazg

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypinyin-0.55.0.tar.gz (839.8 kB view details)

Uploaded Source

Built Distribution

Filter files by name, interpreter, ABI, and platform.

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

Copy a direct link to the current filters

pypinyin-0.55.0-py2.py3-none-any.whl (840.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pypinyin-0.55.0.tar.gz.

File metadata

  • Download URL: pypinyin-0.55.0.tar.gz
  • Upload date:
  • Size: 839.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pypinyin-0.55.0.tar.gz
Algorithm Hash digest
SHA256 b5711b3a0c6f76e67408ec6b2e3c4987a3a806b7c528076e7c7b86fcf0eaa66b
MD5 f4ec73fafb92bf6890be3fb9aefa2426
BLAKE2b-256 b4a4784cf98c09e0dc22776b0d7d8a4a5b761218bcae4608c2416ce1e167c8af

See more details on using hashes here.

File details

Details for the file pypinyin-0.55.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pypinyin-0.55.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 840.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pypinyin-0.55.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d53b1e8ad2cdb815fb2cb604ed3123372f5a28c6f447571244aca36fc62a286f
MD5 d99d2fe1dd1fd4b6d837e2a754d141f5
BLAKE2b-256 b97b4cabc76fcc21c3c7d5c671d8783984d30ac9d3bb387c4ba784fca3cdfa3a

See more details on using hashes here.

Supported by

👁 Image
AWS Cloud computing and Security Sponsor 👁 Image
Datadog Monitoring 👁 Image
Depot Continuous Integration 👁 Image
Fastly CDN 👁 Image
Google Download Analytics 👁 Image
Pingdom Monitoring 👁 Image
Sentry Error logging 👁 Image
StatusPage Status page