This is the fifth day of my participation in Gwen Challenge

Use JS to make a Chinese to pinyin package, warehouse address visible: Pinyin-Pro

features

  • Supports Chinese characters, words, and sentences
  • Access to pinyin
  • To obtain initial
  • For finals
  • Gets pinyin initials
  • Access to tone
  • Get multiple pinyin for polyphonic words
  • Supports two output formats: string and array

The installation

NPM install

npm install pinyin-pro
Copy the code

Yarn installation

yarn add pinyin-pro
Copy the code

The introduction of

Browser script introduced:

<! -- Import a version, such as 3.2.0 -->
<! -- < script SRC = "https://cdn.jsdelivr.net/gh/zh-lx/[email protected]/dist/pinyin-pro.js" > < / script > -- >
<! -- Introducing 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(Hanyu Pinyin); // 'han yǔ P īn yīn'
</script>
Copy the code

ESModule introduction:

import { pinyin } from 'pinyin-pro';
pinyin(Hanyu Pinyin); // 'han yǔ P īn yīn'
Copy the code

Commonjs is introduced:

const { pinyin } = require('pinyin-pro');
pinyin(Hanyu Pinyin); // 'han yǔ P īn yīn'
Copy the code

parameter

Pinyin (Word, options) takes two arguments

  • Word: mandatory. String type, need to be converted to pinyin Chinese
  • Options: Optional. The Object type is used to configure various output formats. The key values of options are as follows:
parameter instructions type An optional value The default value
pattern Output information about the result (pinyin/initials/vowels/tones/initials) string pinyin / initial / final / num / first pinyin
toneType Tone output form (pinyin symbols/numbers/without tones) string symbol / num / none symbol
type Output result type (string/array) string string / array string
multiple Output polyphonic word in full pinyin (only valid if Word is a Character string of length 1) boolean true / false false

Use the sample

Access to pinyin

import { pinyin } from 'pinyin-pro';

// Get pinyin with tones
pinyin(Hanyu Pinyin); // 'han yǔ P īn yīn'
// Get pinyin without tones
pinyin(Hanyu Pinyin, { toneType: 'none' }); // 'han yu pin yin'
// Get pinyin for tone conversion to numeric suffixes
pinyin(Hanyu Pinyin, { toneType: 'num' }); // 'han4 yu3 pin1 yin1'
// Get array form with tonal pinyin
pinyin(Hanyu Pinyin, { type: 'array' }); // [" han ", "Y ǔ"," P īn", "Y īn"]
// Get the array form of pinyin without tones
pinyin(Hanyu Pinyin, { toneType: 'none'.type: 'array' }); // ["han", "yu", "pin", "yin"]
// Get array form tones converted to numeric suffixes in pinyin
pinyin(Hanyu Pinyin, { toneType: 'num'.type: 'array' }); // ["han4", "yu3", "pin1", "yin1"]
Copy the code

To obtain initial

import { pinyin } from 'pinyin-pro';

// Get initial
pinyin(Hanyu Pinyin, { pattern: 'initial' }); // 'h y p y'
// Get the array initials
pinyin(Hanyu Pinyin, { pattern: 'initial'.type: 'array' }); // ["h", "y", "p", "y"]
Copy the code

For finals

import { pinyin } from 'pinyin-pro';

// Get tonal vowels
pinyin(Hanyu Pinyin, { pattern: 'final' }); // 'àn ǔ īn īn'
// Get untoned vowels
pinyin(Hanyu Pinyin, { pattern: 'final'.toneType: 'none' }); // 'an u in in'
// Get the vowel with a number of tones
pinyin(Hanyu Pinyin, { pattern: 'final'.toneType: 'num' }); // 'an4 u3 in1 in1'
// Get the array form with tonal vowels
pinyin(Hanyu Pinyin, { pattern: 'final'.type: 'array' }); // [" an ", "ǔ", "īn", "īn"]
// Get an array of untoned vowels
pinyin(Hanyu Pinyin, { pattern: 'final'.toneType: 'none'.type: 'array' }); // ["an", "u", "in", "in"]
// Get an array of vowels with tones of numbers
pinyin(Hanyu Pinyin, { pattern: 'final'.toneType: 'num'.type: 'array' }); // ['an4', 'u3', 'in1', 'in1']
Copy the code

Access to tone

import { pinyin } from 'pinyin-pro';

// Get the tone
pinyin(Hanyu Pinyin, { pattern: 'num' }); // '1'
// Get an array of tones
pinyin(Hanyu Pinyin, { pattern: 'num'.type: 'array' }); // ["4", "3", "1", "1"]
Copy the code

Gets pinyin initials

import { pinyin } from 'pinyin-pro';

// Get pinyin initials
pinyin('Zhao Qian Sun Li Eh', { pattern: 'first' }); // 'z q s l e '
// Get the first letter of the phonetic alphabet
pinyin('Zhao Qian Sun Li Eh', { pattern: 'first'.toneType: 'none' }); // 'z q s l e'
// Get the array form pinyin first letter
pinyin('Zhao Qian Sun Li Eh', { pattern: 'first'.type: 'array' }); // ['z', 'q', 's', 'l', 'e ']
// Get array form without tone tone pinyin initials
pinyin('Zhao Qian Sun Li Eh', { pattern: 'first'.toneType: 'none', type:'array' }); // ['z', 'q', 's', 'l', 'e']
Copy the code

Gets the polyphony of a single word

Only single words can get polyphonic patterns, words and sentences are invalid. You can also obtain the array format and vowel format by configuring the Options option

import { pinyin } from 'pinyin-pro';

// Get multitone
pinyin('good', { multiple: true }); / / 'm: h o hao'
// Get array form polyphonic
pinyin('good', { multiple: true.type: 'array' }); / / [h m: "o", "hao"]
Copy the code