mozillazg / python-pinyin Goto Github PK
View Code? Open in Web Editor NEW汉字转拼音(pypinyin)
Home Page: https://pypinyin.readthedocs.io
License: MIT License
汉字转拼音(pypinyin)
Home Page: https://pypinyin.readthedocs.io
License: MIT License
实际上是分词算法有问题,需要重写分词算法。
from pypinyin import load_phrases_dict, lazy_pinyin, TONE2
lazy_pinyin('还没', style = TONE2)
['hua2n', 'me2i']
from pypinyin import load_phrases_dict, lazy_pinyin, TONE
lazy_pinyin('还没', style = TONE)
['huán', 'méi']
load_phrases_dict({'还没': [['hái'], ['méi']]})
lazy_pinyin('还没', style = TONE)
['huán', 'méi']
用户可以选择使用自己喜爱的分词模块,只需要将经过分词模块处理的结果传给 pypinyin 就可以了:
hans = seg(u'你好吗') # 分词模块返回一个列表: [u'你好', u'吗']
pypinyin.pinyin(hans) # pinyin
类似
pinyin(u'中心', style=pypinyin.INITIALS) # 设置拼音风格
[['zh'], ['x']]
代码中声母表
_INITIALS = 'b,p,m,f,d,t,n,l,g,k,h,j,q,x,zh,ch,sh,r,z,c,s,'.split(',')
没有y和w。如果碰到Y和W开头的字,相应的字会返回空。
例如:
pinyin(u'火影忍者', style=pypinyin.INITIALS)
[[u'h'], [u''], [u'r'], [u'zh']]
我查了下资料,有的说声母不包括Y和W,所以这个返回是正常,但这样处理导致应用不好做,只能用首字母模式替代。是否新加一个接口,加上Y和W的返回,或者,说明上告知这个情况,以免别人使用的时候碰到问题。
只保留『有多音字的词组』,没有多音字的词组以普通的拼音转换即可。
ref: hotoo/pinyin#23
>>> s=u"两年前七斤喝醉了酒"
>>> pypinyin.lazy_pinyin(s)
[u'liang', u'nian', u'qian', u'qi', u'jin', u'he', u'zui', u',he', u'zui', u'jiu', u'liao', u'jiu']
结果中多了u',he', u'zui', u'jiu',
应该是bug
node版本没有这个问题:
> var pinyin = require("pinyin");
undefined
> s='你好了解了'
'你好了解了'
> pinyin(s)
[ [ 'nǐ' ], [ 'hǎo' ], [ 'liǎo' ], [ 'jiě' ], [ 'liǎo' ] ]
> s='两年前七斤喝醉了酒'
'两年前七斤喝醉了酒'
> pinyin(s)
[ [ 'liǎng' ],
[ 'nián' ],
[ 'qián' ],
[ 'qī' ],
[ 'jīn' ],
[ 'hē' ],
[ 'zuì' ],
[ 'liǎo' ],
[ 'jiǔ' ] ]
如“打量”,输出 [['da3'], ['liang']]
,文档中写“用数字 [0-4] 进行表示”。这种情况除了 ü 都有。请问该修文档还是程序行为?
先有的功能下,如果系统内有安装 jieba
的话,
pinyin(u'你好')
会自动调用 jieba
进行分词,只能通过 pinyin([u'你好'])
的方式进行禁用。
将传入的字符串按是否有拼音来分割:
'你好吗にほんごРусский язык我很好' -> ['你好吗', 'にほんごРусский язык', '我很好']
目前结巴分词结果是:
'你好吗にほんごРусский язык我很好' -> ['你好吗', 'に', 'ほ', 'ん', 'ご', 'Р', 'у', 'с', 'с', 'к', 'и', 'й', ' ', 'я', 'з', 'ы', 'к', '我很好']
https://github.com/mozillazg/python-pinyin/blob/master/pypinyin/__init__.py#L256
In [4]: lazy_pinyin(u'张靓颖')
Out[4]: [u'zhang', u'jing', u'ying']
第二个字应该为 liang
>>> pypinyin.pinyin("狼")
[['狼']]
load_phrases_dict({'几': [['jǐ']]})
load_phrases_dict({'桔子': [['jú'], ['zǐ']]})
load_phrases_dict({'还没': [['hái'], ['méi']]})
load_phrases_dict({'不用谢':[['bú'], ['yòng'],['xiè']]})
Output
不用谢(bú yòng xiè)
桔子(jú zǐ)
还没(huán méi)
几(jǐ)
So 还没 is incorrect. Why?
ref: hotoo/pinyin#26
通过汉典网按 unicode 码获取所有的汉字和拼音(只获取有拼音的汉字)。
CJK基本:[4E00-9FFF]
CJK扩展A:[3400-4DBF]
CJK扩展B:[20000-2A6DF]
CJK扩展C:[2A700-2B73F]
CJK扩展D:[2B740-2B81D]
CJK兼容扩展:[2F800-2FA1F]
CJK兼容:[F900-FAFF]
『卡』字的拼音总是注音成 qia
网卡
显卡
『莎』suo
浪莎
core.py中的pinyin 方法
for words in hans:
pys.extend(_pinyin(words, style, heteronym, errors))
这个地方用的是extend,导致即使使用了jieba分词,分出来的结果还是返回一个一个汉字的拼音list
区别不出来那几个是一个词语,跟没分一样。。
比如汉字“网”
按照顺序输出应该是 w w wang wa3ng
但是实际上的输出是 w wang wa3ng
声母部分为空,测试了好几个单字母声母的汉字都是这样的情况
对岸常用的注音符号是可以从拼音数据转换出来的,至于语文标准审定字音那也不是这个项目的事情。
可以参考 https://github.com/The-Orizon/nlputils/blob/master/libpinyin_bopomofo.py 的转换(我也不知道 tone2 是什么格式,本来看文档还以为是 to2ne 呢)。
Style | Desc |
---|---|
STYLE_BOPOMOFO | 普通的带声调注音。注音的声调永远在最后,阴平(第一声)不标。 |
STYLE_BOPOMOFO_NOTONE | 没声调。 |
注音本身可以说有类似双拼的特性。
>>> from pypinyin import pinyin
>>> pinyin('你会')
[['nǐ'], ['huì']]
>>> pinyin('妳會')
[['nǎi'], ['huì']]
拼音库主要依赖的是拼音字典、词典(后面简称“词典”),这个词典共用性很高,但由于词典库较大,出现问题的概率的也高。
建议大家一起共建、共同维护这个词典,你们觉得怎么样?
#41 #42
cc @mozillazg
我发现有些字不是多音字也识别为多音字了,请问怎么避免这种情况呢
pinyin(u'分', heteronym=True)
fēn
fèn
fén
bàn
pinyin(u'平', heteronym=True)
píng
pián
bìng
bēng
$ pypinyin 你好
nǐ hǎo
load_phrases_dict({u'弹力素': [[u'tan'], [u'li'], u['su']]})
print lazy_pinyin(u'弹力素', style=NORMAL, errors=handle_error)
Output: [u'dan', u'li', u'su']
是分词的问题。把弹力素切成两个词了。只加入'弹力'就好了。
但是希望能把用户词典里的词能添加到结巴中当做单独的词汇。
虽然v可以在python中轻易转换,不过还是想问一下,是不是有办法不用lv se(绿色),而用lü se,我是中文教师,如果用lv会和一般教材产生不统一的问题。
这个工具是我最爱用的python工具之一,非常感谢!
我试了几例:
请问是我用法错误,还是缺少多音字库?
不好意思,看到参数啦:pypinyin.pinyin(u'长江水长长长长长长长', style=pypinyin.NORMAL, heteronym=True)
如果使用结巴分词来处理多音字,可以在使用前先调用这个函数来训练结巴的词库。
class Pinyin(object):
def __init__(self, ...):
pass
def pinyin(self, hans):
pass
# ...
# Backward compatibility
def pinyin(hans, ....):
return Pinyin(...).pinyin(hans)
这样使用者方便加入拼音,带音标的字母比较难输入
首先谢谢提供如此便利的汉字转拼音工具
近日在使用时遇到这样的问题
>>> from pypinyin import lazy_pinyin
>>> lazy_pinyin('彷徨')
['panghuang']
预想得到的应该是['pang', 'huang']?
我的环境是Python 3.5.1, pypinyin 0.12.0
使用的是python 3.4
pypinyin.slug('王', pypinyin.INITIALS, separator='')
无法正确获得 ‘y’, 而返回一个空串
when use pypinyin.lazy_pinyin
or pypinyin.pinyin
, it transform '厦门' to 'shamen', but the right output is 'xiamen'
res = pinyin(u'你明天在上海吗', style=pypinyin.TONE)
for word in res:
print word[0]
结果如下
nǐ
míng
tiān
zài
shàng
hǎi
má
最后那个“吗”字拼音错了,感觉是常见字,不应该出错
pip install jieba
pip install pypinyin
from pypinyin import pinyin, lazy_pinyin
print lazy_pinyin(u'黄山B股')
[u'huang', u'shan', u'B', u'\u80a1']
输出结果应该是en才是啊。
import pypinyin
zi = '苹果'
py = pypinyin.slug(zi, style=pypinyin.NORMAL, separator=' ')
Building Trie..., from /usr/local/lib/python3.4/site-packages/jieba/dict.txt
loading model from cache /var/folders/k9/47fd1ycj2rg19gn7d2g5g16c0000gn/T/jieba.cache
loading model cost 2.451965808868408 seconds.
Trie has been built succesfully.
print(py)
pin guo
What I was looking for is "ping guo".
Do we use a conversion table in python-pinyin? I probably should search for it before asking. If yes, I'm interested in this table.
In [12]: pinyin("中心")
Out[12]: [['zhōng'], ['xīn']]
In [13]: pinyin("重心")
Out[13]: [['zhòng'], ['xīn']]
In [14]: pinyin("情调来调整风格")
Out[14]: [['qíng'], ['diào'], ['lái'], ['diào'], ['zhěng'], ['fēng'], ['gé']]
In [15]: pinyin("调整风格")
Out[15]: [['diào'], ['zhěng'], ['fēng'], ['gé']]
In [16]: pinyin("调整风格")
Out[16]: [['diào'], ['zhěng'], ['fēng'], ['gé']]
In [17]: pinyin("调整")
Out[17]: [['tiáo'], ['zhěng']]
In [18]: pinyin("调薪")
Out[18]: [['diào'], ['xīn']]
分词了之后识别还是有问题
是否支持训练功能来纠正?
类似 str.encode()
方法的 errors
参数。
只处理 [^a-zA-Z0-9_] 字符。
-help里的argument总是报错
我试了下面的code:
print(lazy_pinyin("了",style=1)) #['le']
print(pinyin("了",style=1)) #[['le']]
请问这是一个bug吗?😉
$ echo "你好" | pypinyin
nǐ hǎo
在做一个给汉字添加拼音的功能,但是基于现有的接口没有办法把中英混合的字mapping上。
可否提供接口实现:
lazy_pinyin(u'你好abc☆☆')
#[u'ni', u'hao', 'a', 'b', 'c', u'\u2606', u'\u2606']
或者 暴露出来分词的接口,这样能够mapping上,或者直接能够返回一个dict
https://github.com/hotoo/pinyin 的实现是:
TONE 标准声调风格
TONE2 声调以数字形式在末尾的风格(当初我这样命名的原因)
TO3NE 声调以数字形式在中间的风格
考虑是否改为它这种风格。
I need strict mapping between the input and output, how can i do?
>> lazy_pinyin('你好')
['ni', 'hao'] # i need {'你': 'ni', '好': 'hao'}
返回的是[[u'bang'], [u'bo'], [u'de'], [u'chan']]
实际应该是:pang, bo, di, chan
print pinyin(u'丽水园茶坊(成都市华厦)',pypinyin.NORMAL)
[[u'li'], [u'shui'], [u'yuan'], [u'cha'], [u'fang'], [u'('], [u'cheng'], [u'dou'], [u'shi'], [u'hua'], [u'sha'], [u')']]
成都读音不对
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.