• Stars
    star
    4,448
  • Rank 9,144 (Top 0.2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

汉字转拼音(pypinyin)

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

Build GitHubAction Coverage PyPI version PyPI downloads DOI

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

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

特性

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

安装

pip install pypinyin

使用示例

>>> 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 )。
  • 默认情况下会原样输出没有拼音的字符(自定义处理没有拼音的字符的方法见 文档 )。
  • 的拼音并不是大部分人以为的 en 以及存在既没有声母也没有韵母的拼音,详见下方 FAQ 中的说明。

命令行工具:

$ pypinyin 音乐
yīn yuè

$ python -m pypinyin.tools.toneconvert to-tone 'zhong4 xin1'
zhòng xīn

文档

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

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

FAQ

拼音有误?

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

  • 可以通过自定义词组拼音库或者单字拼音库的方式修正拼音结果, 详见 文档
>> from pypinyin import 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 文件中的拼音数据优化结果
>>> from pypinyin_dict.phrase_pinyin_data import cc_cedict
>>> cc_cedict.load()

# 使用 pinyin-data 项目中 kXHC1983.txt 文件中的拼音数据优化结果
>>> from pypinyin_dict.pinyin_data import kxhc1983
>>> kxhc1983.load()
  • 如果是分词导致的拼音有误的话,可以先使用其他的分词模块对数据进行分词处理, 然后将分词后的词组结果列表作为函数的参数即可:
>>> # 使用其他分词模块分词,比如 jieba 之类,
>>> #或者基于 phrases_dict.py 里的词语数据使用其他分词算法分词
>>> words = list(jieba.cut('每股24.67美元的确定性协议'))
>>> pinyin(words)
  • 如果你希望能通过训练模型的方式提高拼音准确性的话,可以看一下 pypinyin-g2pW 这个项目。

为什么没有 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 参数的影响

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

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

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

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

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

可以通过 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 部分。

拼音数据

More Repositories

1

go-pinyin

汉字转拼音
Go
1,467
star
2

pinyin-data

汉字拼音数据
Python
1,025
star
3

request

A developer-friendly HTTP request library for Gopher.
Go
427
star
4

pypy

The unofficial GitHub mirror of PyPy (mirrored via https://github.com/mozillazg/job-mirror-hg-repos)
Python
425
star
5

phrase-pinyin-data

词语拼音数据
Python
392
star
6

rust-pinyin

汉字转拼音
Rust
190
star
7

baidu-pcs-python-sdk

百度个人云存储(PCS)Python SDK. (因为 PCS 服务已关闭,推荐改用百度网盘 API: https://github.com/ly0/baidupcsapi )
Python
182
star
8

ShortURL

A URL Shortener Site 短网址生成网站(web.py)
Python
167
star
9

go-unidecode

ASCII transliterations of Unicode text.
Go
111
star
10

go-cos

腾讯云对象存储服务 COS(Cloud Object Storage) Go SDK(XML API)
Go
89
star
11

go-slugify

Pretty Slug.
Go
88
star
12

random-avatar

Random Avatar(Identicon) Service.
Python
45
star
13

go-httpheader

A Go library for encoding structs into Header fields.
Go
44
star
14

bustard

A tiny WSGI web framework
Python
44
star
15

python-shanbay

提供一系列操作扇贝网 (www.shanbay.com) 的 API(不再维护)
Python
43
star
16

pyqr

[web.py]Online QR Code Generator 在线生成二维码图片
Python
38
star
17

pypinyin-g2pW

基于 g2pW 提升 pypinyin 的准确性
Python
33
star
18

hello-libbpfgo

examples for libbpf and libbpfgo
Makefile
32
star
19

flask-sites

A Website - Collecting Websites Powered By Flask. http://flasksites.org
JavaScript
31
star
20

apm-python-agent-principle

Python 探针实现原理
Python
29
star
21

PyShanb

命令行下的扇贝(shanbay.com)词典(停止维护)
Python
26
star
22

pypinyin-dict

使用 pinyin-data 和 phrase-pinyin-data 中的拼音数据文件覆盖 pypinyin 中的内置拼音数据
Python
25
star
23

libbpfgo-tools

libbpfgo port of bcc/libbpf-tools
Go
23
star
24

blog

My Blog
Python
19
star
25

python-shanbay-team-assistant

扇贝网 (www.shanbay.com) 小组管理助手
Python
18
star
26

pkuic-001

https://class.coursera.org/pkuic-001/class/index
C++
14
star
27

justping

找出 ping 值最小的 IP/域名
Python
14
star
28

lark

Music FM
Python
13
star
29

my-blog-file

my blog post source file(markdown )
Perl
13
star
30

lsbate

Let's Build A Template Engine(让我们一起来构建一个模板引擎)
Python
12
star
31

tinyq

A tiny job queue framework
Python
10
star
32

django-endless-pagination-bootstrap-theme

An bootstrap theme for django-endless-pagination.
7
star
33

mozillazg

4
star
34

hello-python-plugin

尝试实现简单的插件功能。
Python
3
star
35

qiniu-cli

Qiniu CLI tool
Python
3
star
36

alibabacloud-oidc-auth

GitHub Action for authenticating to Alibaba Cloud with GitHub Actions OIDC tokens.
TypeScript
3
star
37

echo-k8s-webhook

Dump k8s Admission webhook request payload
Go
3
star
38

go-o3o

a ascii emoticon generator based on Go
Go
3
star
39

mozillazg.github.com

https://mozillazg.github.io/
HTML
3
star
40

comkc

Python
3
star
41

Unidecode

The unofficial GitHub mirror of Unidecode
Python
2
star
42

go-charset

Get the content charset from header and html content-type.
Go
2
star
43

flask-demo

Hello Flask.
Python
2
star
44

image-mime

根据图片内容判断 MIME 类型
Python
2
star
45

aiobearychat

BearyChat 异步 Python SDK
Python
2
star
46

django-simple-projects

Some demos
JavaScript
2
star
47

python-mini-script

some python script
Python
2
star
48

python-shellwords

Parse line as shell words
Python
2
star
49

webhookcert

A simple certificate solution for writing Kubernetes Webhook Server
Go
1
star
50

mirror-hg-repo

A GitHub Action to mirror Mercurial (hg) repository to GitHub.
TypeScript
1
star
51

gobpf-examples

C
1
star
52

docker-credential-acr-helper

A credential helper for the Docker daemon that makes it easier to use Alibaba Cloud Container Registry(ACR).
Go
1
star
53

qn_cli

Qiniu upload client written in Go.
Go
1
star
54

mtum

Clone tumblr.com with django (just for learn django) 使用 django 实现 tumblr.com
Python
1
star
55

Markdown-textarea

http://userscripts.org/scripts/show/91369
JavaScript
1
star
56

f

Ruby-Style String Interpolation for Python.
Python
1
star
57

file-hash

file hash(MD5,SHA1,CRC32) [Python]
Python
1
star
58

binaryless

Binaryless Base Images
Makefile
1
star
59

comic

http://comic.mozillazg.com
HTML
1
star
60

coscli

CLI for COS
Go
1
star
61

snippets

Code Snippets
Go
1
star
62

scripts

some tools and/or scripts.
1
star
63

go-chardet

Character encoding auto-detection in Go.
1
star
64

hide-github-fork-button.user.js

Hide github fork button for some reason
JavaScript
1
star
65

stpinyin

Convert pinyin like this: you1 -> yōu
Go
1
star
66

xiaoai

Python
1
star
67

webpy-code-examples

web.py Code samples ( http://webpy.org/src/ )
Python
1
star