99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)大數(shù)據(jù)時(shí)代一個(gè)有意思還有用的python包-漢字轉(zhuǎn)換拼音
一個(gè)有意思還有用的python包-漢字轉(zhuǎn)換拼音
2020-08-31
收藏

作者:小伍哥 

來(lái)源:AI入門學(xué)習(xí)

一、應(yīng)用概述

最近做一個(gè)項(xiàng)目,發(fā)現(xiàn)很多場(chǎng)景,把漢字轉(zhuǎn)換成拼音,然后進(jìn)行深度學(xué)習(xí)分類,能夠取得非常不錯(cuò)的效果,在做內(nèi)容識(shí)別,特別是涉及到同音字的時(shí)候,轉(zhuǎn)換成拼音就顯得特別重要。比如垃圾廣告識(shí)別:公眾號(hào)、工仲號(hào)、躬總號(hào),公眾號(hào)、微信、威信、維伈.........,pypinyin是我用的一個(gè)比較好用的Python包,給大家分享下,當(dāng)然,在其他很多場(chǎng)景也是可以使用的,排序、檢索等等場(chǎng)合。

二、有關(guān)文檔

GitHub: https://github.com/mozillazg/python-pinyin

文   檔:https://pypinyin.readthedocs.io/zh_CN/master/

PyPi  :https://pypi.org/project/pypinyin/

三、關(guān)于安裝

#可以使用 pip 進(jìn)行安裝
pip install pypinyin

#easy_install 安裝
easy_install pypinyin

#源碼安裝
python setup.py install

四、核心函數(shù)

1、pypinyin.pinyin

語(yǔ)法:pypinyin.pinyin(hans, style=Style.TONE,  heteronym=False, errors='default', strict=True)

功能:將漢字轉(zhuǎn)換為拼音,返回漢字的拼音列表。

參數(shù):

  • hans (unicode 字符串或字符串列表) – 漢字字符串( '你好嗎' )或列表( ['你好', '嗎'] ). 可以使用自己喜愛(ài)的分詞模塊對(duì)字符串進(jìn)行分詞處理, 只需將經(jīng)過(guò)分詞處理的字符串列表傳進(jìn)來(lái)就可以了。
  • style – 指定拼音風(fēng)格,默認(rèn)是 TONE 風(fēng)格。更多拼音風(fēng)格詳見(jiàn) Style
  • errors –指定如何處理沒(méi)有拼音的字符。詳見(jiàn) 處理不包含拼音的字符
  • heteronym – 是否啟用多音字
  • strict – 是否嚴(yán)格遵照《漢語(yǔ)拼音方案》來(lái)處理聲母和韻母,詳見(jiàn) strict 參數(shù)的影響
