js實(shí)現(xiàn)中文轉(zhuǎn)拼音的完整步驟記錄
使用js搞了一個(gè)中文轉(zhuǎn)拼音的包,倉庫地址可見:pinyin-pro
特色功能
- 支持漢字、詞語、句子多種格式輸入獲取
- 獲取拼音
- 獲取聲母
- 獲取韻母
- 獲取拼音首字母
- 獲取音調(diào)
- 獲取多音字的多種拼音
- 支持字符串和數(shù)組兩種輸出形式
安裝
npm 安裝
npm install pinyin-pro
yarn 安裝
yarn add pinyin-pro
引入
瀏覽器 script 引入:
<!--引入某個(gè)版本,如3.2.0版本--> <!-- <script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@3.2.0/dist/pinyin-pro.js"></script> --> <!--引入最新版本--> <script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@latest/dist/pinyin-pro.js"></script> <script> var { pinyin } = pinyinPro; pinyin('漢語拼音'); // 'hàn yǔ pīn yīn' </script>
ESModule 引入:
import { pinyin } from 'pinyin-pro'; pinyin('漢語拼音'); // 'hàn yǔ pīn yīn'
commonjs 引入:
const { pinyin } = require('pinyin-pro'); pinyin('漢語拼音'); // 'hàn yǔ pīn yīn'
參數(shù)
pinyin(word, options) 接收兩個(gè)參數(shù)
word:必填。String 類型,需要轉(zhuǎn)化為拼音的中文
options:可選。Object 類型,用于配置各種輸出形式,options 的鍵值配置如下:
參數(shù) | 說明 | 類型 | 可選值 | 默認(rèn)值 |
---|---|---|---|---|
pattern | 輸出的結(jié)果的信息(拼音 / 聲母 / 韻母 / 音調(diào) / 首字母) | string | pinyin / initial / final / num / first | pinyin |
toneType | 音調(diào)輸出形式(拼音符號 / 數(shù)字 / 不加音調(diào)) | string | symbol / num / none | symbol |
type | 輸出結(jié)果類型(字符串/數(shù)組) | string | string / array | string |
multiple | 輸出多音字全部拼音(僅在 word 為長度為 1 的漢字字符串時(shí)生效) | boolean | true / false | false |
使用示例
獲取拼音
import { pinyin } from 'pinyin-pro'; // 獲取帶音調(diào)拼音 pinyin('漢語拼音'); // 'hàn yǔ pīn yīn' // 獲取不帶聲調(diào)的拼音 pinyin('漢語拼音', { toneType: 'none' }); // 'han yu pin yin' // 獲取聲調(diào)轉(zhuǎn)換為數(shù)字后綴的拼音 pinyin('漢語拼音', { toneType: 'num' }); // 'han4 yu3 pin1 yin1' // 獲取數(shù)組形式帶音調(diào)拼音 pinyin('漢語拼音', { type: 'array' }); // ["hàn", "yǔ", "pīn", "yīn"] // 獲取數(shù)組形式不帶聲調(diào)的拼音 pinyin('漢語拼音', { toneType: 'none', type: 'array' }); // ["han", "yu", "pin", "yin"] // 獲取數(shù)組形式聲調(diào)轉(zhuǎn)換為數(shù)字后綴的拼音 pinyin('漢語拼音', { toneType: 'num', type: 'array' }); // ["han4", "yu3", "pin1", "yin1"]
獲取聲母
import { pinyin } from 'pinyin-pro'; // 獲取聲母 pinyin('漢語拼音', { pattern: 'initial' }); // 'h y p y' // 獲取數(shù)組形式聲母 pinyin('漢語拼音', { pattern: 'initial', type: 'array' }); // ["h", "y", "p", "y"]
獲取韻母
import { pinyin } from 'pinyin-pro'; // 獲取帶音調(diào)韻母 pinyin('漢語拼音', { pattern: 'final' }); // 'àn ǔ īn īn' // 獲取不帶音調(diào)韻母 pinyin('漢語拼音', { pattern: 'final', toneType: 'none' }); // 'an u in in' // 獲取音調(diào)為數(shù)字的韻母 pinyin('漢語拼音', { pattern: 'final', toneType: 'num' }); // 'an4 u3 in1 in1' // 獲取數(shù)組形式帶音調(diào)韻母 pinyin('漢語拼音', { pattern: 'final', type: 'array' }); // ["àn", "ǔ", "īn", "īn"] // 獲取數(shù)組形式不帶音調(diào)韻母 pinyin('漢語拼音', { pattern: 'final', toneType: 'none', type: 'array' }); // ["an", "u", "in", "in"] // 獲取數(shù)組形式音調(diào)為數(shù)字的韻母 pinyin('漢語拼音', { pattern: 'final', toneType: 'num', type: 'array' }); // ['an4', 'u3', 'in1', 'in1']
獲取音調(diào)
import { pinyin } from 'pinyin-pro'; // 獲取音調(diào) pinyin('漢語拼音', { pattern: 'num' }); // '4 3 1 1' // 獲取數(shù)組形式音調(diào) pinyin('漢語拼音', { pattern: 'num', type: 'array' }); // ["4", "3", "1", "1"]
獲取拼音首字母
import { pinyin } from 'pinyin-pro'; // 獲取拼音首字母 pinyin('趙錢孫李額', { pattern: 'first' }); // 'z q s l é' // 獲取不帶音調(diào)拼音首字母 pinyin('趙錢孫李額', { pattern: 'first', toneType: 'none' }); // 'z q s l e' // 獲取數(shù)組形式拼音首字母 pinyin('趙錢孫李額', { pattern: 'first', type: 'array' }); // ['z', 'q', 's', 'l', 'é'] // 獲取數(shù)組形式不帶音調(diào)拼音首字母 pinyin('趙錢孫李額', { pattern: 'first', toneType: 'none', type: 'array' }); // ['z', 'q', 's', 'l', 'e']
獲取單個(gè)字的多音
只有單字可以獲取到多音模式, 詞語、句子無效。同樣可以通過配置 options 選項(xiàng)獲取數(shù)組形式、韻母等格式
import { pinyin } from 'pinyin-pro'; // 獲取多音 pinyin('好', { multiple: true }); // 'hǎo hào' // 獲取數(shù)組形式多音 pinyin('好', { multiple: true, type: 'array' }); // ["hǎo", "hào"]
總結(jié)
到此這篇關(guān)于js實(shí)現(xiàn)中文轉(zhuǎn)拼音的文章就介紹到這了,更多相關(guān)js中文轉(zhuǎn)拼音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js實(shí)現(xiàn)動(dòng)態(tài)增加文件域表單功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)動(dòng)態(tài)增加文件域表單功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10div+css實(shí)現(xiàn)鼠標(biāo)放上去,背景跟圖片都會變化。
div+css實(shí)現(xiàn)鼠標(biāo)放上去,背景跟圖片都會變化。...2007-06-06JavaScript內(nèi)置對象Math與String詳細(xì)介紹
這篇文章主要介紹了JavaScript內(nèi)置對象Math與String的介紹與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08BootStrap Typeahead自動(dòng)補(bǔ)全插件實(shí)例代碼
本文給大家介紹BootStrap Typeahead自動(dòng)補(bǔ)全插件的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-08-08原生JS運(yùn)動(dòng)實(shí)現(xiàn)輪播圖
這篇文章主要為大家詳細(xì)介紹了原生JS運(yùn)動(dòng)實(shí)現(xiàn)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01關(guān)于__defineGetter__ 和__defineSetter__的說明
關(guān)于__defineGetter__ 和__defineSetter__的說明...2007-05-05快速實(shí)現(xiàn)JS圖片懶加載(可視區(qū)域加載)示例代碼
目前很多網(wǎng)站,在圖片加載時(shí)均采用了一種名為懶加載的方式,具體表現(xiàn)為,當(dāng)頁面被請求時(shí),只加載可視區(qū)域的圖片,其它部分的圖片則不加載,只有這些圖片出現(xiàn)在可視區(qū)域時(shí)才會動(dòng)態(tài)加載這些圖片,下面本文就介紹了JS圖片懶加載(可視區(qū)域加載)的實(shí)現(xiàn)方法,一起來看看吧。2017-01-01js 返回時(shí)間戳所對應(yīng)的具體時(shí)間
返回unix時(shí)間戳所對應(yīng)的具體時(shí)間的代碼2010-07-07javascript實(shí)現(xiàn)日期三級聯(lián)動(dòng)下拉框選擇菜單
這篇文章主要介紹了javascript實(shí)現(xiàn)日期三級聯(lián)動(dòng)下拉框選擇菜單,實(shí)現(xiàn)JS年月日三級聯(lián)動(dòng)下拉框選擇功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04