<p>Python's <code>pypinyin</code> module does what you want.  Check it out at <a href="https://pypi.python.org/pypi/pypinyin" rel="nofollow">https://pypi.python.org/pypi/pypinyin</a>.</p>

<p>I highly recommend taking Python's <code>antigravity</code> module for a spin as well, it's a real trip.</p>

CC BY-SA 3.0
2015-08-06T05:30:20.770Z
Not bad, but fails on things like 愛好 (ai4 hao4 vs. ai4 hao3) and some other expressions.  >>> pinyin(u'愛好')
[['ài'], ['hǎo']].
imrek
CC BY-SA 3.0
2015-08-06T10:49:37.010Z
how can it possibly be expected to handle 多音字 correctly without being an actual dictionary?
Master Sparkles
CC BY-SA 3.0
2015-08-06T11:21:28.927Z
That's what I pointed out in my answer. You either use a dictionary file to look up multi-character words and expressions, or dump garbage to the user.
imrek
CC BY-SA 3.0
2015-08-06T11:23:38.347Z
I disagree that a strict minority of instances where the pinyin deviates from "standard" results in "garbage" being delivered to the user.
Master Sparkles
CC BY-SA 3.0
2015-08-06T15:28:59.307Z
I have run a test against the CEDICT file which is available at http://www.mdbg.net/chindict/chindict.php?page=cc-cedict. My findings are in my updated answer.
imrek
CC BY-SA 3.0
2015-08-06T19:43:50.200Z
Thats actually impressive work- you should forward it to the developers. Though a 10 percent error rate is not all that bad, and a flat comparison doesn't reflect the fact that some of the non-standard readings aren't going to occur very often in actual material being processed.
Master Sparkles
CC BY-SA 3.0
2015-08-05T22:48:09.393Z
2015-08-05T22:48:09.393Z
Master Sparkles
<p>For chrome there are a few extensions that can add pinyin to the current web page:</p>
<ol>
<li><strong><a href="https://web.archive.org/web/20220522173132/https://chrome.google.com/webstore/detail/add-pinyin-chinese-zero-t/nhhoiplgolfammoegggnjojoaljjbojg" rel="nofollow noreferrer">Add Pinyin</a></strong></li>
<li><strong><a href="https://chrome.google.com/webstore/detail/%E7%AE%80%E4%BD%93%E7%B9%81%E4%BD%93%E6%8B%BC%E9%9F%B3%E5%B9%BF%E4%B8%9C%E8%AF%9D%E8%BD%AC%E6%8D%A2-simplifiedtra/olpihmabpjpllgmahlgiakkgaccigpfo" rel="nofollow noreferrer">Simplified-Traditional-Converter</a></strong> (includes pinyin)  <a href="https://github.com/DoctorLai/Simplified-and-Traditional-Chinese" rel="nofollow noreferrer">github repo</a> (replaces the original text with pinyin rather than appending). A while ago I forked DoctorLai's repo and made it work in append mode rather than replace but <a href="https://github.com/glowinthedark/Simplified-and-Traditional-Chinese" rel="nofollow noreferrer">the fork</a> is more of an experiment rather than ready for use.</li>
</ol>

CC BY-SA 4.0
2020-07-25T16:27:56.843Z
2022-07-09T07:16:40.797Z
2022-07-09T07:16:40.797Z
ccpizza
<p>There are many tools, commercials, open sources, free tools or online services  can do this job.</p>

<p>I don't know what are you gonna do, so it's hard to pick one for you.</p>

<p>Here are some that ease to use:
online service
<a href="http://pth.linqi.org/pyzd_biaozhu.html" rel="nofollow">http://pth.linqi.org/pyzd_biaozhu.html</a></p>

<p>Chinese font with pinyin:
<a href="http://www.pinyinok.com/pyhzk.htm" rel="nofollow">http://www.pinyinok.com/pyhzk.htm</a></p>

<p>free tool:
<a href="http://www.putonghuaworld.com/computer/100604/100604_0101.htm" rel="nofollow">http://www.putonghuaworld.com/computer/100604/100604_0101.htm</a></p>

