js to achieve the complete steps of Chinese to Pinyin conversion

js to achieve the complete steps of Chinese to Pinyin conversion

I used js to create a package for converting Chinese to Pinyin. The warehouse address can be seen: pinyin-pro

Features

  • Supports input and acquisition of Chinese characters, words, and sentences in multiple formats
  • Get Pinyin
  • Get initial consonants
  • Get the finals
  • Get the first letter of Pinyin
  • Get the Tone
  • Get multiple pinyins of polyphonetic characters
  • Supports string and array output formats

Install

npm installation

npm install pinyin-pro

Yarn Installation

yarn add pinyin-pro

Introduction

Browser script introduction:

<!--Introduce a version, such as version 3.2.0-->
<!-- <script src="https://cdn.jsdelivr.net/gh/zh-lx/[email protected]/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('Hanyu Pinyin'); // 'hàn yǔ pīn yīn'
</script>

ESModule import:

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

Commonjs is introduced:

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

parameter

pinyin(word, options) accepts two parameters

word: required. String type, needs to be converted to Chinese pinyin
options: Optional. Object type, used to configure various output forms. The key value configuration of options is as follows:

parameter illustrate type Optional Values default value
pattern Output result information (pinyin/initial consonant/final vowel/tone/first letter) string pinyin / initial / final / num / first Pinyin
toneType Tone output format (pinyin symbols/numbers/no tone) string symbol / num / none symbol
type Output result type (string/array) string string / array string
multiple Output all pinyins of polyphones (valid only when word is a Chinese character string with a length of 1) boolean true / false false

Usage Examples

Get Pinyin

import { pinyin } from 'pinyin-pro';

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

Get initial consonants

import { pinyin } from 'pinyin-pro';

// Get the initial consonant pinyin('Hanyu Pinyin', { pattern: 'initial' }); // 'hypy'
// Get the initial consonants in array form pinyin('Hanyu Pinyin', { pattern: 'initial', type: 'array' }); // ["h", "y", "p", "y"]

Get the finals

import { pinyin } from 'pinyin-pro';

// Get the finals with tones pinyin('Hanyu Pinyin', { pattern: 'final' }); // 'àn ǔ īn īn'
// Get the finals without tone pinyin('Hanyu Pinyin', { pattern: 'final', toneType: 'none' }); // 'an u in in'
// Get the final vowels with numeral tones pinyin('Hanyu Pinyin', { pattern: 'final', toneType: 'num' }); // 'an4 u3 in1 in1'
// Get the array of tones and finals pinyin('Hanyu Pinyin', { pattern: 'final', type: 'array' }); // ["àn", "ǔ", "īn", "īn"]
// Get the array of finals without tones pinyin('Hanyu Pinyin', { pattern: 'final', toneType: 'none', type: 'array' }); // ["an", "u", "in", "in"]
// Get the finals of Chinese Pinyin in array format with tones as numbers pinyin('Hanyu Pinyin', { pattern: 'final', toneType: 'num', type: 'array' }); // ['an4', 'u3', 'in1', 'in1']

Get the Tone

import { pinyin } from 'pinyin-pro';

// Get the tone pinyin('Hanyu Pinyin', { pattern: 'num' }); // '4 3 1 1'
// Get the tones in array form pinyin('Hanyu Pinyin', { pattern: 'num', type: 'array' }); // ["4", "3", "1", "1"]

Get the first letter of Pinyin

import { pinyin } from 'pinyin-pro';

// Get the first letter of pinyin pinyin('赵钱孙李额', { pattern: 'first' }); // 'zqsl é'
// Get the first letter of pinyin without tone pinyin('赵钱孙李额', { pattern: 'first', toneType: 'none' }); // 'zqsle'
// Get the first letters of pinyin in array form pinyin('赵钱孙李额', { pattern: 'first', type: 'array' }); // ['z', 'q', 's', 'l', 'é']
// Get the first letters of pinyin without tone in array form pinyin('赵钱孙李额', { pattern: 'first', toneType: 'none', type: 'array' }); // ['z', 'q', 's', 'l', 'e']

Get multiple pronunciations of a single word

Only single words can obtain polyphonic mode, words and sentences are invalid. You can also configure options to get the array form, finals and other formats

import { pinyin } from 'pinyin-pro';

// Get multiple pinyin('好', { multiple: true }); // 'hǎo hào'
// Get multiple sounds in array form pinyin('好', { multiple: true, type: 'array' }); // ["hǎo", "hào"]

Summarize

This is the end of this article about how to convert Chinese to Pinyin using js. For more information about how to convert Chinese to Pinyin using js, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript to get the first letter of Chinese Pinyin
  • JS method to obtain the first letter of Chinese pinyin and quickly find the corresponding Chinese content on the page through the first letter of pinyin [with demo source code]
  • JS implements the method of sorting Chinese characters by pinyin
  • js gets Chinese pinyin, Select automatically matches letters to get the value code
  • js package tool class for extracting the first letter of Chinese pinyin
  • JavaScript Chinese to Pinyin conversion code has some bugs
  • JavaScript code to sort Chinese characters by pinyin
  • Extjs Gird supports Chinese pinyin sorting implementation code

<<:  How to enable the slow query log function in MySQL

>>:  CentOS 7.5 deploys Varnish cache server function

Recommend

Vue3 realizes the image magnifying glass effect

This article example shares the specific code of ...

Detailed explanation of how to configure Nginx web server sample code

Overview Today we will mainly share how to config...

Detailed explanation of process management in Linux system

Table of contents 1. The concept of process and t...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

Telnet is moved to busybox-extras in Alpine image

The telnet in the Alpine image has been moved to ...

How to quickly deploy an Elasticsearch cluster using docker

This article will use Docker containers (orchestr...

CentOS6 upgrade glibc operation steps

Table of contents background Compile glibc 2.14 M...

CSS implements a pop-up window effect with a mask layer that can be closed

Pop-up windows are often used in actual developme...

Detailed tutorial on installing and configuring MySql5.7 on Ubuntu 20.04

Table of contents 1. Ubuntu source change 2. Inst...

Explanation of the working mechanism of namenode and secondarynamenode in Hadoop

1) Process 2) FSImage and Edits Nodenode is the b...

Web front-end skills summary (personal practical experience)

1. Today, when I was making a page, I encountered ...