from pypinyin import pinyin, Style
import pypinyin
#普通模式
pinyin('中心')
[['zhōng'], ['xīn']]
pinyin('公眾號(hào)')
[['gōng'], ['zhòng'], ['hào']]
# 啟用多音字模式
pinyin('中心', heteronym=True)
[['zhōng', 'zhòng'], ['xīn']]
# 設(shè)置拼音風(fēng)格
pinyin('中心', style=Style.NORMAL )
#普通風(fēng)格
[['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)
#漢語(yǔ)拼音與俄語(yǔ)字母對(duì)照風(fēng)格
[['чжун1'], ['синь1']]

2、pypinyin.lazy_pinyin

語(yǔ)法:pypinyin.lazy_pinyin(hans, style=Style, errors='default', strict=True)

功能:將漢字轉(zhuǎn)換為拼音,返回不包含多音字結(jié)果的拼音列表,與 pinyin() 的區(qū)別是返回的拼音是個(gè)字符串, 并且每個(gè)字只包含一個(gè)讀音

參數(shù):

  • hans (unicode or list) – 漢字
  • style – 指定拼音風(fēng)格,默認(rèn)是 NORMAL 風(fēng)格。更多拼音風(fēng)格詳見(jiàn) Style。
  • errors – 指定如何處理沒(méi)有拼音的字符,詳情請(qǐng)參考 pinyin()
  • strict – 是否嚴(yán)格遵照《漢語(yǔ)拼音方案》來(lái)處理聲母和韻母,詳見(jiàn) strict 參數(shù)的影響
from pypinyin import lazy_pinyin, Style
import pypinyin
lazy_pinyin('中心')
['zhong', 'xin']lazy_pinyin('微信公眾號(hào)')['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']

3、pypinyin.slug

功能:將漢字轉(zhuǎn)換為拼音,然后生成 slug 字符串,簡(jiǎn)單說(shuō)就是自定義分隔符

語(yǔ)法:pypinyin.slug(hans , style=Style, heteronym=False, separator='-', errors='default', strict=True)

  • hans (unicode or list) – 漢字
  • style – 指定拼音風(fēng)格,默認(rèn)是 NORMAL 風(fēng)格。更多拼音風(fēng)格詳見(jiàn) Style
  • heteronym – 是否啟用多音字
  • separator – 兩個(gè)拼音間的分隔符/連接符
  • errors – 指定如何處理沒(méi)有拼音的字符,詳情請(qǐng)參考 pinyin()
  • strict – 是否嚴(yán)格遵照《漢語(yǔ)拼音方案》來(lái)處理聲母和韻母,詳見(jiàn) strict 參數(shù)的影響
import pypinyin
from pypinyin import Style
pypinyin.slug('我是中國(guó)人')
'wo-shi-zhong-guo-ren'
pypinyin.slug('我是中國(guó)人', separator=' ')
'wo shi zhong guo ren'
pypinyin.slug('中國(guó)人2020雄起', separator=' ')
#遇到數(shù)字等非漢字不注音'zhong guo ren 2020 xiong qi'
pypinyin.slug('中國(guó)人2020雄起', style=Style.FIRST_LETTER)
'z-g-r-2020-x-q'
pypinyin.slug('我是中國(guó)人', style=Style.CYRILLIC)
'во3-ши4-чжун1-го2-жэнь'

 4、 pypinyin.load_single_dict

功能:載入用戶自定義的單字拼音庫(kù)

語(yǔ)法: pypinyin.load_single_dict(pinyin_dict, style='default')

參數(shù):

  • pinyin_dict (dict) – 單字拼音庫(kù)。比如: {0x963F: u"ā,ē"}
  • style – pinyin_dict 參數(shù)值的拼音庫(kù)風(fēng)格. 支持 ‘default’, ‘tone2’

5、 pypinyin.load_phrases_dict

功能:載入用戶自定義的詞語(yǔ)拼音庫(kù)

語(yǔ)法: pypinyin.load_phrases_dict(phrases_dict, style='default')

參數(shù):

  • phrases_dict (dict) – 詞語(yǔ)拼音庫(kù)。比如: {u"阿爸": [[u"ā"], [u"bà"]]}
  • style – phrases_dict 參數(shù)值的拼音庫(kù)風(fēng)格. 支持 ‘default’, ‘tone2’

五、一個(gè)案例

假如需要找出一個(gè)垃圾評(píng)價(jià)的相似樣本,用漢語(yǔ)相似性遠(yuǎn)遠(yuǎn)小于拼音,這個(gè)時(shí)候,拼音就能發(fā)揮很大的優(yōu)勢(shì)。

當(dāng)然轉(zhuǎn)換成拼音后,把每個(gè)音節(jié)當(dāng)一個(gè)詞,進(jìn)行深度學(xué)習(xí),效果也是非常好的。

 S1 = '加公眾號(hào):小優(yōu)惠,領(lǐng)券,便宜購(gòu)買'

S2 = '伽工仲號(hào):小優(yōu)惠,伶綣,便宜購(gòu)買'

#漢語(yǔ)相似

simi_1 = len(set(S1).intersection(set(S2)))/len(set(S1).union(set(S2)))#相似不懂的可以看我前面集合的文章

simi_1

0.5

#轉(zhuǎn)換成拼音后顯示

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

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }