当前位置: 首页 > 后端技术 > Python

一个有趣好用的Python包——汉字转拼音

时间:2023-03-26 11:36:04 Python

作者:小吴师兄来源:AI学习简介一、应用概述最近在做一个项目,发现很多场景,汉字转拼音,然后执行深度学习分类,可以取得很好的效果。在做内容识别的时候,尤其是遇到同音字的时候,转拼音就显得尤为重要。比如垃圾广告识别:公众号,公众号,公总号,公众号,微信,微信,微一...,pypinyin是我用的一个比较好用的包分享给大家,当然也可以用在许多其他场景中,例如排序、检索等。2.相关文档GitHub:https://github.com/mozillazg/...文档:https://pypinyin.readthedocs...._CN/master/PyPi:https://pypi.org/project/pypi。..三、关于安装,可以使用pip安装pipinstallpypinyineasy_installinstalleasy_installpypinyinsourcecodeinstallpythonsetup.pyinstall四、核心功能1、pypinyin.pinyin语法:pypinyin.pinyin(hans,style=Style.TONE,heteronym=False,errors='default',strict=True)功能:将汉字转为拼音,返回汉字的拼音列表。参数:hans(unicodestringorlistofstrings)–中文字符串('你好吗')或列表(['你好','你好'])。可以使用自己喜欢的分词模块对字符串进行分词处理,传入分词后的字符串列表即可。style–指定拼音风格,默认为TONE风格。有关更多拼音样式,请参阅Styleerrors-指定如何处理没有拼音的字符。详见Handlingheteronymcharactersthatdonotcontainpinyin–是否开启和弦字符strict–是否严格按照《汉语拼音方案》处理声母和韵母,参见strict参数的影响frompypinyinimportpinyin,Styleimportpypinyinnormalmodepinyin('center')[['zhōng'],['xīn']]拼音('公众号')[['gōng'],['zhòng'],['hào']]开启和弦模式拼音('center',heteronym=True)[['zhōng','zhòng'],['xīn']]设置拼音风格pinyin('center',style=Style.NORMAL)normalstyle[['zhong'],['xin']]pinyin('center',style=Style.FIRST_LETTER)[['z'],['x']]pinyin('center',style=Style.TONE2)[['zho1ng'],['xi1n']]pinyin('center',style=Style.TONE3)[['zhong1'],['xin1']]pinyin('center',style=Style.CYRILLIC)汉语拼音和俄文字母比较风格[['чжун1'],['синь1']]2.pypinyin.lazy_pinyin语法:pypinyin.lazy_pinyin(hans,style=Style,errors='default',strict=True)功能:将汉字转为拼音,并返回拼音不包含和弦结果List,与pinyin()的区别在于返回的拼音是一个字符串,每个字符只包含一个发音参数:hans(_unicodeorlist_)–Chinesecharacterstyle–指定拼音风格,默认为正常风格。有关更多拼音样式,请参阅样式。errors–指定如何处理不带拼音的字符,详见pinyin()strict–是否严格按照《汉语拼音方案》处理声母和韵母,参见strict参数的影响frompypinyinimportlazy_pinyin,Styleimportpypinyinlazy_pinyin('center')['zhong','xin']lazy_pinyin('微信公众号')['wei','xin','gong','zhong','hao']lazy_pinyin('center',style=Style.TONE)['zhōng','xīn']lazy_pinyin('center',style=Style.FIRST_LETTER)['z','x']lazy_pinyin('center',style=Style.TONE2)['zho1ng','xi1n']lazy_pinyin('center',style=Style.CYRILLIC)['чжун1','синь1']3.pypinyin.slug函数:将汉字转成拼音,然后生成slug字符串,简单来说就是一个自定义分隔符语法:pypinyin.slug(hans,style=Style,heteronym=False,separator='-',errors='default',strict=True)hans(_unicodeorlist_)–汉字样式–指定拼音样式,默认为NORMAL样式。更多拼音样式参见Styleheteronym–是否启用和弦字符separator–两个拼音之间的分隔符/连接符errors–指定如何处理不带拼音的字符,请参见pinyin()strict–是否严格按照《汉语拼音方案》为首字母和决赛,看严格参数的影响'woshizhongguoren'pypinyin.slug('Chinese2020xiongqi',separator='')遇到数字等非汉字时不注音'zhongguoren2020xiongqi'pypinyin.slug('Chinesepeople2020雄奇',style=Style.FIRST_LETTER)'z-g-r-2020-x-q'pypinyin.slug('我是中国人',style=Style.CYRILLIC)'во3-ши4-чжун1-го2-жэнь'4,pypinyin.load_single_dict函数:加载用户自定义的单字拼音库语法:pypinyin.load_single_dict(pinyin_dict,style='default')参数:pinyin_dict(_dict_)–单字拼音库。例如:{0x963F:u"ā,ē"}style–pinyin_dict参数值的拼音风格。支持'default','tone2'5,pypinyin.load_phrases_dict函数:加载拼音库中用户自定义的词和词组语法:pypinyin.load_phrases_dict(phrases_dict,style='default')参数:phrases_dict(_dict_)–单词拼音库。例如:{u"Aba":[[u"ā"],[u"bà"]]}style——phrases_dict参数值的拼音库的风格。支持'default','tone2'5.如果需要找个案例垃圾评论的相似样本远不如中文拼音相似。这时候,拼音就可以发挥很大的优势。当然,转换成拼音后,把每个音节当成一个词来深度学习,效果也很好。S1='添加公众号:小优惠,优惠券,便宜购'S2='家公中豪:小优惠,零零,便宜购'中文相似simi_1=len(set(S1).intersection(set(S2)))/len(集合(S1)。)simi_2=len(集合(S1).intersection(集合(S2)))/len(集合(S1).union(集合(S2)))simi_20.875