node 将汉字转化为拼音

使用npm包:pinyin,这还是一个两年前发布的神库吧。

1、安装

yarn add pinying
  • 1

2、使用

const pinyin = require("pinyin");
 
console.log(pinyin("中心"));    // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
  heteronym: true               // 启用多音字模式
}));                            // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
  heteronym: true,              // 启用多音字模式
  segment: true                 // 启用分词,以解决多音字问题。
}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
  style: pinyin.STYLE_INITIALS, // 设置拼音风格
  heteronym: true
}));                
原文地址:https://www.cnblogs.com/lguow/p/14351936.html