发布时间:2024-01-22阅读(4)
最近做一个项目,发现很多场景,把汉字转换成拼音,然后进行深度学习分类,能够取得非常不错的效果,在做内容识别,特别是涉及到同音字的时候,转换成拼音就显得特别重要。比如垃圾广告识别:公众号、工仲号、躬总号,公众號、微信、威信、维伈.........,pypinyin是我用的一个比较好用的包是
给大家分享下,当然,在其他很多场景也是可以使用的,排序、检索等等场合。
二、有关文档GitHub: https://github.com/mozillazg/Python-pinyin
文 档:https://pypinyin.readthedocs.io/zh_CN/master/
PyPi :https://pypi.org/project/pypinyin/
三、关于安装#可以使用 pip 进行安装
pip install pypinyin
#easy_install 安装
easy_install pypinyin
#源码安装
python setup.py install
语法:pypinyin.pinyin(hans, Style=Style.TONE, heteronym=False, errors=default, strict=True)
功能:将汉字转换为拼音,返回汉字的拼音列表。
参数:
hans (unicode 字符串或字符串列表) – 汉字字符串( 你好吗 )或列表( [你好, 吗] ). 可以使用自己喜爱的分词??槎宰址蟹执蚀? 只需将经过分词处理的字符串列表传进来就可以了。
style – 指定拼音风格,默认是 TONE 风格。更多拼音风格详见 Style
errors –指定如何处理没有拼音的字符。详见 处理不包含拼音的字符
heteronym – 是否启用多音字
strict – 是否严格遵照《汉语拼音方案》来处理声母和韵母,详见 strict 参数的影响
from pypinyin import pinyin, Style
import pypinyin
#普通模式
pinyin(中心)
[[zhōng], [xīn]]
pinyin(公众号)
[[gōng], [zhòng], [hào]]
# 启用多音字模式
pinyin(中心, heteronym=True)
[[zhōng, zhòng], [xīn]]
# 设置拼音风格
pinyin(中心, style=Style.NORMAL ) #普通风格
[[zhong], [xin]]
pinyin(中心, style=Style.FIRST_LETTER)
[[z], [x]]
pinyin(中心, style=Style.TONE2)
[[zho1ng], [xi1n]]
pinyin(中心, style=Style.TONE3)
[[zhong1], [xin1]]
pinyin(中心, style=Style.CYRILLIC)#汉语拼音与俄语字母对照风格
[[чжун1], [синь1]]
语法:pypinyin.lazy_pinyin(hans, style=Style, errors=default, strict=True)
功能:将汉字转换为拼音,返回不包含多音字结果的拼音列表,与 pinyin 的区别是返回的拼音是个字符串, 并且每个字只包含一个读音
参数:
hans(unicode or list) – 汉字
style– 指定拼音风格,默认是NORMAL风格。更多拼音风格详见Style。
errors– 指定如何处理没有拼音的字符,详情请参考pinyin
strict– 是否严格遵照《汉语拼音方案》来处理声母和韵母,详见 strict 参数的影响
from pypinyin import lazy_pinyin, Style
import pypinyin
lazy_pinyin(中心)
[zhong, xin]
lazy_pinyin(微信公众号)
[wei, xin, gong, zhong, hao]
lazy_pinyin(中心, style=Style.TONE)
[zhōng, xīn]
lazy_pinyin(中心, style=Style.FIRST_LETTER)
[z, x]
lazy_pinyin(中心, style=Style.TONE2)
[zho1ng, xi1n]
lazy_pinyin(中心, style=Style.CYRILLIC)
[чжун1, синь1]
功能:将汉字转换为拼音,然后生成 slug 字符串,简单说就是自定义分隔符
语法:pypinyin.slug(hans , style=Style, heteronym=False, separator=-, errors=default, strict=True)
hans(unicode or list) – 汉字
style– 指定拼音风格,默认是NORMAL风格。更多拼音风格详见Style
heteronym– 是否启用多音字
separator– 两个拼音间的分隔符/连接符
errors– 指定如何处理没有拼音的字符,详情请参考pinyin
strict– 是否严格遵照《汉语拼音方案》来处理声母和韵母,详见 strict 参数的影响
import pypinyin
from pypinyin import Style
pypinyin.slug(我是中国人)
wo-shi-zhong-guo-ren
pypinyin.slug(我是中国人, separator= )
wo shi zhong guo ren
pypinyin.slug(中国人2020雄起, separator= )#遇到数字等非汉字不注音
zhong guo ren 2020 xiong qi
pypinyin.slug(中国人2020雄起, style=Style.FIRST_LETTER)
z-g-r-2020-x-q
pypinyin.slug(我是中国人, style=Style.CYRILLIC)
во3-ши4-чжун1-го2-жэнь
功能:载入用户自定义的单字拼音库
语法:pypinyin.load_single_dict(pinyin_dict, style=default)
参数:
pinyin_dict(dict) – 单字拼音库。比如: {0x963F: u"ā,ē"}
style– pinyin_dict 参数值的拼音库风格. 支持 ‘default’, ‘tone2’
功能:载入用户自定义的词语拼音库
语法:pypinyin.load_phrases_dict(phrases_dict, style=default)
参数:
phrases_dict(dict) – 词语拼音库。比如: {u"阿爸": [[u"ā"], [u"bà"]]}
style– phrases_dict 参数值的拼音库风格. 支持 ‘default’, ‘tone2’
假如需要找出一个垃圾评价的相似样本,用汉语相似性远远小于拼音,这个时候,拼音就能发挥很大的优势。
当然转换成拼音后,把每个音节当一个词,进行深度学习,效果也是非常好的。
S1 = 加公众号:小优惠,领券,便宜购买
S2 = 伽工仲号:小优惠,伶绻,便宜购买
#汉语相似
simi_1 = len(set(S1).intersection(set(S2)))/len(set(S1).union(set(S2)))#相似不懂的可以看我前面集合的文章
simi_1
0.5
#转换成拼音后显示
S1 = lazy_pinyin(S1)
S2 = lazy_pinyin(S2)
simi_2 = len(set(S1).intersection(set(S2)))/len(set(S1).union(set(S2)))
simi_2
0.875
Python学习交流群
为了让大家更加即时地沟通学习,我们建了一个Python学习交流群,有想入群的同学,可以添加下面小助手微信,他会拉大家入群哈~
欢迎分享转载→http://www.umpkq.cn/read-76433.html
下一篇:一个礼拜是多少天
Copyright ? 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图