I was wondering if anyone here is aware of an API out there on the net anywhere that can receive a traditional character and send back the pinyin for it in JSON?
There are several libraries for converting Chinese characters to pinyin. If you are comfortable with programming languages (which you probably are if you are asking for an API), your choice will depend on the programming languages you know, the quality of the library and the licence under which it is available. If you need a user interface, you should probably first look at pinyin4j and ZDT.
- pinyin4net: C# library that supports both simplified and traditional Chinese characters. Output is hanyu pinyin. Licence: MIT.
- PinYin4Objc: Objective-C library (with some Ruby) that supports both simplified and traditional Chinese characters and most popular pinyin systems. Licence: Apache License 2.0.
- HanziPinyin: Swift library that supports both simplified and traditional Chinese characters. Licence: MIT License.
- PiPinyin: Java library for Android for converting Chinese characters to pinyin. I assume it supports simplified characters but could not find out if it also supports traditional characters. (The older pinyin4android library, on which PiPinyin is based, appears to have no documentation.) Licence: Apache License 2.0.
- pinyin4cpp: C++ library. Licence: Apache License 2.0. (Possibly inspired by pinyin4j, below.)
- pinyin4j: Java library for conversion "between Chinese characters and most popular Pinyin systems". Can also ouput Wade-Giles, Yale and Gwoyeu Romatzyh! Licence: GPL v2.
- Unihanconver: a Perl library "to convert between Traditional/Simplified Chinese directly in Unicode (not GB/Big5 conversion)". It can be used through a Gtk2+ Interface and from the console. (See also the developer site.) Licence: GPL 2.
- ZDT (Zhongwen Development Tool), written in Java, "is an easy to use, open-source Mandarin Chinese flashcard application that can help you learn Chinese". One of its features is conversion between Chinese charactes and pinyin. (Developer site.) Licence: Eclipse Public License.
- zh2tone / Chinese Word to ZuYin Code: a tool to convert Chinese characters/words to zhuyin fuhao (bopomofo). The tool has not been updated since 2012, has no licence, ...
- I have not checked the quality of these libraries (alpha/beta/production-ready, test coverage, etc.) nor their completeness (how many characters they support etc.).
- If you need such a library for professional use, I would steer clear of libraries that have no licence. Software available under Apache 2.0, MIT or BSD can be reused both in open source and commercial applications. Libraries available under a GPL licence ("GNU General Public Licence") can only be used in other GPL code.
Glosbe seems to offer this service:
In a very small experiment the service didn't seem so smart at distinguishing characters with multiple pronunciations:
See the documentation at https://glosbe.com/a-api
if you have programming knowledge, you may download the 國語辭典 (of taiwan), which is under creative commons 3.0
basically, it's 3 large excel files, using bash, grep, cut would give you relevant info of characters / words; and mandarin romanisation is one of them.
it's about 166000+ entries, that would be enough for "normal" usage.
have fun :)
It might be easier just to download CC-CEDICT in its entirety and look up your character in it. You can turn this into an API with very little effort.
I created a python module which converts a whole sentence into Pinyin. It also supports Cantonese/Jyutping. There's an accompanying REST API: https://github.com/lucwastiaux/python-pinyin-jyutping-sentence . It attempts to separate the sentence into words (which I haven't seen other chinese-to-pinyin modules do), using the jieba library.
Installing the module
pip install pinyin_jyutping_sentence
Mandarin / simplified characters
>>> import pinyin_jyutping_sentence >>> pinyin_jyutping_sentence.pinyin("提高口语") 'tígāo kǒuyǔ'
Cantonese / traditional characters
>>> import pinyin_jyutping_sentence >>> pinyin_jyutping_sentence.jyutping("我出去攞野食") 'ngǒ cēothêoi ló jěsik'
Calling the REST API
import requests import json url = "http://api.mandarincantonese.com/pinyin/办所有的事情" response = requests.get(url) print(json.loads(response.content)["pinyin"]) >>> bàn suǒyǒu de shìqíng
# calling the API from python import requests import json url = "http://api.mandarincantonese.com/jyutping/我哋盪失咗" response = requests.get(url) print(json.loads(response.content)["jyutping"]) >>> ngǒ déi dongsāt zó
Why would you want to use an external API for static data that does not change over time?
The translation map is a constant, and all the libraries and services just wrap those maps.
Or, as suggested in another answer, you can extract your own map from CEDICT.
Python-based identifier and conversion tool
Ruby-based conversion tool:
The wechat app for android comes with built-in conversion tables for chinese2pinyin and traditional2simplified which you can peek at by renaming the apk file to zip, unzipping it, and then looking under the
assets/fts folder where you will find
fts_t2s.txt corrrespondingly. (Note that this might change in future versions.)