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

熱線電話:13121318867

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

我們都知道python是一中功能強(qiáng)大,易上手的計算機(jī)編程語言,應(yīng)用范圍很是廣泛。我們平時可以使用python進(jìn)行數(shù)據(jù)統(tǒng)計,報表制作等,有時候也會遇到內(nèi)容識別的場景,需要將漢字轉(zhuǎn)換成拼音。今天小編跟大家分享的這篇文章就是教大家利用python將漢字轉(zhuǎn)化成拼音的,希望對大家學(xué)習(xí)和使用python有所幫助。


以下文章來源: AI入門學(xué)習(xí)

作者:小伍哥


一、應(yīng)用概述

最近做一個項目,發(fā)現(xiàn)很多場景,把漢字轉(zhuǎn)換成拼音,然后進(jìn)行深度學(xué)習(xí)分類,能夠取得非常不錯的效果,在做內(nèi)容識別,特別是涉及到同音字的時候,轉(zhuǎn)換成拼音就顯得特別重要。比如垃圾廣告識別:公眾號、工仲號、躬總號,公眾號、微信、威信、維伈.........,pypinyin是我用的一個比較好用的包是

給大家分享下,當(dā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

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

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

參數(shù):

  • hans (unicode 字符串或字符串列表) – 漢字字符串( '你好嗎' )或列表( ['你好', '嗎'] ). 可以使用自己喜愛的分詞模塊對字符串進(jìn)行分詞處理, 只需將經(jīng)過分詞處理的字符串列表傳進(jìn)來就可以了。
  • style – 指定拼音風(fēng)格,默認(rèn)是 TONE 風(fēng)格。更多拼音風(fēng)格詳見 Style
  • errors –指定如何處理沒有拼音的字符。詳見 處理不包含拼音的字符
  • heteronym – 是否啟用多音字
  • strict – 是否嚴(yán)格遵照《漢語拼音方案》來處理聲母和韻母,詳見 strict 參數(shù)的影響
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']]
# 設(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)#漢語拼音與俄語字母對照風(fēng)格
[['чжун1'], ['синь1']]

2、pypinyin.lazy_pinyin

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

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

參數(shù):

  • hans (unicode or list) – 漢字
  • style – 指定拼音風(fēng)格,默認(rèn)是 NORMAL 風(fēng)格。更多拼音風(fēng)格詳見 Style。
  • errors – 指定如何處理沒有拼音的字符,詳情請參考 pinyin()
  • strict – 是否嚴(yán)格遵照《漢語拼音方案》來處理聲母和韻母,詳見 strict 參數(shù)的影響
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']

3、pypinyin.slug

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

語法: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)格詳見 Style
  • heteronym – 是否啟用多音字
  • separator – 兩個拼音間的分隔符/連接符
  • errors – 指定如何處理沒有拼音的字符,詳情請參考 pinyin()
  • strict – 是否嚴(yán)格遵照《漢語拼音方案》來處理聲母和韻母,詳見 strict 參數(shù)的影響
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=' ')#遇到數(shù)字等非漢字不注音
'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-жэнь'

4、 pypinyin.load_single_dict

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

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

參數(shù):

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

 

 5、 pypinyin.load_phrases_dict

功能:載入用戶自定義的詞語拼音庫

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

參數(shù):

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

 

五、一個案例

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

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

 

S1 = '加公眾號:小優(yōu)惠,領(lǐng)券,便宜購買'
S2 = '伽工仲號:小優(yōu)惠,伶綣,便宜購買'

#漢語相似
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ù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號: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)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".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 = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+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); }