首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将编号为拼音的拼音转换为带音符的拼音

问将编号为拼音的拼音转换为带音符的拼音
EN

Stack Overflow用户
提问于 2011-11-20 08:32:07
回答 6查看 3.9K关注 0票数 22

是否有任何使用Python或BASH工具(例如awk、perl、sed)的脚本、库或程序可以正确地将有编号的拼音(例如dian4 nao3)转换为带有音符的UTF-8拼音(例如,diàn​nǎo)?

我找到了以下示例,但它们需要PHP或C#:

我还找到了各种各样的在线工具,但它们无法处理大量的转换。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-11-20 08:39:44

我有一些Python 3代码,它很小,可以直接放在这里的答案中。

代码语言:javascript
复制
PinyinToneMark = {
    0: "aoeiuv\u00fc",
    1: "\u0101\u014d\u0113\u012b\u016b\u01d6\u01d6",
    2: "\u00e1\u00f3\u00e9\u00ed\u00fa\u01d8\u01d8",
    3: "\u01ce\u01d2\u011b\u01d0\u01d4\u01da\u01da",
    4: "\u00e0\u00f2\u00e8\u00ec\u00f9\u01dc\u01dc",
}

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("[aoeiuv\u00fc]+", 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的兼容性,需要做一些小的修改。

票数 24
EN

Stack Overflow用户

发布于 2012-05-20 21:03:44

cjklib库确实满足了您的需要:

要么使用Python:

代码语言:javascript
复制
>>> from cjklib.reading import ReadingFactory
>>> f = ReadingFactory()
>>> print f.convert('Bei3jing1', 'Pinyin', 'Pinyin', sourceOptions={'toneMarkType': 'numbers'})
Běijīng

或者仅仅是命令行:

代码语言:javascript
复制
$ cjknife -m Bei3jing1
Běijīng

免责声明:我开发了那个库。

票数 6
EN

Stack Overflow用户

发布于 2014-01-31 19:37:57

我编写了另一个Python函数,它不区分大小写,保留空格、标点符号和其他文本(当然,除非出现假阳性):

代码语言:javascript
复制
# -*- coding: utf-8 -*-
import re

pinyinToneMarks = {
    u'a': u'āáǎà', u'e': u'ēéěè', u'i': u'īíǐì',
    u'o': u'ōóǒò', u'u': u'ūúǔù', u'ü': u'ǖǘǚǜ',
    u'A': u'ĀÁǍÀ', u'E': u'ĒÉĚÈ', u'I': u'ĪÍǏÌ',
    u'O': u'ŌÓǑÒ', u'U': u'ŪÚǓÙ', u'Ü': u'ǕǗǙǛ'
}

def convertPinyinCallback(m):
    tone=int(m.group(3))%5
    r=m.group(1).replace(u'v', u'ü').replace(u'V', u'Ü')
    # for multple vowels, use first one if it is a/e/o, otherwise use second one
    pos=0
    if len(r)>1 and not r[0] in 'aeoAEO':
        pos=1
    if tone != 0:
        r=r[0:pos]+pinyinToneMarks[r[pos]][tone-1]+r[pos+1:]
    return r+m.group(2)

def convertPinyin(s):
    return re.sub(ur'([aeiouüvÜ]{1,3})(n?g?r?)([012345])', convertPinyinCallback, s, flags=re.IGNORECASE)

print convertPinyin(u'Ni3 hao3 ma0?')
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8200349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档