将带数字的拼音转换为带声调标记的拼音

22
7个回答

24
我有一些 Python 3 的代码可以做到这一点,它足够小,可以直接放在答案中。
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,需要进行一些小的修改。


decode_pinyin 是一个函数。可以像这样调用它:decode_pinyin("ni3 hao3"),或者从文件中读取输入,或者使用其他你喜欢的方式。 - Greg Hewgill
如果您允许多个音节,就像您的“你好”示例一样,最好在输出中保留标点符号(至少是空格和撇号!)。 - John Machin
没错。为了支持这一点(虽然我的应用程序并不需要),进行修改应该很简单。 - Greg Hewgill
4
谢谢您!只是想提醒一下,对于 Python 2.x 版本,所需的更改很简单,只需要在任何包含 \u... 字符的字符串前面添加一个 u(表示Unicode),这为我解决了问题。 - Herman Schaaf
1
@GregHewgill 在这里你可以找到一个 Lua 实现的脚本:http://tex.stackexchange.com/a/125128/16071 - susis strolch
显示剩余3条评论

7
我编写了另一个Python函数,它是不区分大小写的,并保留空格、标点和其他文本(当然,除非有误报):
# -*- 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

cjklib库 可以满足您的需求:

您可以使用Python shell:

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

或者只使用命令行:

$ cjknife -m Bei3jing1
Běijīng

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

cjknife或其他函数能否轻松地将变音符号转换为同时包含汉字和英文的文件中?还是在Anki数据库(我相信这是SQLLite)中转换? - WGroleau
$ cjknife.py -m "ni3hao3吗?Hello" 将返回 "nǐhǎo吗?Hello"。然而,如果您输入的是"How are you?",它会抱怨缺少 "you" 这个带声调的音节,因为它是一个有效的拼音音节。所以我想您需要先将英语和拼音分开处理。 - cburgmer
1
当它抱怨缺少声调时,它是否仍然输出原始字符?许多人在中性音节上不使用数字。其他人使用5或0。它能处理吗?然后,还有一种不幸的做法是使用一个模糊的“u”来代替“ü”。 - WGroleau
SourceForge 把我引导到 cjklib.org 查看文档,但是那里出现了 500 错误。 - WGroleau

5

更新的代码: 请注意,@Lakedaemon的Kotlin代码没有考虑声调位置规则。

  • 字母a和e优先于其他元音字母,并且始终带有声调标记。汉语拼音中没有包含同时包含a和e的音节。
  • 在ou组合中,字母o带有标记。
  • 在所有其他情况下,最后一个元音字母带有标记。

我最初将@Lakedaemon的Kotlin代码移植到Java,现在我已经修改了代码并敦促使用此代码或@Lakedaemon的Kotlin代码的人更新代码。

我添加了一个额外的辅助函数来获取正确的声调标记位置。


    private static int getTonePosition(String r) {
        String lowerCase = r.toLowerCase();

        // exception to the rule
        if (lowerCase.equals("ou")) return 0;

        // higher precedence, both never go together
        int preferencePosition = lowerCase.indexOf('a');
        if (preferencePosition >= 0) return preferencePosition;
        preferencePosition = lowerCase.indexOf('e');
        if (preferencePosition >= 0) return preferencePosition;

        // otherwise the last one takes the tone mark
        return lowerCase.length() - 1;
    }

    static public String getCharacter(String string, int position) {
        char[] characters = string.toCharArray();
        return String.valueOf(characters[position]);
    }

    static public String toPinyin(String asciiPinyin) {
        Map<String, String> pinyinToneMarks = new HashMap<>();
        pinyinToneMarks.put("a", "āáǎà"); pinyinToneMarks.put("e", "ēéěè");
        pinyinToneMarks.put("i", "īíǐì"); pinyinToneMarks.put("o",  "ōóǒò");
        pinyinToneMarks.put("u", "ūúǔù"); pinyinToneMarks.put("ü", "ǖǘǚǜ");
        pinyinToneMarks.put("A",  "ĀÁǍÀ"); pinyinToneMarks.put("E", "ĒÉĚÈ");
        pinyinToneMarks.put("I", "ĪÍǏÌ"); pinyinToneMarks.put("O", "ŌÓǑÒ");
        pinyinToneMarks.put("U", "ŪÚǓÙ"); pinyinToneMarks.put("Ü",  "ǕǗǙǛ");

        Pattern pattern = Pattern.compile("([aeiouüvÜ]{1,3})(n?g?r?)([012345])");
        Matcher matcher = pattern.matcher(asciiPinyin);
        StringBuilder s = new StringBuilder();
        int start = 0;

        while (matcher.find(start)) {
            s.append(asciiPinyin, start, matcher.start(1));
            int tone = Integer.parseInt(matcher.group(3)) % 5;
            String r = matcher.group(1).replace("v", "ü").replace("V", "Ü");
            if (tone != 0) {
                int pos = getTonePosition(r);
                s.append(r, 0, pos).append(getCharacter(pinyinToneMarks.get(getCharacter(r, pos)),tone - 1)).append(r, pos + 1, r.length());
            } else {
                s.append(r);
            }
            s.append(matcher.group(2));
            start = matcher.end(3);
        }
        if (start != asciiPinyin.length()) {
            s.append(asciiPinyin, start, asciiPinyin.length());
        }
        return s.toString();
    }


