7

超简单 Python 汉字拼音转换工具

 1 year ago
source link: https://www.51cto.com/article/719536.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

超简单 Python 汉字拼音转换工具

作者:Ckend 2022-09-24 16:53:05

将汉字转为拼音,可以用于批量汉字注音、文字排序、拼音检索文字等常见场景。

将汉字转为拼音,可以用于批量汉字注音、文字排序、拼音检索文字等常见场景。

56d018740d7a3ead44f335db4d6fea902af84e.jpg

现在互联网上有许多拼音转换工具,基于Python的开源模块也不少,今天给大家介绍一个功能特性最多的模块:  pypinyin ,它支持以下特性:

  1. 根据词组智能匹配最正确的拼音。
  2. 支持多音字。
  3. 简单的繁体支持, 注音支持。
  4. 支持多种不同拼音/注音风格。
  5. 命令行工具一键转化

开始之前,你要确保Python和pip已经成功安装在电脑上,如果没有,可以访问这篇文章:超详细Python安装指南 进行安装。

(可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda:Python数据分析与挖掘好帮手—Anaconda,它内置了Python和pip.

(可选2) 此外,推荐大家用VSCode编辑器,它有许多的优点:Python 编程的最好搭档—VSCode 详细指南。

请选择以下任一种方式输入命令安装依赖:

  1. Windows 环境 打开 Cmd (开始-运行-CMD)。
  2. MacOS 环境 打开 Terminal (command+空格输入Terminal)。
  3. 如果你用的是 VSCode编辑器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install pypinyin

2.基本使用

最普通的拼音转化方法如下:

pypinyin  pinyin, lazy_pinyin, Style
pinyin()

识别多音字:

pypinyin  pinyin, lazy_pinyin, Style
pinyin(, heteronym=)

设置输出风格,只识别首字母:

pypinyin  pinyin, lazy_pinyin, Style
pinyin(, style=Style.FIRST_LETTER)

修改音调输出位置,在相应字母的后面显示音调,或者拼音的最后显示音调:

pypinyin  pinyin, lazy_pinyin, Style


pinyin(, style=Style.TONE2, heteronym=)



pinyin(, style=Style.TONE3, heteronym=)

不考虑多音字的情况:

pypinyin  pinyin, lazy_pinyin, Style
lazy_pinyin()

不使用v来代替ü:

pypinyin  pinyin, lazy_pinyin, Style
lazy_pinyin(, v_to_u=)

标记轻声:

pypinyin  pinyin, lazy_pinyin, Style

lazy_pinyin(, style=Style.TONE3, neutral_tone_with_five=)

使用命令行一键识别拼音:

python -m pypinyin 音乐

3.高级使用

自定义拼音显示风格

我们可以通过 register() 来实现自定义拼音风格的需求:

pypinyin  lazy_pinyin

 pypinyin.style  register


 :
     .format(pinyin)

  
lazy_pinyin(, style=)

可以见到,通过定义一个 kiss 函数,使用 register 装饰器,我们生成了一个新的 style,这个 style 可以直接被用于拼音的转换参数,非常方便。

另外,所有模块自带的 style 及其效果如下:

:
    

    
    NORMAL = 
    
    TONE = 
    
    TONE2 = 
    
    TONE3 = 
    
    INITIALS = 
    
    FIRST_LETTER = 
    
    FINALS = 
    
    FINALS_TONE = 
    
    FINALS_TONE2 = 
    
    FINALS_TONE3 = 
    
    BOPOMOFO = 
    
    BOPOMOFO_FIRST = 
    
    CYRILLIC = 
    
    CYRILLIC_FIRST =

处理特殊字符

默认情况下,对于文字中的特殊字符会不做任何处理,原样返回:

pinyin()

不过如果你想对这些特殊字符做处理也是可以的,比如:

ignore  : 忽略该字符

pinyin(, errors=)

errors​ : 替换为去掉  \u  的 unicode 编码:

pinyin(, errors=)

callable 对象​  : 提供一个回调函数,接受无拼音字符(串)作为参数, 支持的返回值类型:  unicode​或 list​或 None:

pinyin('你好☆☆', errors=lambda x: 'star')
# [['nǐ'], ['hǎo'], ['star']]

pinyin('你好☆☆', errors=lambda x: None)
# [['nǐ'], ['hǎo']]

返回值类型为 list时,会自动 expend list:

pinyin('你好☆☆', errors=lambda x: ['star' for _ in x])
# [['nǐ'], ['hǎo'], ['star'], ['star']]

# 指定多音字
pinyin('你好☆☆', heteronym=True, errors=lambda x: [['star', '☆'] for _ in x])
# [['nǐ'], ['hǎo'], ['star', '☆'], ['star', '☆']]

自定义拼音库

如果你觉得模块输出效果不合你意,或者你想做特殊处理,可以通过  load_single_dict()​ 或  load_phrases_dict()  以自定义拼音库的方式修正结果:

from pypinyin import lazy_pinyin, load_phrases_dict, Style, load_single_dict
hans = '桔子'
lazy_pinyin(hans, style=Style.TONE2)
# ['jie2', 'zi3']
load_phrases_dict({'桔子': [['jú'], ['zǐ']]}) # 增加 "桔子" 词组
lazy_pinyin(hans, style=Style.TONE2)
# ['ju2', 'zi3']

hans = '还没'
lazy_pinyin(hans, style=Style.TONE2)
# ['hua2n', 'me2i']
load_single_dict({ord('还'): 'hái,huán'}) # 调整 "还" 字的拼音顺序
lazy_pinyin('还没', style=Style.TONE2)
# ['ha2i', 'me2i']

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK