I used js to create a Chinese-to-pinyin package, and the warehouse address is visible:pinyin-pro
Featured functions
- Supports input and obtaining Chinese characters, words, and sentences in multiple formats
- Get the pinyin
- Get the initials
- Get the final
- Get the first letter of the pinyin
- Get the tone
- Get multiple pinyin for polyphonic characters
- Supports two output forms: string and array
Install
npm installation
npm install pinyin-pro
yarn installation
yarn add pinyin-pro
Introduced
Browser script introduction:
<!--Introduce a version,like3.2.0Version--> <!-- <script src="/gh/zh-lx/[email protected]/dist/"></script> --> <!--引入最新Version--> <script src="/gh/zh-lx/pinyin-pro@latest/dist/"></script> <script> var { pinyin } = pinyinPro; pinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn' </script>
ESModule introduced:
import { pinyin } from 'pinyin-pro'; pinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn'
Commonjs introduced:
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, Chinese that needs to be converted into pinyin
options: optional. Object type, used to configure various output forms, the key value configuration of options is as follows:
parameter | illustrate | type | Optional value | default value |
---|---|---|---|---|
pattern | The output result information (Pinyin/Initial/Indefinition/Tone/Initial) | string | pinyin / initial / final / num / first | pinyin |
toneType | Tone output form (pinyin notation/number/no pitch) | string | symbol / num / none | symbol |
type | Output result type (string/array) | string | string / array | string |
multiple | Output all the pinyin of polyphonic characters (actually if word is a Chinese character string with length 1) | boolean | true / false | false |
Example of usage
Get the pinyin
import { pinyin } from 'pinyin-pro'; // Get the pinyin with tonepinyin('Chinese Pinyin'); // 'hàn yǔ pīn yīn' // Get the pinyin without tonepinyin('Chinese Pinyin', { toneType: 'none' }); // 'han yu pin yin' // Get the pinyin of the tone converted to numeric suffixpinyin('Chinese Pinyin', { toneType: 'num' }); // 'han4 yu3 pin1 yin1' // Get array form with tone pinyinpinyin('Chinese Pinyin', { type: 'array' }); // ["hàn", "yǔ", "pīn", "yīn"] // Get the pinyin in the array form without tonepinyin('Chinese Pinyin', { toneType: 'none', type: 'array' }); // ["han", "yu", "pin", "yin"] // Get the pinyin of the array form tone converted to numeric suffixpinyin('Chinese Pinyin', { toneType: 'num', type: 'array' }); // ["han4", "yu3", "pin1", "yin1"]
Get the initials
import { pinyin } from 'pinyin-pro'; // Get the initialspinyin('Chinese Pinyin', { pattern: 'initial' }); // 'h y p y' // Get the initials of the arraypinyin('Chinese Pinyin', { pattern: 'initial', type: 'array' }); // ["h", "y", "p", "y"]
Get the final
import { pinyin } from 'pinyin-pro'; // Get the tonal finalspinyin('Chinese Pinyin', { pattern: 'final' }); // 'àn ǔ īn īn' // Get the final without tonepinyin('Chinese Pinyin', { pattern: 'final', toneType: 'none' }); // 'an u in in' // Get the final of the tone of the numberpinyin('Chinese Pinyin', { pattern: 'final', toneType: 'num' }); // 'an4 u3 in1 in1' // Get array form with tone finalspinyin('Chinese Pinyin', { pattern: 'final', type: 'array' }); // ["àn", "ǔ", "īn", "īn"] // Get array form without tonal finalspinyin('Chinese Pinyin', { pattern: 'final', toneType: 'none', type: 'array' }); // ["an", "u", "in", "in"] // Get the final of the array form tone as a numberpinyin('Chinese Pinyin', { pattern: 'final', toneType: 'num', type: 'array' }); // ['an4', 'u3', 'in1', 'in1']
Get the tone
import { pinyin } from 'pinyin-pro'; // Get the tonepinyin('Chinese Pinyin', { pattern: 'num' }); // '4 3 1 1' // Get array form tonepinyin('Chinese Pinyin', { pattern: 'num', type: 'array' }); // ["4", "3", "1", "1"]
Get the first letter of the pinyin
import { pinyin } from 'pinyin-pro'; // Get the first letter of the pinyinpinyin('Zhao Qiansun Li', { pattern: 'first' }); // 'z q s l é' // Get the first letter of the pinyin without tonepinyin('Zhao Qiansun Li', { pattern: 'first', toneType: 'none' }); // 'z q s l e' // Get the pinyin first letter in the array formpinyin('Zhao Qiansun Li', { pattern: 'first', type: 'array' }); // ['z', 'q', 's', 'l', 'é'] // Get the array form without tone pinyin first letterpinyin('Zhao Qiansun Li', { pattern: 'first', toneType: 'none', type: 'array' }); // ['z', 'q', 's', 'l', 'e']
Get the polyphonics of a single word
Only single words can be obtained in polyphonic mode, and words and sentences are invalid. You can also obtain array form, final form, etc. by configuring options
import { pinyin } from 'pinyin-pro'; // Get polyphonicpinyin('good', { multiple: true }); // 'hǎo hào' // Get array form polyphonicpinyin('good', { multiple: true, type: 'array' }); // ["hǎo", "hào"]
Summarize
This is the end of this article about JS's implementation of Chinese to Pinyin. For more related content on JS's Chinese to Pinyin, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!