加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

蟒蛇 – 用编号将拼音拼写为拼音

发布时间:2020-12-15 18:49:10 所属栏目:安全 来源:网络整理
导读:是否有使用 Python的脚本,库或程序,或BASH工具(例如awk,perl,sed),可以将编号的拼音(例如,dian4 nao3)正确地转换成带有音标(例如diànnǎo)的UTF-8拼音? 我发现了以下示例,但是它们需要PHP或#C: PHP Convert numbered to accentuated Pinyin? #C Any libra
是否有使用 Python的脚本,库或程序,或BASH工具(例如awk,perl,sed),可以将编号的拼音(例如,dian4 nao3)正确地转换成带有音标(例如diànnǎo)的UTF-8拼音?

我发现了以下示例,但是它们需要PHP或#C:

> PHP Convert numbered to accentuated Pinyin?
> #C Any libraries to convert number pinyin to pinyin with tone markings?

我还发现了各种在线工具,但是它们无法处理大量的转换.

我有一些Python 3代码,这样做很小,直接放在这里的答案中.
PinyinToneMark = {
    0: "aoeiuvu00fc",1: "u0101u014du0113u012bu016bu01d6u01d6",2: "u00e1u00f3u00e9u00edu00fau01d8u01d8",3: "u01ceu01d2u011bu01d0u01d4u01dau01da",4: "u00e0u00f2u00e8u00ecu00f9u01dcu01dc",}

def decode_pinyin(s):
    s = s.lower()
    r = ""
    t = ""
    for c in s:
        if c >= 'a' and c <= 'z':
            t += c
        elif c == ':':
            assert t[-1] == 'u'
            t = t[:-1] + "u00fc"
        else:
            if c >= '0' and c <= '5':
                tone = int(c) % 5
                if tone != 0:
                    m = re.search("[aoeiuvu00fc]+",t)
                    if m is None:
                        t += c
                    elif len(m.group(0)) == 1:
                        t = t[:m.start(0)] + PinyinToneMark[tone][PinyinToneMark[0].index(m.group(0))] + t[m.end(0):]
                    else:
                        if 'a' in t:
                            t = t.replace("a",PinyinToneMark[tone][0])
                        elif 'o' in t:
                            t = t.replace("o",PinyinToneMark[tone][1])
                        elif 'e' in t:
                            t = t.replace("e",PinyinToneMark[tone][2])
                        elif t.endswith("ui"):
                            t = t.replace("i",PinyinToneMark[tone][3])
                        elif t.endswith("iu"):
                            t = t.replace("u",PinyinToneMark[tone][4])
                        else:
                            t += "!"
            r += t
            t = ""
    r += t
    return r

这处理ü,u:和v,我遇到过的所有. Python 2兼容性将需要进行小的修改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读