Python pypinyin module 的应用

  • 安装pypinyin module
    • 这个模块主要处理中文拼音
    • 可以用来做很多很有趣的事情

如何使用help 函数

将help 函数的输出保存到文件中去

  • 一眼会
import sys
import pypinyin

with open('帮助文档.txt','w') as f:
    sys.stdout = f 
    help(pypinyin) 

如何查找模块中内置的函数

  • 函数 

  • lazy_pinyin(hans, style=<Style.NORMAL: 0>, errors='default', strict=True, v_to_u=False, neutral_tone_with_five=False, tone_sandhi=False)
    • 将汉字转换为拼音,返回不包含多音字结果的拼音列表
  • load_phrases_dict(phrases_dict, style='default')
    • 载入用户自定义的词语拼音库
  • load_single_dict(pinyin_dict, style='default')
    • 载入用户自定义的单字拼音库
  • pinyin(hans, style=<Style.TONE: 1>, heteronym=False, errors='default', strict=True, v_to_u=False, neutral_tone_with_five=False)
    • 将汉字转换为拼音,返回汉字的拼音列表
  • slug(hans, style=<Style.NORMAL: 0>, heteronym=False, separator='-', errors='default', strict=True)
    • 将汉字转换为拼音,然后生成 slug 字符串

pypinyin module

style example

from pypinyin import pinyin

for i in range(15):
    print("i=", i)
    print("".join(
            [i[0] for i in pinyin("啊什么,你说计算机?", style=i, heteronym=False)]
                 )
          )

i= 0
ashenme,nishuojisuanji?
i= 1
ashénme,nǐshuōjìsuànjī?
i= 2
ashe2nme,ni3shuo1ji4sua4nji1?
i= 3
shm,nshjsj?
i= 4
asm,nsjsj?
i= 5
aene,iuoiuani?
i= 6
aéne,ǐuōìuànī?
i= 7
ae2ne,i3uo1i4ua4ni1?
i= 8
ashen2me,ni3shuo1ji4suan4ji1?
i= 9
aen2e,i3uo1i4uan4i1?
i= 10
ㄚ˙ㄕㄣˊㄇㄜ˙,ㄋㄧˇㄕㄨㄛㄐㄧˋㄙㄨㄢˋㄐㄧ?
i= 11
ㄚㄕㄇ,ㄋㄕㄐㄙㄐ?
i= 12
ашэнь2мэ,ни3шуо1цзи4суань4цзи1?
i= 13
ашм,ншцсц?
i= 14
ashenme,nishochisuanchi?

  • 拼音风格问题
  • 多音字问题

有趣的应用:寻找同音字

完整代码

from pypinyin import pinyin


def get_jokesrt(jokestr):
    return "".join([i[0] for i in pinyin(jokestr, style=1, heteronym=False)])

def find_jokes(name, style=1, path="sentences.txt", heteronym=False, COUNT=10):
    jokestr = get_jokesrt(name)
    count = 0
    with open(path, "r", encoding='utf-8') as file:
        for line in file:
            linestr = line.strip()
            if jokestr in get_jokesrt(linestr):
                print(linestr)
                print("##")
                count += 1
            if count >= COUNT:
                break



find_jokes("睿雯")

  •  问题
    • 没有实现字与音的一一对应

改进后的代码

from pypinyin import pinyin
import pypinyin 

def get_jokestr(jokestr):
    return pypinyin.slug(jokestr, style=1, separator="-") + "-"

def printf(name, linestr, separator="-"):
    print(linestr)
    jokestr = get_jokestr(name)
    textstr = get_jokestr(linestr)
    index1 = textstr.find(jokestr)
    index2 = textstr[:index1].count(separator)

    newstr =  linestr[:index2]
    newstr += "¥" * len(name)
    newstr += linestr[len(name) + index2:]
    print(newstr)
    return [linestr, newstr]

def find_jokes(name, style=1, path="file.txt", heteronym=False, COUNT=3):
    jokestr = get_jokestr(name)
    count = 0
    with open(path, "r", encoding='gbk') as file:
        for line in file:
            linestr = line.strip()
            if jokestr in get_jokestr(linestr):
                printf(name, linestr)
                print("##")
                count += 1
            if count >= COUNT:
                break

test_str = "睿雯"
find_jokes(test_str)

常用到的几个内置函数

  • strip()
    • 用于去除字符串两端的指定字符(默认为空格)的函数
  • find()
  • count()
    • 统计字符串中指定子串出现的次数
  • lower()、upper()
    • 将字符串转换为小写和大写
  • islower()、isupper()
    • 判断字符串是否全为小写或大写
  • capitalize()
    • 将字符串的首字母转换为大写,其他字母转换为小写
  • title()
    • 将字符串中每个单词的首字母转换为大写
  • isalpha()、isdigit()、isalnum()
    • 判断字符串是否全为字母、数字、字母和数字的组合
  • startswith(substring)、endswith(substring)
    • 判断字符串是否以指定的子串开始或结束
  • replace(old, new)
    • 将字符串中的指定子串替换为新的子串
  • split(separator)
    • 将字符串按照指定的分隔符分割成列表
  • index(substring)
    • 在字符串中查找指定子串,返回第一次出现的索引
  • join(iterable)
    • 将可迭代对象中的元素连接成一个字符串。
  • isdigit()
    • 判断字符串是否全为数字字符。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

River Chandler

谢谢,我会更努力学习工作的!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值