Pinyin Pro Library -- a pure front-end implementation of javascript for converting Chinese characters to Pinyin

Posted by chiefrokka on Fri, 24 Dec 2021 16:05:27 +0100

Generally, the function of converting Chinese characters to Pinyin is implemented by requesting the server api. For some simple requirements, we can use an npm toolkit—— pinyin-pro , the pure front-end realizes the function of converting Chinese characters to Pinyin.

Characteristic function

  • Support the input and acquisition of Chinese characters, words and sentences
  • Get Pinyin
  • Get initial consonant
  • Get vowels
  • Get Pinyin initials
  • Get tone
  • Get multiple Pinyin of polyphonic characters
  • Support person name and last name mode
  • Support custom Pinyin
  • Support string and array output

Version update

Current version: 3.3 1 -> 3.4. 0

  • New features
    • Support Pinyin recognition of last name pattern. See for specific usage Last name mode
    • Support user-defined Pinyin function. See for specific usage Custom Pinyin
  • Problem repair
    Fix stack overflow when text is too long
  • Phonetic calibration
    • Chu: 'zh ǔ’ -> ‘ ch ǔ zh ǔ’
    • Yu: 'y ù' - > 'y ú y ù'
    • Zang: 'z long ng' - > 'z ā ng’
    • Ben: 'B' - > 'B' ē n bì’
    • Shen: "X" ī n’ -> ‘sh ē n x ī n’
    • Li: 'zh í' - > 'l ì zh í'

Click to view Version update document

install

npm installation

npm install pinyin-pro

yarn installation

yarn add pinyin-pro

introduce

Browser script import:

<!--Introduce a version, such as 3.3.0 edition-->
<!-- <script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@3.3.0/dist/pinyin-pro.js"></script> -->
<!--Introduce the latest version-->
<script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@latest/dist/pinyin-pro.js"></script>
<script>
  var { pinyin } = pinyinPro;
  pinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn'
</script>

ESModule introduction:

import { pinyin } from 'pinyin-pro';
pinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn'

commonjs introduction:

const { pinyin } = require('pinyin-pro');
pinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn'

parameter

pinyin(word, options) receives two parameters

  • word: required. String type, which needs to be converted into Pinyin Chinese
  • Options: optional. Object type is used to configure various output forms. The key values of options are configured as follows:
parameterexplaintypeOptional valueDefault value
patternInformation of output results (pinyin / initials / finals / tones / initials)stringpinyin / initial / final / num / firstpinyin
toneTypeTone output form (pinyin symbol / number / no tone)stringsymbol / num / nonesymbol
typeOutput result type (string / array)stringstring / arraystring
multipleOutput all Pinyin of multi tone characters (only effective when word is a Chinese character string with length of 1)booleantrue / falsefalse
modePinyin search mode (general mode / last name mode)stringnormal / surnamenormal

Use example

Get Pinyin

import { pinyin } from 'pinyin-pro';

// Get Pinyin with tone
pinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn'
// Get Pinyin without tone
pinyin('Chinese Pinyin', { toneType: 'none' }); // 'han yu pin yin'
// Gets the Pinyin that converts the tone to a numeric suffix
pinyin('Chinese Pinyin', { toneType: 'num' }); // 'han4 yu3 pin1 yin1'
// Gets the tonal Pinyin in array form
pinyin('Chinese Pinyin', { type: 'array' }); // ["hàn", "yǔ", "pīn", "yīn"]
// Get Pinyin without tone in array form
pinyin('Chinese Pinyin', { toneType: 'none', type: 'array' }); // ["han", "yu", "pin", "yin"]
// Gets the Pinyin of the array tone converted to a numeric suffix
pinyin('Chinese Pinyin', { toneType: 'num', type: 'array' }); // ["han4", "yu3", "pin1", "yin1"]

Get initial consonant

import { pinyin } from 'pinyin-pro';

// Get initial consonant
pinyin('Chinese Pinyin', { pattern: 'initial' }); // 'h y p y'
// Get array initial
pinyin('Chinese Pinyin', { pattern: 'initial', type: 'array' }); // ["h", "y", "p", "y"]

Get vowels

import { pinyin } from 'pinyin-pro';

// Get tonal vowel
pinyin('Chinese Pinyin', { pattern: 'final' }); // 'àn ǔ īn īn'
// Get vowel without tone
pinyin('Chinese Pinyin', { pattern: 'final', toneType: 'none' }); // 'an u in in'
// Gets the vowel whose tone is a number
pinyin('Chinese Pinyin', { pattern: 'final', toneType: 'num' }); // 'an4 u3 in1 in1'
// Gets the tonal vowel in array form
pinyin('Chinese Pinyin', { pattern: 'final', type: 'array' }); // ["àn", "ǔ", "īn", "īn"]
// Gets an array form without a tonal vowel
pinyin('Chinese Pinyin', { pattern: 'final', toneType: 'none', type: 'array' }); // ["an", "u", "in", "in"]
// Gets the vowel whose tone is a number in the form of an array
pinyin('Chinese Pinyin', { pattern: 'final', toneType: 'num', type: 'array' }); // ['an4', 'u3', 'in1', 'in1']

Get tone

import { pinyin } from 'pinyin-pro';

// Get tone
pinyin('Chinese Pinyin', { pattern: 'num' }); // '4 3 1 1'
// Gets an array of tones
pinyin('Chinese Pinyin', { pattern: 'num', type: 'array' }); // ["4", "3", "1", "1"]

Get Pinyin initials

import { pinyin } from 'pinyin-pro';

// Get Pinyin initials
pinyin('Zhao Qian, sun li'e', { pattern: 'first' }); // 'z q s l é'
// Get phonetic initials without tones
pinyin('Zhao Qian, sun li'e', { pattern: 'first', toneType: 'none' }); // 'z q s l e'
// Gets the phonetic initials in array form
pinyin('Zhao Qian, sun li'e', { pattern: 'first', type: 'array' }); // ['z', 'q', 's', 'l', 'é']
// Gets the phonetic initials in array form without tones
pinyin('Zhao Qian, sun li'e', { pattern: 'first', toneType: 'none', type: 'array' }); // ['z', 'q', 's', 'l', 'e']

Get multiple tones of a single word

Only single words can get multi tone mode, and words and sentences are invalid. You can also configure the options option to obtain the array form, vowel and other formats

import { pinyin } from 'pinyin-pro';

// Get multi tone
pinyin('good', { multiple: true }); // 'hǎo hào'
// Get array multi tone
pinyin('good', { multiple: true, type: 'array' }); // ["hǎo", "hào"]

After the last name mode is enabled by setting mode: 'surname', the last names matched to the hundreds of family names will give priority to the output of last name Pinyin

import { pinyin } from 'pinyin-pro';

// Do not turn on last name mode
pinyin('My name is Zeng Xiaoxian'); // 'wǒ jiào céng xiǎo xián'

// Turn on last name mode
pinyin('My name is Zeng Xiaoxian', { mode: 'surname' }); // 'wǒ jiào zēng xiǎo xián'

The customPinyin method is exported inside the package, which supports the user-defined word and sentence Pinyin. When the Chinese matches the user-defined word and sentence Pinyin, the user-defined word and sentence pinyin is preferred

import { pinyin, customPinyin } from 'pinyin-pro';

customPinyin({
  Line by line: 'gàn yī háng xíng yī háng',
});
pinyin('Line by line'); // 'gàn yī háng xíng yī háng'

Topics: Javascript Front-end