<p>pro open source tool list:
<a href="http://www.oschina.net/project/tag/446/pinyin" rel="nofollow">http://www.oschina.net/project/tag/446/pinyin</a></p>

<p>BTW: You can find this feature in the appropriate edition of Microsoft Word and Adobe InDesign.</p>

CC BY-SA 3.0
2015-08-04T12:27:50.150Z
That pinyin font looks dubious to me, since when is 中 zhòng (4th tone)? I doubt it works for characters with different readings.
imrek
CC BY-SA 3.0
2015-08-04T12:31:31.243Z
The first link fails on words like 用不着. (zhe vs. zháo)
imrek
CC BY-SA 3.0
2015-08-04T12:56:21.157Z
@DrunkenMaster  Should be 用不了(liao4). But it works perfect here. Maybe you should try a proxy or vpn. And the font, It has many symbols for one character depending on its sound. In the page, you can find the method how to input the different Pinyin symbols for one character.
sfy
CC BY-SA 3.0
2015-08-04T13:05:26.900Z
@DrunkenMaster 中计...
Mou某
CC BY-SA 3.0
2015-08-04T13:18:46.450Z
Check the image http://www.pinyinok.com/download/pyztxi.jpg it reads 中文字型范例, those are not just random characters, but a full expression: Chinese typeface sample. 中 in 中文字 is not zhòng.
imrek
CC BY-SA 3.0
2015-08-04T13:20:52.310Z
用不着 does not work, because the transcription logic flawed and that has nothing to do with proxy or VPN.
imrek
CC BY-SA 3.0
2015-08-05T12:12:52.900Z
Even if it's not 100% accurate it's a lot less work anyway... =P
Enrico Brasil
CC BY-SA 3.0
2015-08-04T11:04:50.240Z
2015-08-04T11:04:50.240Z
sfy
<p>I think it's needless to point out that you do realize that [hanzi --> pinyin] is not a straightforward function, because of the ambiguous relation of characters and pronunciation (多音字).</p>

<p>Character by character rendering is a very simple way, but will leave you with many false pinyin syllables and/or multiple options.<a href="https://www.branah.com/chinese-to-pinyin" rel="nofollow">This site</a> gives all the different pinyins of a character in parentheses, e.g.</p>

<pre><code>INPUT: 你好
OUTPUT: nǐ[hǎo hāo hào]
</code></pre>

<p>So any decent approach would require a <em>lookup in a  multi-character</em> word dictionary. If there is nothing online that comes close to this, you could download the latest <a href="http://www.mdbg.net/chindict/chindict.php?page=cc-cedict" rel="nofollow">CEDICT</a> file, remove all but the Traditional, Simplified and the Pinyin fields and use Python/awk, grep to write a script to bulk lookup Chinese words and output the results to a file or similar. Would not be that complex, although you would still need an 'algorithm' to identify <em>word boundaries</em>, otherwise just use 'brute force' to look up all the different matching entries in CEDICT. </p>

<p>What is the application context or purpose of what you would like to do?</p>

