导购网站开发源码,wordpress 光点特效,卡曼科技网站建设,WordPress添加用户投稿一、汉字转拼音
在python中将汉字的拼音输出可以采用pypinyin包#xff0c;一下是简单的demo示例#xff1a;
默认调用pinyin方法转换时时默认时带声调的#xff0c;不带声调需要添加“styleStyle.NORMAL”参数。
from pypinyin import pinyin, Styledef pinyin_transfer…一、汉字转拼音
在python中将汉字的拼音输出可以采用pypinyin包一下是简单的demo示例
默认调用pinyin方法转换时时默认时带声调的不带声调需要添加“styleStyle.NORMAL”参数。
from pypinyin import pinyin, Styledef pinyin_transfer(word):pinyins []# 带声调的(stylepypinyin.NORMAL)for w in pinyin(word):pinyins.extend(w)print( .join(pinyins))pinyins []# 不带声调的(stylepypinyin.NORMAL)for w in pinyin(word, styleStyle.NORMAL):pinyins.extend(w)print( .join(pinyins))if __name__ __main__:pinyin_transfer(曾经沧海难为水,除却巫山不是云)
输出结果 céng jīng cāng hǎi nán wéi shuǐ , chú què wū shān bú shì yún ceng jing cang hai nan wei shui , chu que wu shan bu shi yun 二、多音字处理
输出多音字则需要添加参数“heteronymTrue”
def transfer_with_yinjie(word):# heteronymTrue开启多音字pinyins []for w in pinyin(word, heteronymTrue):pinyins.extend(w)print( .join(pinyins))if __name__ __main__:# pinyin_transfer(曾经沧海难为水,除却巫山不是云)transfer_with_yinjie(曾经沧海难为水,除却巫山不是云)
输出结果 céng jīng cāng hǎi nán wéi shuǐ , chú zhù shū què wū shān bú shì yún 除chú zhù shū 则为多音字
三、多音字输出准确读音
结合包jieba进行拆词处理
import jieba
from pypinyin import pinyin, Styledef pinyin_with_jieba(word):words jieba.cut(word)pinyins []for w in words:print(w)pw pinyin(w)pinyins.extend([i[0] for i in pw])return .join(pinyins)# 示例
print(pinyin_with_jieba(曾经沧海难为水, 除却巫山不是云))
输出结果 曾经沧海 难为 水 , 除却 巫山 不是 云 céng jīng cāng hǎi nán wéi shuǐ , chú què wū shān bú shì yún