你在getCharacter()方法中为什么不使用string.charAt(position) - Thomas
我不知道为什么我没有使用那个函数。我猜我错过了它,所以谢谢! - Ezequiel Santiago Sánchez

3
使用Python的DragonMapper库(pip install dragonmapper),可以将汉字转换为拼音。

汉字转拼音

from dragonmapper.transcriptions import hanzi

hanzi.to_pinyin("过河拆桥。")
# >>> 'guòhéchāiqiáo。'

hanzi.to_pinyin("过河拆桥。", accented=False)
# >>> 'guo4he2chai1qiao2。'

带声调的拼音转换为数字标记的拼音

from dragonmapper.transcriptions import accented_to_numbered

accented_to_numbered('guò hé chāi qiáo。')
# >>> 'guo4 he2 chai1 qiao2。'

数字拼音转换为带重音符号的拼音

from dragonmapper.transcriptions import numbered_to_accented

numbered_to_accented('guo4 he2 chai1 qiao2。')
# >>> 'guò hé chāi qiáo。'

免责声明:本人与dragonmapper作者无任何关系


1
我将dani_l的代码移植到Kotlin中(Java中的代码应该非常相似)。它的实现如下:
import java.util.regex.Pattern
val pinyinToneMarks = mapOf(
    'a' to "āáǎà",
    'e' to "ēéěè",
    'i' to "īíǐì",
    'o' to  "ōóǒò",
    'u' to "ūúǔù",
    'ü' to "ǖǘǚǜ",
    'A' to  "ĀÁǍÀ",
    'E' to "ĒÉĚÈ",
    'I' to "ĪÍǏÌ",
    'O' to "ŌÓǑÒ",
    'U' to "ŪÚǓÙ",
    'Ü' to  "ǕǗǙǛ"
)

fun toPinyin(asciiPinyin: String) :String {
  val pattern = Pattern.compile("([aeiouüvÜ]{1,3})(n?g?r?)([012345])")!!
  val matcher = pattern.matcher(asciiPinyin)
  val s = StringBuilder()
  var start = 0
  while (matcher.find(start)) {
      s.append(asciiPinyin, start, matcher.start(1))
      val tone = Integer.parseInt(matcher.group(3)!!) % 5
      val r = matcher.group(1)!!.replace("v", "ü").replace("V", "Ü")
      // for multple vowels, use first one if it is a/e/o, otherwise use second one
      val pos = if (r.length >1 && r[0].toString() !in "aeoAEO") 1 else 0
      if (tone != 0) s.append(r, 0, pos).append(pinyinToneMarks[r[pos]]!![tone - 1]).append(r, pos + 1, r.length)
      else s.append(r)
      s.append(matcher.group(2))
      start = matcher.end(3)
  }
  if (start != asciiPinyin.length) s.append(asciiPinyin, start, asciiPinyin.length)
  return s.toString()
}

fun test() = print(toPinyin("Ni3 hao3 ma0?"))

-1

我发现了一个在Microsoft Word中实现的VBA宏,网址是pinyinjoe.com

它有一个小缺陷,我向作者报告后他回应说他会尽快将我的建议纳入到宏中。那是在2014年1月初; 由于这个功能已经在我的副本中完成了,所以我没有再去验证这个宏。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接