python拼音庫pypinyin庫詳解

來源:互聯網
上載者:User

標籤:call   letter   fir   pytho   ras   none   去掉   替換   load   

# -*- coding: utf-8 -*-# @Author   : FELIX# @Date     : 2018/6/30 9:20from pypinyin import pinyin, lazy_pinyin, Style, load_phrases_dict, load_single_dictfrom pypinyin.style import registerprint(pinyin(‘你好‘))  # [[‘nǐ‘], [‘hǎo‘]]print(pinyin(‘中心‘, heteronym=True))  # 啟用多音字模式  # [[‘zhōng‘, ‘zhòng‘], [‘xīn‘]]print(pinyin(‘中心‘, style=Style.FIRST_LETTER))  # 設定拼音風格,第一個字母 [[‘z‘], [‘x‘]]print(pinyin(‘中心‘, style=Style.TONE2, heteronym=True))  # [[‘zho1ng‘, ‘zho4ng‘], [‘xi1n‘]]print(lazy_pinyin(‘中心‘))  # 不考慮多音字的情況 # [‘zhong‘, ‘xin‘]##########處理不包含拼音的字元# default (預設行為): 不做任何處理,原樣返回:print(lazy_pinyin(‘你好☆☆‘))  # [‘ni‘, ‘hao‘, ‘☆☆‘]# ignore : 忽略該字元print(lazy_pinyin(‘你好☆☆‘, errors=‘ignore‘))  # [‘ni‘, ‘hao‘]# replace : 替換為去掉 \u 的 unicode 編碼print(lazy_pinyin(‘你好☆☆‘, errors=‘replace‘))  # [‘ni‘, ‘hao‘, ‘26062606‘]# callable 對象 : 提供一個回呼函數,接受無拼音字元(串)作為參數, 支援的傳回值類型: unicode 或 list ([unicode, …]) 或 None 。print(lazy_pinyin(‘你好☆☆‘, errors=lambda x: ‘star‘))  # [‘ni‘, ‘hao‘, ‘star‘]########### 自訂拼音庫print(lazy_pinyin(‘還沒‘, style=Style.TONE2))load_phrases_dict({‘桔子‘: [[‘jú‘], [‘zǐ‘]]})  # 增加 "桔子" 片語,可以自己定義print(lazy_pinyin(‘桔子‘, style=Style.TONE2))load_single_dict({ord(‘還‘): ‘hái,huán‘})  # 調整 "還" 字的拼音順序print(lazy_pinyin(‘還沒‘, style=Style.TONE2))###########自訂拼音風格@register(‘kiss‘)def kiss(mypinyin, **kwargs):    return ‘?? {0}‘.format(mypinyin)print(lazy_pinyin(‘麼麼噠‘, style=‘kiss‘))

 

python拼音庫pypinyin庫詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.