@edgeros/jsre-pinyin4js
v1.0.0
Published
A javascript library for converting chinese to pinyin.
Downloads
4
Readme
Overview
A opensource javascript library for converting chinese to pinyin. Features:
Zero dependency
Thesaurus can be imported and packaged flexibly You can adjust the dictionary by yourself, refer to src/dict for details; all resource calls are encapsulated by PinyinResource, and can be modified and packaged by yourself
Accurate and perfect font library Unicode code from 4E00-9FA5 range and 3007 (〇) 20903 Chinese characters, pinyin4js can convert all Chinese characters except 46 variants (there is no standard pinyin for variants).
Fast Pinyin conversion speed After testing, pinyin4js takes about 110 milliseconds for 20902 Chinese characters ranging from 4E00-9FA5.
Multi-pinyin format output support Support multiple pinyin output formats: with phonetic symbols, without phonetic symbols, numbers for phonetic symbols, and pinyin first letter output formats.
Common polyphonic word recognition Support the recognition of common polyphonic characters, including phrases, idioms, place names, etc.
Simplified Traditional Chinese Conversion
Support adding custom dictionaries Support adding user-defined dictionaries.
User can use the following code to import the pinyin4js
module .
var pinyin4js = require('pinyin4js');
Support
The following shows pinyin4js
module APIs avaliable for each permissions.
|User Mode|Privilege Mode --|:--:|:--: pinyin4js.pinyinFormat|●|● pinyin4js.pinyinHelper|●|● pinyinHelper.convertToPinyinString|●|● pinyinHelper.getShortPinyin|●|● pinyinHelper.hasMultiPinyin|●|●
Pinyin4js Object
pinyin4js.pinyinFormat
- {Object} Pinyin tone format options. Optons:
- WITH_TONE_MARK: With tone.
- WITHOUT_TONE: Without tone.
- WITH_TONE_NUMBER: The number represents the tone of the voice.
- FIRST_LETTER: Acronym style.
pinyin4js.pinyinHelper
- {Object} This object has some methods to convert Chinese characters into pinyin.
PinyinHelper Object
pinyinHelper.convertToPinyinString(str, separator, format)
str
{String} Chinese characters to be converted.separator
{String} Separator between each pinyin.format
{PinyinFormat} Pinyin format:- pinyin4js.pinyinFormat.WITH_TONE_NUMBER -- digits represent tones;
- pinyin4js.pinyinFormat.WITHOUT_TONE -- has no tones;
- pinyin4js.pinyinFormat.WITH_TONE_MARK -- has tones.
- Returns: {String} Result string.
Convert the string to Pinyin in the corresponding format.
Example
const {pinyinHelper, pinyinFormat} = require('pinyin4js');
var result = pinyinHelper.convertToPinyinString('你好世界', '-', pinyinFormat.WITHOUT_TONE);
console.log('get pinyin:', result);
pinyinHelper.getShortPinyin(str)
str
{String} Chinese characters to be converted.- Returns: {String} Result string.
Get the first letter of the string corresponding to Pinyin.
Example
const {pinyinHelper} = require('pinyin4js');
var result = pinyinHelper.getShortPinyin('你好世界');
console.log('get first char:', result);
pinyinHelper.hasMultiPinyin(char)
char
{Char} Chinese character to be check.- Returns: {Boolean} Judeg result.
Judge whether a Chinese character is polysyllabic or not.
Example
const {pinyinHelper} = require('pinyin4js');
var result = pinyinHelper.hasMultiPinyin('长');
console.log('Whether it is polysyllabic or not:', result);