#-*-coding:utf-8-*-#@Author: FELIX#@Date: 2018/6/30 9:20 fromPypinyinImportPinyin, Lazy_pinyin, Style, load_phrases_dict, Load_single_dict fromPypinyin.styleImportRegisterPrint(Pinyin ('Hello'))#[ [' nǐ '], [' Hǎo ' ]Print(Pinyin ('Center', heteronym=true))#enable Polyphone mode # [[' Zhōng ', ' Zhòng '], [' Xīn ' ]Print(Pinyin ('Center', Style=style.first_letter))#set phonetic style, first letter [[' Z '], [' X ']]Print(Pinyin ('Center', Style=style.tone2, Heteronym=true))#[ [' zho1ng ', ' zho4ng '], [' xi1n ' ]Print(Lazy_pinyin ('Center'))#do not consider the case of Polyphone # [' Zhong ', ' Xin ']######### #处理不包含拼音的字符#default behavior: Returns without any processing:Print(Lazy_pinyin ('Hello ☆'))#[' ni ', ' Hao ', ' ☆ ★ ']#Ignore: ignore this characterPrint(Lazy_pinyin ('Hello ☆', errors='Ignore'))#[' ni ', ' Hao ']#Replace : Replaced with Unicode encoding to remove \uPrint(Lazy_pinyin ('Hello ☆', errors='Replace'))#[' ni ', ' Hao ', ' 26062606 ']#Callable object: Provides a callback function that accepts no phonetic characters (strings) as arguments, supported return value types: Unicode or list ([Unicode, ...]) or None. Print(Lazy_pinyin ('Hello ☆', errors=LambdaX:'Star'))#[' ni ', ' Hao ', ' star ']########### Custom Phonetic LibrariesPrint(Lazy_pinyin ('not yet .', style=style.tone2)) Load_phrases_dict ({'Orange': [['Jú'], ['zǐ']]})#add an "orange" phrase that you can define yourselfPrint(Lazy_pinyin ('Orange', style=style.tone2)) Load_single_dict ({ord ('still'):'Hái,huán'})#Adjust the phonetic order of the "also" wordPrint(Lazy_pinyin ('not yet .', style=style.tone2))########## #自定义拼音风格@register ('Kiss')defKiss (Mypinyin, * *Kwargs):return '?? {0}'. Format (mypinyin)Print(Lazy_pinyin ('What, da ?', style='Kiss'))
Python Pinyin Library Pypinyin Library detailed