<p><em>(I am adding this as an answer because it's too long for a comment and also because of the URLs)</em></p>

<hr>

<p>UPDATE</p>

<p>Some users have recommended the <a href="https://pypinyin.readthedocs.org/en/master/" rel="nofollow">pypinyin</a> module which claims to support 多音字. I have run a test of <code>pypinyin</code> against the <a href="http://www.mdbg.net/chindict/chindict.php?page=cc-cedict" rel="nofollow">CEDICT</a> dictionary file, which is human compiled by many users and is the largest open source Chinese dictionary and is used by many apps (including <code>Pleco</code>). </p>

<p>Test results:
The <code>CEDICT</code> file contains over 113k entries (compiled over many years and continuously updated). For each entry in the <code>CEDICT</code> file I have compared the pinyin transcription against the output for the Simplified character expression of the pypinyin module. </p>

<p>I have accounted for:</p>

<ul>
<li><em>differences in pinyin transcription</em>, like 'ü' vs. 'v'</li>
<li>transcriptin of <em>儿</em> ('er' vs. 'r') in erhua expressions</li>
<li><em>upper- and lowercase</em> pinyin transcriptions</li>
<li>some obvious errors in the <code>CEDICT</code> file (missing pinyin) or whitespace and · (middle dot) and comma usage</li>
</ul>

<p>To be fair, I have also excluded all the <em>single character</em> <code>CEDICT</code> entries from the test, because it's impossible to give the correct <code>pinyin</code> for a 多音字 without any context.</p>

<p>In the first round, with excluding the single character entries, I run the test for 100.169 entries with 9471 mismatching pairs (<code>CEDICT</code> &lt;--> <code>pypinyin</code>).  </p>

<p>Next, I analysed the the results and realized that <code>pypinyin</code> has a somewhat contradictory policy on <em>tone sandhis</em>, it accounts for the change of 不 and 一 (e.g. in 不在) but not for 3rd tone + 3rd tone words, like 小姐. So I decided to exclude 不 and 一 to drill further deeper (and thus also excluded some cases where pypinyin was incorrect for some other reason than the tone sandhi notation). In the case of 不 and 一, marking tone change is a matter of opinion (but should be done consequently, not as in <code>pypinyin</code>).</p>

<p>In the 2nd round, I had 97.196 entries to test and got 8968 mismatching pairs.
After scrolling through the list of mismatching results, I came to the conclusion that <code>pypinyin</code> has <strong>no support for the neutral tone</strong> (5), e.g. 什么 (shén me) is *shén mé for <code>pypinyin</code>. Final 子, like in  位子, are given as 3rd tone. Similarly, 儿 is always incorrectly er2, etc.</p>

<p>The other most common error is <strong>incorrect tone</strong>, other than those neutral syllables. These include (among others):</p>

<ul>
<li>们 *men2</li>
<li>发 in 黑发 *hei1 fa1</li>
<li>禁伐 is incorrectly *jin1 fa2</li>
<li>近几年 *jin4 ji1 nian2, etc.</li>
</ul>

<p><strong>Incorrect initials</strong> make up another large group:</p>

<ul>
<li>调 tiao vs. diao</li>
<li>朝 chao vs. zhao </li>
<li>重 chong vs. zhong, </li>
<li>传 chuan vs. zhuan, etc.</li>
<li>弹琴 *dan qin</li>
<li>厦门 *sha men</li>
<li>社长 *she chang, etc.</li>
</ul>

<p>Also many elementary words are given with <strong>incorrect pinyin</strong>:</p>

<ul>
<li>音乐 is *yin le in <code>pypinyin</code> (my "favorite")</li>
<li>鱼露 is incorrectly *yu luo</li>
<li>卡 as *qia in almost all cases, like 卡拉OK * qia la OK</li>
<li>柏林  as *bai lin</li>
<li>落下 *la xia</li>
</ul>

<p>I have noticed in some cases that the <em>Taiwan Mandarin</em> pronunciation is favored by <code>pypinyin</code>, but the <a href="https://pypinyin.readthedocs.org/en/master/" rel="nofollow">doc pages</a> do not comment on this, which is obviously another issue, since users do have to know what to expect as output. </p>

<p>With all this, <code>pypinyin</code> has obvious shortcomings that make it unsuitable for professional use (you have to manually check every word to eliminate hard to spot errors) and also for building dictionaries or other material targeted at beginners. Even <code>HSK</code> level 2 or 3 expect most of the examples noted above to be used correctly. </p>

<hr>

<p>UPDATE2:</p>

<p>I am considering to create a Python lib that uses the CEDICT file to transcribe Chinese characters to pinyin. My only concern is that the CEDICT file also contains inaccurate pinyins, so first I would need a reliable source to update incorrect pinyins in CEDICT. If anyone knows about such a file or API, please leave a comment.  </p>

CC BY-SA 3.0
2015-08-04T13:39:46.807Z
Just guessing, he wants to write Chinese + Pinyin and does not want a lot of work. Many books are published with Pinyin and Chinese characters. How do they do it?
Peter Pan
CC BY-SA 3.0
2015-08-04T14:35:45.243Z
@PeterPan
You may be right, from the description it is not clear to me if the OP wants the pinyin transcription as the output of a 'dual' IME (outputting the Chinese characters and pinyin at the same time. I don't think that would work as an operating system wide IME, because some input fields are single-line fields, but there are code editors like Sublime Text which can run multiple cursos and input points in a document. But that still doesn't solve the key question, precise conversion logic that makes the most intelligent guess about the pinyin syllables.
imrek
CC BY-SA 3.0
2015-08-04T14:38:17.997Z
@PeterPan My guess was that the OP needs some sort of bulk converter that accepts passages of text and outputs the correct pinyin. There is no way to do that without a dictionary that maps multi character words to pinyins and just as important some grammar base logic, like guessing what role 着 (zhao1, zhao2, zhe5, zhuo2) plays in a sentence.
imrek
CC BY-SA 3.0
2015-08-08T21:20:42.230Z
right... you're going to need a lexical parser or it's still going to fail on things like 他们的爱好像春天一般的明媚与灿烂。
Master Sparkles
CC BY-SA 3.0
2017-03-14T04:23:15.533Z
Very thorough rundown :)
Stumpy Joe Pete
CC BY-SA 3.0
2015-08-04T07:21:39.533Z
2015-08-08T19:28:53.153Z
2015-08-08T19:28:53.153Z
imrek
<p>Is there any open software that adds a second line of pinyin to a line of characters?</p>
<p>i.e.: converting “你好” to:</p>
<blockquote>
<p>nǐhǎo</p>
<p>你好</p>
</blockquote>
<p>Converting to ni3hao3 would be fine to.</p>
<p>Open, namely, so that I can tinker with the pinyin to fit my needs.</p>

<software>
Open Software Two-Line Add Pinyin To Characters
# Open Software Two-Line Add Pinyin To Characters

(Asked by: username_0 on 2015-08-04)

Is there any open software that adds a second line of pinyin to a line of characters?

i.e.: converting “你好” to:

> nǐhǎo
>
> 你好

Converting to ni3hao3 would be fine to.

Open, namely, so that I can tinker with the pinyin to fit my needs.

**Tags:** software


---

## 4 answers

### Answer by username_2 on 2015-08-04. Score: 6 | Accepted answer

I think it's needless to point out that you do realize that [hanzi --> pinyin] is not a straightforward function, because of the ambiguous relation of characters and pronunciation (多音字).

Character by character rendering is a very simple way, but will leave you with many false pinyin syllables and/or multiple options.[This site](https://www.branah.com/chinese-to-pinyin) gives all the different pinyins of a character in parentheses, e.g.

```
INPUT: 你好
OUTPUT: nǐ[hǎo hāo hào]

```

So any decent approach would require a *lookup in a multi-character* word dictionary. If there is nothing online that comes close to this, you could download the latest [CEDICT](http://www.mdbg.net/chindict/chindict.php?page=cc-cedict) file, remove all but the Traditional, Simplified and the Pinyin fields and use Python/awk, grep to write a script to bulk lookup Chinese words and output the results to a file or similar. Would not be that complex, although you would still need an 'algorithm' to identify *word boundaries*, otherwise just use 'brute force' to look up all the different matching entries in CEDICT.

What is the application context or purpose of what you would like to do?

*(I am adding this as an answer because it's too long for a comment and also because of the URLs)*

---

UPDATE

Some users have recommended the [pypinyin](https://pypinyin.readthedocs.org/en/master/) module which claims to support 多音字. I have run a test of `pypinyin` against the [CEDICT](http://www.mdbg.net/chindict/chindict.php?page=cc-cedict) dictionary file, which is human compiled by many users and is the largest open source Chinese dictionary and is used by many apps (including `Pleco`).

Test results:
The `CEDICT` file contains over 113k entries (compiled over many years and continuously updated). For each entry in the `CEDICT` file I have compared the pinyin transcription against the output for the Simplified character expression of the pypinyin module.

I have accounted for:

* *differences in pinyin transcription*, like 'ü' vs. 'v'
* transcriptin of *儿* ('er' vs. 'r') in erhua expressions
* *upper- and lowercase* pinyin transcriptions
* some obvious errors in the `CEDICT` file (missing pinyin) or whitespace and · (middle dot) and comma usage

To be fair, I have also excluded all the *single character* `CEDICT` entries from the test, because it's impossible to give the correct `pinyin` for a 多音字 without any context.

In the first round, with excluding the single character entries, I run the test for 100.169 entries with 9471 mismatching pairs (`CEDICT` <--> `pypinyin`).

Next, I analysed the the results and realized that `pypinyin` has a somewhat contradictory policy on *tone sandhis*, it accounts for the change of 不 and 一 (e.g. in 不在) but not for 3rd tone + 3rd tone words, like 小姐. So I decided to exclude 不 and 一 to drill further deeper (and thus also excluded some cases where pypinyin was incorrect for some other reason than the tone sandhi notation). In the case of 不 and 一, marking tone change is a matter of opinion (but should be done consequently, not as in `pypinyin`).

In the 2nd round, I had 97.196 entries to test and got 8968 mismatching pairs.
After scrolling through the list of mismatching results, I came to the conclusion that `pypinyin` has **no support for the neutral tone** (5), e.g. 什么 (shén me) is \*shén mé for `pypinyin`. Final 子, like in 位子, are given as 3rd tone. Similarly, 儿 is always incorrectly er2, etc.

The other most common error is **incorrect tone**, other than those neutral syllables. These include (among others):

* 们 \*men2
* 发 in 黑发 \*hei1 fa1
* 禁伐 is incorrectly \*jin1 fa2
* 近几年 \*jin4 ji1 nian2, etc.

**Incorrect initials** make up another large group:

* 调 tiao vs. diao
* 朝 chao vs. zhao
* 重 chong vs. zhong,
* 传 chuan vs. zhuan, etc.
* 弹琴 \*dan qin
* 厦门 \*sha men
* 社长 \*she chang, etc.

Also many elementary words are given with **incorrect pinyin**:

* 音乐 is \*yin le in `pypinyin` (my "favorite")
* 鱼露 is incorrectly \*yu luo
* 卡 as \*qia in almost all cases, like 卡拉OK \* qia la OK
* 柏林 as \*bai lin
* 落下 \*la xia

I have noticed in some cases that the *Taiwan Mandarin* pronunciation is favored by `pypinyin`, but the [doc pages](https://pypinyin.readthedocs.org/en/master/) do not comment on this, which is obviously another issue, since users do have to know what to expect as output.

With all this, `pypinyin` has obvious shortcomings that make it unsuitable for professional use (you have to manually check every word to eliminate hard to spot errors) and also for building dictionaries or other material targeted at beginners. Even `HSK` level 2 or 3 expect most of the examples noted above to be used correctly.

---

UPDATE2:

I am considering to create a Python lib that uses the CEDICT file to transcribe Chinese characters to pinyin. My only concern is that the CEDICT file also contains inaccurate pinyins, so first I would need a reliable source to update incorrect pinyins in CEDICT. If anyone knows about such a file or API, please leave a comment.

> **Comments:**
> **username_6**: Just guessing, he wants to write Chinese + Pinyin and does not want a lot of work. Many books are published with Pinyin and Chinese characters. How do they do it?  
> **username_2**: @username_6 You may be right, from the description it is not clear to me if the OP wants the pinyin transcription as the output of a 'dual' IME (outputting the Chinese characters and pinyin at the same time. I don't think that would work as an operating system wide IME, because some input fields are single-line fields, but there are code editors like Sublime Text which can run multiple cursos and input points in a document. But that still doesn't solve the key question, precise conversion logic that makes the most intelligent guess about the pinyin syllables.  
> **username_2**: @username_6 My guess was that the OP needs some sort of bulk converter that accepts passages of text and outputs the correct pinyin. There is no way to do that without a dictionary that maps multi character words to pinyins and just as important some grammar base logic, like guessing what role 着 (zhao1, zhao2, zhe5, zhuo2) plays in a sentence.  
> **username_1**: right... you're going to need a lexical parser or it's still going to fail on things like 他们的爱好像春天一般的明媚与灿烂。  
> **username_7**: Very thorough rundown :)  

---

### Answer by username_1 on 2015-08-05. Score: 2

Python's `pypinyin` module does what you want. Check it out at <https://pypi.python.org/pypi/pypinyin>.

I highly recommend taking Python's `antigravity` module for a spin as well, it's a real trip.

> **Comments:**
> **username_2**: Not bad, but fails on things like 愛好 (ai4 hao4 vs. ai4 hao3) and some other expressions.  >>> pinyin(u'愛好') [['ài'], ['hǎo']].  
> **username_1**: how can it possibly be expected to handle 多音字 correctly without being an actual dictionary?  
> **username_2**: That's what I pointed out in my answer. You either use a dictionary file to look up multi-character words and expressions, or dump garbage to the user.  
> **username_1**: I disagree that a strict minority of instances where the pinyin deviates from "standard" results in "garbage" being delivered to the user.  
> **username_2**: I have run a test against the CEDICT file which is available at http://www.mdbg.net/chindict/chindict.php?page=cc-cedict. My findings are in my updated answer.  
> **username_1**: Thats actually impressive work- you should forward it to the developers. Though a 10 percent error rate is not all that bad, and a flat comparison doesn't reflect the fact that some of the non-standard readings aren't going to occur very often in actual material being processed.  

---

### Answer by username_3 on 2020-07-25. Score: 1

For chrome there are a few extensions that can add pinyin to the current web page:

1. **[Add Pinyin](https://web.archive.org/web/20220522173132/https://chrome.google.com/webstore/detail/add-pinyin-chinese-zero-t/nhhoiplgolfammoegggnjojoaljjbojg)**
2. **[Simplified-Traditional-Converter](https://chrome.google.com/webstore/detail/%E7%AE%80%E4%BD%93%E7%B9%81%E4%BD%93%E6%8B%BC%E9%9F%B3%E5%B9%BF%E4%B8%9C%E8%AF%9D%E8%BD%AC%E6%8D%A2-simplifiedtra/olpihmabpjpllgmahlgiakkgaccigpfo)** (includes pinyin) [github repo](https://github.com/DoctorLai/Simplified-and-Traditional-Chinese) (replaces the original text with pinyin rather than appending). A while ago I forked DoctorLai's repo and made it work in append mode rather than replace but [the fork](https://github.com/glowinthedark/Simplified-and-Traditional-Chinese) is more of an experiment rather than ready for use.


---

### Answer by username_4 on 2015-08-04. Score: 0

There are many tools, commercials, open sources, free tools or online services can do this job.

I don't know what are you gonna do, so it's hard to pick one for you.

Here are some that ease to use:
online service
<http://pth.linqi.org/pyzd_biaozhu.html>

Chinese font with pinyin:
<http://www.pinyinok.com/pyhzk.htm>

free tool:
<http://www.putonghuaworld.com/computer/100604/100604_0101.htm>

pro open source tool list:
<http://www.oschina.net/project/tag/446/pinyin>

BTW: You can find this feature in the appropriate edition of Microsoft Word and Adobe InDesign.

> **Comments:**
> **username_2**: That pinyin font looks dubious to me, since when is 中 zhòng (4th tone)? I doubt it works for characters with different readings.  
> **username_2**: The first link fails on words like 用不着. (zhe vs. zháo)  
> **username_4**: @DrunkenMaster  Should be 用不了(liao4). But it works perfect here. Maybe you should try a proxy or vpn. And the font, It has many symbols for one character depending on its sound. In the page, you can find the method how to input the different Pinyin symbols for one character.  
> **username_0**: @DrunkenMaster 中计...  
> **username_2**: Check the image http://www.pinyinok.com/download/pyztxi.jpg it reads 中文字型范例, those are not just random characters, but a full expression: Chinese typeface sample. 中 in 中文字 is not zhòng.  
> **username_2**: 用不着 does not work, because the transcription logic flawed and that has nothing to do with proxy or VPN.  
> **username_5**: Even if it's not 100% accurate it's a lot less work anyway... =P  

---

