随笔 - 4  文章 - 0  评论 - 0  阅读 - 79 

从清华的镜像去安装pypinyin

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pypinyin 
from pypinyin import pinyin, Style, lazy_pinyin

def convert_to_pinyin(text):
    return ' '.join([sub[0] for sub in pinyin(text, style=Style.TONE2)])


def convert_to_pinyin2(text):
    return ' '.join([sub[0] for sub in pinyin(text)])

def convert_to_lazy_pinyin(text):
    return ' '.join(lazy_pinyin(text))

text = '''
姓名
孙涵雅
张三
钱浩
唐燕姿
'''
converted_text = convert_to_pinyin(text)
print(converted_text)  # nǐ hǎo shì jì

converted_text2 = convert_to_pinyin2(text)
print(converted_text2)  # nǐ hǎo shì jì

converted_text = convert_to_lazy_pinyin(text)
print(converted_text )

输出结果

 xi4ng mi2ng 
 su1n ha2n ya3 
 zha1ng sa1n 
 qia2n ha4o 
 ta2ng ya4n zi1 


 xìng míng 
 sūn hán yǎ 
 zhāng sān 
 qián hào 
 táng yàn zī 


 xing ming 
 sun han ya 
 zhang san 
 qian hao 
 tang yan zi 
posted on   daniel_han2020  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
  1. 1 愉快轻松放松 愉快轻松放松
  2. 2 蜜雪冰城主题曲 蜜雪冰城
  3. 3 星茶会 灰澈
  4. 4 Give_it_Back_To_me 飞飞飞飞
  5. 5 Married_Life Hazel
Give_it_Back_To_me - 飞飞飞飞
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Loading

点击右上角即可分享
微信分享提示