1.0.1 • Published 6 months ago

ts-pinyin-lxc v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

本项目为sxei/pinyinjs项目修改而成

本项目仅支持TS(模块化)

安装

npm i ts-pinyin-lxc

使用

为了优化大小,字典可按需导入:

有以下字典(大小从小到大):

  • pinyin_dict_firstletter -> 仅使用汉字转拼音首字母
  • pinyin_dict_notone -> 没有声调、不支持多音字的字典
  • pinyin_dict_withtone -> 含有声调、支持多音字的字典
  • pinyin_dict_polyphone -> 词库(带多音字的词语更精确) 词典巨大, 不推荐

获取没有声调的拼音

// 导入工具和字典
import {pinyinUtil} from 'ts-pinyin-lxc'
import {pinyin_dict_notone} from 'ts-pinyin-lxc/dict'

// 添加字典
pinyinUtil.addDict(pinyin_dict_notone)

/**
 * 根据汉字获取拼音,如果不是汉字直接返回原字符
 * @param chinese 要转换的汉字
 * @param splitter 分隔字符,默认用空格分隔
 * @param withtone 返回结果是否包含声调(需要withtone字典),默认为false
 * @param polyphone 是否支持多音字(需要withtone字典),默认false,如果为true,会返回所有可能的组合数组, 导入pinyin_dict_polyphone词库后设为true才启用词库匹配(此时返回string)
 * @return 如果polyphone为false则返回string, 如果polyphone为true但没有导入pinyin_dict_polyphone词库则返回string[] (各个音的所有可能字符串数组), 导入了则返回string
 */
const result = pinyinUtil.getPinyin('你好世界')

console.log(result);

获取含有声调的拼音

// 导入工具和字典
import {pinyinUtil} from 'ts-pinyin-lxc'
import {pinyin_dict_withtone} from 'ts-pinyin-lxc/dict'

// 添加字典
pinyinUtil.addDict(pinyin_dict_withtone)

/**
 * 根据汉字获取拼音,如果不是汉字直接返回原字符
 * @param chinese 要转换的汉字
 * @param splitter 分隔字符,默认用空格分隔
 * @param withtone 返回结果是否包含声调(需要withtone字典),默认为false
 * @param polyphone 是否支持多音字(需要withtone字典),默认false,如果为true,会返回所有可能的组合数组, 导入pinyin_dict_polyphone词库后设为true才启用词库匹配(此时返回string)
 * @return 如果polyphone为false则返回string, 如果polyphone为true但没有导入pinyin_dict_polyphone词库则返回string[] (各个音的所有可能字符串数组), 导入了则返回string
 */
const result = pinyinUtil.getPinyin('你好世界', ' ', true)

console.log(result);

要获取拼音首字母

// 导入工具和字典
import {pinyinUtil} from 'ts-pinyin-lxc'
import {pinyin_dict_firstletter} from 'ts-pinyin-lxc/dict'

// 添加字典
pinyinUtil.addDict(pinyin_dict_firstletter)

/**
 * 获取汉字的拼音首字母
 * @param str 汉字字符串,如果遇到非汉字则原样返回
 * @param polyphone 是否支持多音字(需要withtone字典),默认false,如果为true,会返回所有可能的组合数组, 导入pinyin_dict_polyphone词库后设为true才启用词库匹配(此时返回string)
 * @return 如果polyphone为false则返回string, 如果polyphone为true但没有导入pinyin_dict_polyphone词库则返回string[] (各个音的所有可能字符串数组), 导入了则返回string
 */
const result = pinyinUtil.getFirstLetter('你好世界')

console.log(result);

从拼音获取汉字

// 导入工具和字典
import {pinyinUtil} from 'ts-pinyin-lxc'
import {pinyin_dict_withtone} from 'ts-pinyin-lxc/dict'

// 添加字典
pinyinUtil.addDict(pinyin_dict_withtone)

/**
 * 拼音转汉字,只支持单个汉字,返回所有匹配的汉字组合
 * @param pinyin 单个汉字的拼音
 */
const result = pinyinUtil.getHanzi('hao')

console.log(result);

更多详细介绍

更多介绍请前往原项目查看: https://github.com/sxei/pinyinjs

1.0.1

6 months ago

1.0.0

6 months ago