發布時間:2024-01-22閱讀(7)

最近做一個項目,發現很多場景,把漢字轉換成拼音,然后進行深度學習分類,能夠取得非常不錯的效果,在做內容識別,特別是涉及到同音字的時候,轉換成拼音就顯得特別重要。比如垃圾廣告識別:公眾號、工仲號、躬總號,公眾號、微信、威信、維伈.........,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, Styleimport 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, Styleimport pypinyinlazy_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 pypinyinfrom pypinyin import Stylepypinyin.slug(我是中國人)wo-shi-zhong-guo-renpypinyin.slug(我是中國人, separator= )wo shi zhong guo renpypinyin.slug(中國人2020雄起, separator= )#遇到數字等非漢字不注音zhong guo ren 2020 xiong qipypinyin.slug(中國人2020雄起, style=Style.FIRST_LETTER)z-g-r-2020-x-qpypinyin.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_10.5#轉換成拼音后顯示S1 = lazy_pinyin(S1)S2 = lazy_pinyin(S2)simi_2 = len(set(S1).intersection(set(S2)))/len(set(S1).union(set(S2)))simi_20.875
Python學習交流群
為了讓大家更加即時地溝通學習,我們建了一個Python學習交流群,有想入群的同學,可以添加下面小助手微信,他會拉大家入群哈~
歡迎分享轉載→http://m.avcorse.com/read-76433.html
Copyright ? 2024 有趣生活 All Rights Reserve吉ICP備19000289號-5 TXT地圖HTML地圖XML地圖