Take the 2-minute tour ×

For sorting Chinese language text, I want to convert Chinese characters to Pinyin, properly separating each Chinese character and grouping successive characters together.

Can you please help me in this task by providing the logic or source code for doing this?

Please let me know if any open source or lib already present for this.

share|improve this question

4 Answers 4

A table containing the mapping between unicode to pinyin


share|improve this answer

Short answer: you don't.

Long answer: There is no one-to-one mapping for 汉字 to 汉语拼音. Just some quick examples:

  • 把 can be "ba" in the third tone or fourth tone.
  • 了 can be "le" toneless or "liao" third tone.
  • 乐 can be "le" or "yue", both in the fourth tone.
  • 落 can be "luo", "la" or "lao", all in the fourth tone.

And so on. I have a beginners' book on this topic that has 207 examples. I stress that this is a beginners' book and is by no means complete. Each one has a page or two of examples of use and conditions under which you choose the appropriate pronunciation. It is not something that could be easily programmed (if at all).

And this doesn't even address the other slippery thing you want to deal with: the separation of characters into grouped words. The very notion of a word is a bit slippery in Chinese. (There's two terms that correspond, roughly to "word" in Chinese for example: 字 and 词. The first is the character, the second groups of characters that are put together into one concept. (I frequently get asked by Chinese speakers how many "words" I can read when they really mean "characters".) While in some cases the distinction is clear (the 词 "乌鸦", for example, is "crow" -- the two 字 must be together to express the idea properly and it would be incorrect to translate it as "black crow"), in others it is not so clear. What does "你好" translate to? Is it one word meaning, idiomatically, "hello"? Or is it two words translating literally to "you good"? Each of the characters involved stands alone or in groups with other words, but together they mean something entirely different from their individual meanings. Given this, how, precisely, do you plan to group the 汉语拼音 transliterations (which are difficult to impossible to get right in the first place!) into "words"?

share|improve this answer

the following code writing in C# can help you to simply convert chinese words that including in gb2312 encodec(just 2312 of often used Simplified-Chinese words) to pinyin.like convert "今天天气不错" to "JinTianTianQiBuCuo".

sometimes a chinese word is not one to one map to a pinyin,it depends on the context we talk about.like the "行" in "自行车"(bike) is pronounced "Xing",but in "银行"(bank) it pronounced "Hang".so if you have problem with this,you may find more complex solution to handle this.

sorry for my poor english.i hope this could give you a little help.

public class ChineseToPinYin
        private static int[] pyValue = new int[]
    -18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012,

    private static string[] pyName = new string[]
"Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan",

    /// <summary>
    /// 把汉字转换成拼音(全拼)
    /// </summary>
    /// <param name="hzString">汉字字符串</param>
    /// <returns>转换后的拼音(全拼)字符串</returns>
    public static string Convert(string hzString)
        // 匹配中文字符
        Regex regex = new Regex("^[\u4e00-\u9fa5]$");
        byte[] array = new byte[2];
        string pyString = "";
        int chrAsc = 0;
        int i1 = 0;
        int i2 = 0;
        char[] noWChar = hzString.ToCharArray();

        for (int j = 0; j < noWChar.Length; j++)
            // 中文字符
            if (regex.IsMatch(noWChar[j].ToString()))
                array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
                i1 = (short)(array[0]);
                i2 = (short)(array[1]);
                chrAsc = i1 * 256 + i2 - 65536;
                if (chrAsc > 0 && chrAsc < 160)
                    pyString += noWChar[j];
                    // 修正部分文字
                    if (chrAsc == -9254) // 修正“圳”字
                        pyString += "Zhen";
                        for (int i = (pyValue.Length - 1); i >= 0; i--)
                            if (pyValue[i] <= chrAsc)
                                pyString += pyName[i];
            // 非中文字符
                pyString += noWChar[j].ToString();
        return pyString;
share|improve this answer
Note only the first plane of GB2312 Chinese characters are arranged by Pinyin, which the simple code above works. It means the code covers only 3755 characters, for other 3008 characters in GB2312 and much more characters in unicode, it does not work (a dictation is required). –  Yu Jianrong Dec 30 '13 at 8:09

i had this problem and i found a solution in PHP (which could be cleaner i suppose but it works). I had some troubles because the file given in this topic is from hexa unicode.

1) Import the data from ftp://ftp.cuhk.hk/pub/chinese/ifcss/software/data/Uni2Pinyin.gz (thanks pierr) to your database or whatever

2) Import your data in an array as $pinyinArray[$hexaUnicode] = $pinyin;

3) Use this code:

 * Decimal representation of $c
 * function found there: http://www.cantonese.sheik.co.uk/phorum/read.php?2,19594
function uniord($c)
$ud = 0;
if (ord($c{0})>=0 && ord($c{0})<=127)
    $ud = $c{0};
if (ord($c{0})>=192 && ord($c{0})<=223)
    $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
if (ord($c{0})>=224 && ord($c{0})<=239)
    $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0})>=240 && ord($c{0})<=247)
    $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0})>=248 && ord($c{0})<=251)
    $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0})>=252 && ord($c{0})<=253)
    $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0})>=254 && ord($c{0})<=255) //error
    $ud = false;
return $ud;
 * Translate the $string string of a single chinese charactere to unicode
function chineseToHexaUnicode($string) {
    return strtoupper(dechex(uniord($string)));
function convertChineseToPinyin($string,$pinyinArray) {
    $pinyinValue = '';
    for ($i = 0; $i < mb_strlen($string);$i++)
        $pinyinValue.=$pinyinArray[chineseToHexaUnicode(mb_substr($string, $i, 1))];
    return $pinyinValue;

$string = '龙江省五大';
echo convertChineseToPinyin($string,$pinyinArray);

echo: (long2)(jiang1)(sheng3,xing3)(wu3)(da4,dai4)

Of course, $pinyinArray is your array of data (hexoUnicode => pinyin)

Hope it will help someone.

share|improve this answer

protected by Community Nov 19 '14 at 19:31


Would you like to answer one of these unanswered questions instead?
