node 将汉字转化为拼音

版权声明:转载请评论留言 https://blog.csdn.net/solocao/article/details/84189332

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

1、安装

yarn add pinying

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
}));                            // [ [ 'zh' ], [ 'x' ] ]

猜你喜欢

转载自blog.csdn.net/solocao/article/details/84189332
今日推荐