Skip to main content

汉语拼音转换工具.

Project description

Build Coverage Pypi version Pypi downloads

将汉语转为拼音。可以用于汉字注音、排序、检索。

基于 hotoo/node-pinyin 开发。

安装

$ pip install pypinyin

为了更好的包含处理多音字及中英文混合的字符串, 推荐安装 jieba 分词模块。

使用示例

>>> from pypinyin import pinyin, lazy_pinyin
>>> import pypinyin
>>> pinyin(u'中心')
[[u'zh\u014dng'], [u'x\u012bn']]
>>> pinyin(u'中心', heteronym=True)  # 启用多音字模式
[[u'zh\u014dng', u'zh\xf2ng'], [u'x\u012bn']]
>>> pinyin(u'中心', style=pypinyin.STYLE_INITIALS)  # 设置拼音风格
[['zh'], ['x']]
>>> lazy_pinyin(u'中心')
['zhong', 'xin']

分词处理

如果安装了 jieba 分词模块,程序会自动调用。

使用其他分词模块:

  1. 安装分词模块,比如 pip install snownlp

  2. 使用经过分词处理的字符串列表作参数:

    >> from pypinyin import lazy_pinyin, STYLE_TONE2
    >> from snownlp import SnowNLP
    >> hans = u'音乐123'
    >>
    >> lazy_pinyin(hans, style=STYLE_TONE2)
    [u'yi1n', u'le4', u'1', u'2', u'3']
    >>
    >> hans_seg = SnowNLP(hans).words  # 分词处理
    >> hans_seg
    [u'\u97f3\u4e50', u'123']
    >> lazy_pinyin(hans_seg, style=STYLE_TONE2)
    [u'yi1n', u'yue4', u'123']

自定义拼音库

如果对结果不满意,可以通过自定义拼音库的方式修正结果:

>> from pypinyin import lazy_pinyin, load_phrases_dict, STYLE_TONE2
>> hans = u'桔子'
>>
>> lazy_pinyin(hans, style=STYLE_TONE2)
[u'jie2', u'zi3']
>>
>> load_phrases_dict({u'桔子': [[u'jú'], [u'zǐ']]})
>> lazy_pinyin(hans, style=STYLE_TONE2)
[u'ju2', u'zi3']

Changelog

0.4.1 (2014-01-04)

  • 支持自定义拼音库,方便用户修正程序结果

0.4.0 (2014-01-03)

  • jieba 模块改为可选安装,用户可以选择使用自己喜爱的分词模块对汉字进行分词处理

  • 支持 Python 3

0.3.1 (2013-12-24)

  • 增加 lazy_pinyin

    >>> lazy_pinyin(u'中心')
    ['zhong', 'xin']

0.3.0 (2013-09-26)

  • 修复首字母风格无法正确处理只有韵母的汉字

  • 新增三个拼音风格:
    • pypinyin.STYLE_FINALS : 韵母风格1,只返回各个拼音的韵母部分,不带声调。如: ong uo

    • pypinyin.STYLE_FINALS_TONE : 韵母风格2,带声调,声调在韵母第一个字母上。如: ōng uó

    • pypinyin.STYLE_FINALS_TONE2 : 韵母风格2,带声调,声调在各个拼音之后,用数字 [0-4] 进行表示。如: o1ng uo2

0.2.0 (2013-09-22)

  • 完善对中英文混合字符串的支持:

    >> pypinyin.pinyin(u'你好abc')
    [[u'n\u01d0'], [u'h\u01ceo'], [u'abc']]

0.1.0 (2013-09-21)

  • Initial Release

Project details


Release history Release notifications | RSS feed

This version

0.4.1

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.4.1.zip (608.1 kB view details)

Uploaded Source

File details

Details for the file pypinyin-0.4.1.zip.

File metadata

  • Download URL: pypinyin-0.4.1.zip
  • Upload date:
  • Size: 608.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pypinyin-0.4.1.zip
Algorithm Hash digest
SHA256 923f184aed5a1612020e9a058eccb8984672034fe84a4ac1e0b4ab925a1c304d
MD5 8905382083dad64315f7c83ab57208b7
BLAKE2b-256 8bfb94470f4296470170d2b947eea3083d6e6eea10595cee5723e3c3b87db50b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page