Git Product home page Git Product logo

python-pinyin's Introduction

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

Build GitHubAction Coverage PyPI version PyPI downloads DOI

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

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

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

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

>>> from pypinyin import 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 )。
  • 默认情况下会原样输出没有拼音的字符(自定义处理没有拼音的字符的方法见 文档 )。

命令行工具:

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

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

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

可以通过自定义词组拼音库或者单字拼音库的方式修正拼音结果, 也可以使用 pypinyin-dict 项目提供的自定义拼音库来纠正结果。 详见 文档

>> from pypinyin import load_phrases_dict, load_single_dict

>> load_phrases_dict({'桔子': [['jú'], ['zǐ']]})  # 增加 "桔子" 词组

>> load_single_dict({ord('还'): 'hái,huán'})  # 调整 "还" 字的拼音顺序或覆盖默认拼音

# 使用 phrase-pinyin-data 项目中 cc_cedict.txt 文件中的拼音数据优化结果
>>> from pypinyin_dict.phrase_pinyin_data import cc_cedict
>>> cc_cedict.load()

# 使用 pinyin-data 项目中 cc_cedict.txt 文件中的拼音数据优化结果
>>> from pypinyin_dict.pinyin_data import kxhc1983
>>> kxhc1983.load()

如果是分词导致的拼音有误的话,可以先使用其他的分词模块对数据进行分词处理, 然后将分词后的词组结果列表作为函数的参数即可:

>>> # 使用其他分词模块分词,比如 jieba 之类,
>>> #或者基于 phrases_dict.py 里的词语数据使用其他分词算法分词
>>> words = list(jieba.cut('每股24.67美元的确定性协议'))
>>> pinyin(words)
>>> 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.contrib.tone_convert 模块对标准拼音进行转换,得到不同风格的拼音。 比如将 zhōng 转换为 zhong,或者获取拼音中的声母或韵母数据:

>>> from pypinyin.contrib.tone_convert import 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 部分。

python-pinyin's People

Contributors

mozillazg avatar howl-anderson avatar tyrbonit avatar bors-homu avatar mingstar avatar wdscxsj avatar hanabi1224 avatar gumblex avatar snyk-bot avatar secsilm avatar bowowzahoya avatar artoria2e5 avatar gitter-badger avatar timgates42 avatar yangwe1 avatar zacheryguan avatar dependabot-preview[bot] avatar mend-bolt-for-github[bot] 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.