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

Solution to the problem of MySQL data delay jump

Today we analyzed another typical problem about d...

Share the responsive frameworks commonly used by web design masters (summary)

This article introduces and shares the responsive...

js to implement file upload style details

Table of contents 1. Overview 2. Parameters for c...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

Analysis of the causes of accidents caused by Unicode signature BOM

Maybe you are using include files here, which is u...

Complete step-by-step record of MySQL 8.0.26 installation and uninstallation

Table of contents Preface 1. Installation 1. Down...

Example of deploying Laravel application with Docker

The PHP base image used in this article is: php:7...

Vue uses el-table to dynamically merge columns and rows

This article example shares the specific code of ...

Solution to MySQL remote connection failure

I have encountered the problem that MySQL can con...

JS cross-domain XML--with AS URLLoader

Recently, I received a requirement for function ex...

Ideas and codes for implementing Vuex data persistence

What is vuex vuex: is a state manager developed s...

How to build a MySQL high-availability and high-performance cluster

Table of contents What is MySQL NDB Cluster Preli...