pinyin, The convert tool of chinese pinyin.
简体中文 | English
Convert Han to pinyin. useful for phonetic notation, sorting, and searching.
Note: This module both support Node and Web browser.
Python version see mozillazg/python-pinyin
via npm:
for developer:
for cli:
<Array> pinyin(words[, options])Convert Han (汉字) to pinyin.
options argument is optional, for sepcify heteronym mode and pinyin styles.
Return a Array<Array<String>>. If one of Han is heteronym word, it would be
have multiple pinyin.
Number pinyin.compare(a, b)Default compare implementation for pinyin.
<Boolean> options.segmentEnable Chinese word segmentation. Segmentation is helpful for fix heteronym problem, but performance will be more slow, and need more CPU and memory.
Default is false.
<Boolean> options.heteronymEnable or disable heteronym mode. default is disabled, false.
<Boolean> options.groupGroup pinyin by phrases. for example:
<Object> options.styleSpecify pinyin style. please use static properties like STYLE_*.
default is .STYLE_TONE. see Static Property for more.
options.modepinyin mode, default is pinyin.MODE_NORMAL. If you cleared in surname scene,
use pinyin.MODE_SURNAME maybe better.
.STYLE_NORMALNormal mode.
Example: pin yin
.STYLE_TONETone style, this is default.
Example: pīn yīn
.STYLE_TONE2tone style by postfix number [0-4].
Example: pin1 yin1
.STYLE_TO3NEtone style by number [0-4] after phonetic notation character.
Example: pin1 yin1
.STYLE_INITIALSInitial consonant (of a Chinese syllable).
Example: pinyin of 中国 is zh g
Note: when a Han (汉字) without initial consonant, will convert to empty string.
.STYLE_FIRST_LETTERFirst letter style.
Example: p y
pinyin.MODE_NORMALNormal mode. This is the default mode.
pinyin.MODE_SURNAMESurname mode. If chinese word is surname, The pinyin of surname is prioritized.
pinyin support Node and Web browser now, the API and usage is complete same.
But the Web version is simple than Node version. Just frequently-used dict, without segmentation, and the dict is compress for web.
Because of Traditional and Segmentation, the convert result will be not complete same. and the test case have some different too.
| Feature | Web version | Node version |
|---|---|---|
| Dict | Frequently-used Dict, Compress. | Complete Dict, without Compress. |
| Segmentation | NO | Segmentation options. |
| Traditional | NO | Full Traditional support. |
This module provide default compare implementation:
But if you need different implementation, do it like:
If this module is helpful for you, please Star this repository.
And you have chioce donate to me via Aliapy or WeChat: