GithubHelp home page GithubHelp logo

hhy5277 / python-pinyin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mozillazg/python-pinyin

0.0 1.0 0.0 4.29 MB

汉字转拼音(pypinyin)

Home Page: https://pypinyin.readthedocs.io

License: MIT License

Makefile 0.06% Python 99.94%

python-pinyin's Introduction

汉字拼音转换工具(Python 版)

Build Coverage PyPI version

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

基于 hotoo/pinyin 开发。

特性

  • 根据词组智能匹配最正确的拼音。
  • 支持多音字。
  • 简单的繁体支持, 注音支持。
  • 支持多种不同拼音/注音风格。

安装

$ pip install pypinyin

使用示例

Python 3(Python 2 下把 '中心' 替换为 u'中心' 即可):

>>> from pypinyin import pinyin, lazy_pinyin, Style
>>> 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.BOPOMOFO)  # 注音风格
[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
>>> pinyin('中心', style=Style.CYRILLIC)  # 俄语字母风格
[['чжун1'], ['синь1']]
>>> lazy_pinyin('中心')  # 不考虑多音字的情况
['zhong', 'xin']

注意事项

  • 拼音结果不会标明哪个韵母是轻声,轻声的韵母没有声调或数字标识。
  • 无声调相关拼音风格下的结果会使用 v 表示 ü

命令行工具:

$ pypinyin 音乐
yīn yuè
$ pypinyin -h

文档

详细文档请访问:http://pypinyin.rtfd.io/

FAQ

词语中的多音字拼音有误?

目前是通过词组拼音库的方式来解决多音字问题的。如果出现拼音有误的情况, 可以自定义词组拼音来调整词语中的拼音:

>>> from pypinyin import Style, pinyin, load_phrases_dict
>>> pinyin('步履蹒跚')
[['bù'], ['lǚ'], ['mán'], ['shān']]
>>> load_phrases_dict({'步履蹒跚': [['bù'], ['lǚ'], ['pán'], ['shān']]})
>>> pinyin('步履蹒跚')
[['bù'], ['lǚ'], ['pán'], ['shān']]

详见 文档

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

>>> from pypinyin import 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 , 这个可能会符合你的预期:

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

详见 strict 参数的影响

如何减少内存占用

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

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

拼音数据

Related Projects

python-pinyin's People

Contributors

artoria2e5 avatar bors-homu avatar gitter-badger avatar gumblex avatar mingstar avatar mozillazg avatar tyrbonit avatar wdscxsj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.