mirror of
https://github.com/zh-lx/pinyin-pro.git
synced 2026-03-13 09:51:38 +08:00
feat(3.4.0): 新增姓氏模式和自定义拼音
This commit is contained in:
17
CHANGELOG.md
17
CHANGELOG.md
@@ -1,3 +1,20 @@
|
||||
## 3.4.0
|
||||
|
||||
当前版本: 3.3.1 -> 3.4.0
|
||||
|
||||
- 新增功能
|
||||
- 支持姓氏模式拼音识别,具体用法见[姓氏模式](#surname)
|
||||
- 支持自定义拼音功能,具体用法见[自定义拼音](#custom)
|
||||
- 问题修复
|
||||
修复文本过长时堆栈溢出问题
|
||||
- 字音校准
|
||||
- 褚: 'zhǔ' -> 'chǔ zhǔ'
|
||||
- 俞: 'yù' -> 'yú yù'
|
||||
- 臧: 'zàng' -> 'zāng'
|
||||
- 贲: 'bì' -> 'bēn bì'
|
||||
- 莘: 'xīn' -> 'shēn xīn'
|
||||
- 郦: 'zhí' -> 'lì zhí'
|
||||
|
||||
## 3.3.1
|
||||
|
||||
当前版本: 3.3.0 -> 3.3.1
|
||||
|
||||
35
README.md
35
README.md
@@ -17,12 +17,17 @@
|
||||
- 获取拼音首字母
|
||||
- 获取音调
|
||||
- 获取多音字的多种拼音
|
||||
- 支持人名姓氏模式
|
||||
- 支持自定义拼音
|
||||
- 支持字符串和数组两种输出形式
|
||||
|
||||
## 版本更新
|
||||
|
||||
当前版本: 3.3.0 -> 3.3.1
|
||||
当前版本: 3.3.1 -> 3.4.0
|
||||
|
||||
- 新增功能
|
||||
- 支持姓氏模式拼音识别,具体用法见[姓氏模式](#surname)
|
||||
- 支持自定义拼音功能,具体用法见[自定义拼音](#custom)
|
||||
- 问题修复
|
||||
修复文本过长时堆栈溢出问题
|
||||
- 字音校准
|
||||
@@ -91,6 +96,7 @@ pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
|
||||
| toneType | 音调输出形式(拼音符号 / 数字 / 不加音调) | string | symbol / num / none | symbol |
|
||||
| type | 输出结果类型(字符串/数组) | string | string / array | string |
|
||||
| multiple | 输出多音字全部拼音(仅在 word 为长度为 1 的汉字字符串时生效) | boolean | true / false | false |
|
||||
| mode | 拼音查找的模式(常规模式 / 姓氏模式) | string | normal / surname | normal |
|
||||
|
||||
## 使用示例
|
||||
|
||||
@@ -182,6 +188,33 @@ pinyin('好', { multiple: true }); // 'hǎo hào'
|
||||
pinyin('好', { multiple: true, type: 'array' }); // ["hǎo", "hào"]
|
||||
```
|
||||
|
||||
### <a id="surname">姓氏模式</a>
|
||||
|
||||
通过设置 `mode: 'surname'` 开启姓氏模式后,匹配到百家姓中的姓氏优先输出姓氏拼音
|
||||
|
||||
```javascript
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 不开启姓氏模式
|
||||
pinyin('我叫曾小贤'); // 'wǒ jiào céng xiǎo xián'
|
||||
|
||||
// 开启姓氏模式
|
||||
pinyin('我叫曾小贤', { mode: 'surname' }); // 'wǒ jiào zēng xiǎo xián'
|
||||
```
|
||||
|
||||
### <a id="custom">自定义拼音</a>
|
||||
|
||||
包内部导出了 `customPinyin` 方法,支持用户自定义设置词句拼音,当中文中匹配用户自己定义的词句拼音时,优先使用用户自定义的词句拼音
|
||||
|
||||
```javascript
|
||||
import { pinyin, customPinyin } from 'pinyin-pro';
|
||||
|
||||
customPinyin({
|
||||
干一行行一行: 'gàn yī háng xíng yī háng',
|
||||
});
|
||||
pinyin('干一行行一行'); // 'gàn yī háng xíng yī háng'
|
||||
```
|
||||
|
||||
## 贡献与反馈
|
||||
|
||||
参与开源贡献请参照 [pinyin-pro 贡献](./docs/contribute.md)
|
||||
|
||||
488
data/surname.ts
488
data/surname.ts
@@ -1,32 +1,476 @@
|
||||
const Surnames = {
|
||||
华: 'huà',
|
||||
乐: 'yuè',
|
||||
缪: 'miào',
|
||||
解: 'xiè',
|
||||
应: 'yīng',
|
||||
单: 'shàn',
|
||||
於: 'yū',
|
||||
隗: 'kuí',
|
||||
宁: 'nìng',
|
||||
仇: 'qiú',
|
||||
钭: 'tǒu',
|
||||
薄: 'bó',
|
||||
能: 'nài',
|
||||
桑: 'sāng',
|
||||
燕: 'yān',
|
||||
阙: 'quē',
|
||||
訾: 'zǐ',
|
||||
那: 'nā',
|
||||
曾: 'zēng',
|
||||
相: 'xiàng',
|
||||
查: 'zhā',
|
||||
const Surnames: { [key: string]: string } = {
|
||||
南宫: 'nán gōng',
|
||||
第五: 'dì wǔ',
|
||||
万俟: 'mò qí',
|
||||
司马: 'sī mǎ',
|
||||
上官: 'shàng guān',
|
||||
欧阳: 'ōu yáng',
|
||||
夏侯: 'xià hóu',
|
||||
诸葛: 'zhū gě',
|
||||
闻人: 'wén rén',
|
||||
东方: 'dōng fāng',
|
||||
赫连: 'hè lián',
|
||||
皇甫: 'huáng pǔ',
|
||||
尉迟: 'yù chí',
|
||||
公羊: 'gōng yáng',
|
||||
澹台: 'tán tái',
|
||||
公冶: 'gōng yě',
|
||||
宗政: 'zōng zhèng',
|
||||
濮阳: 'pú yáng',
|
||||
淳于: 'chún yú',
|
||||
单于: 'chán yú',
|
||||
太叔: 'tài shū',
|
||||
申屠: 'shēn tú',
|
||||
公孙: 'gōng sūn',
|
||||
仲孙: 'zhòng sūn',
|
||||
轩辕: 'xuān yuán',
|
||||
令狐: 'líng hú',
|
||||
钟离: 'zhōng lí',
|
||||
宇文: 'yǔ wén',
|
||||
长孙: 'zhǎng sūn',
|
||||
慕容: 'mù róng',
|
||||
鲜于: 'xiān yú',
|
||||
闾丘: 'lǘ qiū',
|
||||
司徒: 'sī tú',
|
||||
司空: 'sī kōng',
|
||||
亓官: 'qí guān',
|
||||
司寇: 'sī kòu',
|
||||
仉督: 'zhǎng dū',
|
||||
子车: 'zǐ jū',
|
||||
颛孙: 'zhuān sūn',
|
||||
端木: 'duān mù',
|
||||
巫马: 'wū mǎ',
|
||||
公西: 'gōng xī',
|
||||
漆雕: 'qī diāo',
|
||||
乐正: 'yuè zhèng',
|
||||
壤驷: 'rǎng sì',
|
||||
公良: 'gōng liáng',
|
||||
拓跋: 'tuò bá',
|
||||
夹谷: 'jiá gǔ',
|
||||
宰父: 'zǎi fǔ',
|
||||
榖梁: 'gǔ liáng',
|
||||
段干: 'duàn gān',
|
||||
百里: 'bǎi lǐ',
|
||||
东郭: 'dōng guō',
|
||||
南门: 'nán mén',
|
||||
呼延: 'hū yán',
|
||||
羊舌: 'yáng shé',
|
||||
梁丘: 'liáng qiū',
|
||||
左丘: 'zuǒ qiū',
|
||||
东门: 'dōng mén',
|
||||
西门: 'xī mén',
|
||||
赵: 'zhào',
|
||||
钱: 'qián',
|
||||
孙: 'sūn',
|
||||
李: 'lǐ',
|
||||
周: 'zhōu',
|
||||
吴: 'wú',
|
||||
郑: 'zhèng',
|
||||
王: 'wáng',
|
||||
冯: 'féng',
|
||||
陈: 'chén',
|
||||
褚: 'chǔ',
|
||||
卫: 'wèi',
|
||||
蒋: 'jiǎng',
|
||||
沈: 'shěn',
|
||||
韩: 'hán',
|
||||
杨: 'yáng',
|
||||
朱: 'zhū',
|
||||
秦: 'qín',
|
||||
尤: 'yóu',
|
||||
许: 'xǔ',
|
||||
何: 'hé',
|
||||
吕: 'lǚ',
|
||||
施: 'shī',
|
||||
张: 'zhāng',
|
||||
孔: 'kǒng',
|
||||
曹: 'cáo',
|
||||
严: 'yán',
|
||||
华: 'huà',
|
||||
金: 'jīn',
|
||||
魏: 'wèi',
|
||||
陶: 'táo',
|
||||
姜: 'jiāng',
|
||||
戚: 'qī',
|
||||
谢: 'xiè',
|
||||
邹: 'zōu',
|
||||
喻: 'yù',
|
||||
柏: 'bǎi',
|
||||
水: 'shuǐ',
|
||||
窦: 'dòu',
|
||||
章: 'zhāng',
|
||||
云: 'yún',
|
||||
苏: 'sū',
|
||||
潘: 'pān',
|
||||
葛: 'gě',
|
||||
奚: 'xī',
|
||||
范: 'fàn',
|
||||
彭: 'péng',
|
||||
郎: 'láng',
|
||||
鲁: 'lǔ',
|
||||
韦: 'wéi',
|
||||
昌: 'chāng',
|
||||
马: 'mǎ',
|
||||
苗: 'miáo',
|
||||
凤: 'fèng',
|
||||
花: 'huā',
|
||||
方: 'fāng',
|
||||
俞: 'yú',
|
||||
任: 'rèn',
|
||||
袁: 'yuán',
|
||||
柳: 'liǔ',
|
||||
酆: 'fēng',
|
||||
鲍: 'bào',
|
||||
史: 'shǐ',
|
||||
唐: 'táng',
|
||||
费: 'fèi',
|
||||
廉: 'lián',
|
||||
岑: 'cén',
|
||||
薛: 'xuē',
|
||||
雷: 'léi',
|
||||
贺: 'hè',
|
||||
倪: 'ní',
|
||||
汤: 'tāng',
|
||||
滕: 'téng',
|
||||
殷: 'yīn',
|
||||
罗: 'luó',
|
||||
毕: 'bì',
|
||||
郝: 'hǎo',
|
||||
邬: 'wū',
|
||||
安: 'ān',
|
||||
常: 'cháng',
|
||||
乐: 'yuè',
|
||||
于: 'yú',
|
||||
时: 'shí',
|
||||
傅: 'fù',
|
||||
皮: 'pí',
|
||||
卞: 'biàn',
|
||||
齐: 'qí',
|
||||
康: 'kāng',
|
||||
伍: 'wǔ',
|
||||
余: 'yú',
|
||||
元: 'yuán',
|
||||
卜: 'bǔ',
|
||||
顾: 'gù',
|
||||
孟: 'mèng',
|
||||
平: 'píng',
|
||||
黄: 'huáng',
|
||||
和: 'hé',
|
||||
穆: 'mù',
|
||||
萧: 'xiāo',
|
||||
尹: 'yǐn',
|
||||
姚: 'yáo',
|
||||
邵: 'shào',
|
||||
湛: 'zhàn',
|
||||
汪: 'wāng',
|
||||
祁: 'qí',
|
||||
毛: 'máo',
|
||||
禹: 'yǔ',
|
||||
狄: 'dí',
|
||||
米: 'mǐ',
|
||||
贝: 'bèi',
|
||||
明: 'míng',
|
||||
臧: 'zāng',
|
||||
计: 'jì',
|
||||
伏: 'fú',
|
||||
成: 'chéng',
|
||||
戴: 'dài',
|
||||
谈: 'tán',
|
||||
宋: 'sòng',
|
||||
茅: 'máo',
|
||||
庞: 'páng',
|
||||
熊: 'xióng',
|
||||
纪: 'jì',
|
||||
舒: 'shū',
|
||||
屈: 'qū',
|
||||
项: 'xiàng',
|
||||
祝: 'zhù',
|
||||
董: 'dǒng',
|
||||
梁: 'liáng',
|
||||
杜: 'dù',
|
||||
阮: 'ruǎn',
|
||||
蓝: 'lán',
|
||||
闵: 'mǐn',
|
||||
席: 'xí',
|
||||
季: 'jì',
|
||||
麻: 'má',
|
||||
强: 'qiáng',
|
||||
贾: 'jiǎ',
|
||||
路: 'lù',
|
||||
娄: 'lóu',
|
||||
危: 'wēi',
|
||||
江: 'jiāng',
|
||||
童: 'tóng',
|
||||
颜: 'yán',
|
||||
郭: 'guō',
|
||||
梅: 'méi',
|
||||
盛: 'shèng',
|
||||
林: 'lín',
|
||||
刁: 'diāo',
|
||||
钟: 'zhōng',
|
||||
徐: 'xú',
|
||||
邱: 'qiū',
|
||||
骆: 'luò',
|
||||
高: 'gāo',
|
||||
夏: 'xià',
|
||||
蔡: 'cài',
|
||||
田: 'tián',
|
||||
樊: 'fán',
|
||||
胡: 'hú',
|
||||
凌: 'líng',
|
||||
霍: 'huò',
|
||||
虞: 'yú',
|
||||
万: 'wàn',
|
||||
支: 'zhī',
|
||||
柯: 'kē',
|
||||
昝: 'zǎn',
|
||||
管: 'guǎn',
|
||||
卢: 'lú',
|
||||
莫: 'mò',
|
||||
经: 'jīng',
|
||||
房: 'fáng',
|
||||
裘: 'qiú',
|
||||
缪: 'miào',
|
||||
干: 'gān',
|
||||
解: 'xiè',
|
||||
应: 'yīng',
|
||||
宗: 'zōng',
|
||||
丁: 'dīng',
|
||||
宣: 'xuān',
|
||||
贲: 'bēn',
|
||||
邓: 'dèng',
|
||||
郁: 'yù',
|
||||
单: 'shàn',
|
||||
杭: 'háng',
|
||||
洪: 'hóng',
|
||||
包: 'bāo',
|
||||
诸: 'zhū',
|
||||
左: 'zuǒ',
|
||||
石: 'shí',
|
||||
崔: 'cuī',
|
||||
吉: 'jí',
|
||||
钮: 'niǔ',
|
||||
龚: 'gōng',
|
||||
程: 'chéng',
|
||||
嵇: 'jī',
|
||||
邢: 'xíng',
|
||||
滑: 'huá',
|
||||
裴: 'péi',
|
||||
陆: 'lù',
|
||||
荣: 'róng',
|
||||
翁: 'wēng',
|
||||
荀: 'xún',
|
||||
羊: 'yáng',
|
||||
於: 'yū',
|
||||
惠: 'huì',
|
||||
甄: 'zhēn',
|
||||
曲: 'qū',
|
||||
家: 'jiā',
|
||||
封: 'fēng',
|
||||
芮: 'ruì',
|
||||
羿: 'yì',
|
||||
储: 'chǔ',
|
||||
靳: 'jìn',
|
||||
汲: 'jí',
|
||||
邴: 'bǐng',
|
||||
糜: 'mí',
|
||||
松: 'sōng',
|
||||
井: 'jǐng',
|
||||
段: 'duàn',
|
||||
富: 'fù',
|
||||
巫: 'wū',
|
||||
乌: 'wū',
|
||||
焦: 'jiāo',
|
||||
巴: 'bā',
|
||||
弓: 'gōng',
|
||||
牧: 'mù',
|
||||
隗: 'kuí',
|
||||
山: 'shān',
|
||||
谷: 'gǔ',
|
||||
车: 'chē',
|
||||
侯: 'hóu',
|
||||
宓: 'mì',
|
||||
蓬: 'péng',
|
||||
全: 'quán',
|
||||
郗: 'xī',
|
||||
班: 'bān',
|
||||
仰: 'yǎng',
|
||||
秋: 'qiū',
|
||||
仲: 'zhòng',
|
||||
伊: 'yī',
|
||||
宫: 'gōng',
|
||||
宁: 'nìng',
|
||||
仇: 'qiú',
|
||||
栾: 'luán',
|
||||
暴: 'bào',
|
||||
甘: 'gān',
|
||||
钭: 'tǒu',
|
||||
厉: 'lì',
|
||||
戎: 'róng',
|
||||
祖: 'zǔ',
|
||||
武: 'wǔ',
|
||||
符: 'fú',
|
||||
刘: 'liú',
|
||||
景: 'jǐng',
|
||||
詹: 'zhān',
|
||||
束: 'shù',
|
||||
龙: 'lóng',
|
||||
叶: 'yè',
|
||||
幸: 'xìng',
|
||||
司: 'sī',
|
||||
韶: 'sháo',
|
||||
郜: 'gào',
|
||||
黎: 'lí',
|
||||
蓟: 'jì',
|
||||
薄: 'bó',
|
||||
印: 'yìn',
|
||||
宿: 'sù',
|
||||
白: 'bái',
|
||||
怀: 'huái',
|
||||
蒲: 'pú',
|
||||
邰: 'tái',
|
||||
从: 'cóng',
|
||||
鄂: 'è',
|
||||
索: 'suǒ',
|
||||
咸: 'xián',
|
||||
籍: 'jí',
|
||||
赖: 'lài',
|
||||
卓: 'zhuó',
|
||||
蔺: 'lìn',
|
||||
屠: 'tú',
|
||||
蒙: 'méng',
|
||||
池: 'chí',
|
||||
乔: 'qiáo',
|
||||
阴: 'yīn',
|
||||
鬱: 'yù',
|
||||
胥: 'xū',
|
||||
能: 'nài',
|
||||
苍: 'cāng',
|
||||
双: 'shuāng',
|
||||
闻: 'wén',
|
||||
莘: 'shēn',
|
||||
党: 'dǎng',
|
||||
翟: 'zhái',
|
||||
谭: 'tán',
|
||||
贡: 'gòng',
|
||||
劳: 'láo',
|
||||
逄: 'páng',
|
||||
姬: 'jī',
|
||||
申: 'shēn',
|
||||
扶: 'fú',
|
||||
堵: 'dǔ',
|
||||
冉: 'rǎn',
|
||||
宰: 'zǎi',
|
||||
郦: 'lì',
|
||||
雍: 'yōng',
|
||||
郤: 'xì',
|
||||
璩: 'qú',
|
||||
桑: 'sāng',
|
||||
桂: 'guì',
|
||||
濮: 'pú',
|
||||
牛: 'niú',
|
||||
寿: 'shòu',
|
||||
通: 'tōng',
|
||||
边: 'biān',
|
||||
扈: 'hù',
|
||||
燕: 'yān',
|
||||
冀: 'jì',
|
||||
郏: 'jiá',
|
||||
浦: 'pǔ',
|
||||
尚: 'shàng',
|
||||
农: 'nóng',
|
||||
温: 'wēn',
|
||||
别: 'bié',
|
||||
庄: 'zhuāng',
|
||||
晏: 'yàn',
|
||||
柴: 'chái',
|
||||
瞿: 'qú',
|
||||
阎: 'yán',
|
||||
充: 'chōng',
|
||||
慕: 'mù',
|
||||
连: 'lián',
|
||||
茹: 'rú',
|
||||
习: 'xí',
|
||||
宦: 'huàn',
|
||||
艾: 'ài',
|
||||
鱼: 'yú',
|
||||
容: 'róng',
|
||||
向: 'xiàng',
|
||||
古: 'gǔ',
|
||||
易: 'yì',
|
||||
慎: 'shèn',
|
||||
戈: 'gē',
|
||||
廖: 'liào',
|
||||
庾: 'yǔ',
|
||||
终: 'zhōng',
|
||||
暨: 'jì',
|
||||
居: 'jū',
|
||||
衡: 'héng',
|
||||
步: 'bù',
|
||||
都: 'dū',
|
||||
耿: 'gěng',
|
||||
满: 'mǎn',
|
||||
弘: 'hóng',
|
||||
匡: 'kuāng',
|
||||
国: 'guó',
|
||||
文: 'wén',
|
||||
寇: 'kòu',
|
||||
广: 'guǎng',
|
||||
禄: 'lù',
|
||||
阙: 'quē',
|
||||
东: 'dōng',
|
||||
欧: 'ōu',
|
||||
殳: 'shū',
|
||||
沃: 'wò',
|
||||
利: 'lì',
|
||||
蔚: 'wèi',
|
||||
越: 'yuè',
|
||||
夔: 'kuí',
|
||||
隆: 'lóng',
|
||||
师: 'shī',
|
||||
巩: 'gǒng',
|
||||
厍: 'shè',
|
||||
聂: 'niè',
|
||||
晁: 'cháo',
|
||||
勾: 'gōu',
|
||||
敖: 'áo',
|
||||
融: 'róng',
|
||||
冷: 'lěng',
|
||||
訾: 'zǐ',
|
||||
辛: 'xīn',
|
||||
阚: 'kàn',
|
||||
那: 'nā',
|
||||
简: 'jiǎn',
|
||||
饶: 'ráo',
|
||||
空: 'kōng',
|
||||
曾: 'zēng',
|
||||
母: 'mǔ',
|
||||
沙: 'shā',
|
||||
乜: 'niè',
|
||||
养: 'yǎng',
|
||||
鞠: 'jū',
|
||||
须: 'xū',
|
||||
丰: 'fēng',
|
||||
巢: 'cháo',
|
||||
关: 'guān',
|
||||
蒯: 'kuǎi',
|
||||
相: 'xiàng',
|
||||
查: 'zhā',
|
||||
后: 'hòu',
|
||||
荆: 'jīng',
|
||||
红: 'hóng',
|
||||
游: 'yóu',
|
||||
竺: 'zhú',
|
||||
权: 'quán',
|
||||
逯: 'lù',
|
||||
盖: 'gài',
|
||||
益: 'yì',
|
||||
桓: 'huán',
|
||||
公: 'gōng',
|
||||
牟: 'móu',
|
||||
哈: 'hǎ',
|
||||
言: 'yán',
|
||||
福: 'fú',
|
||||
};
|
||||
|
||||
export default Surnames;
|
||||
|
||||
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/pinyin-pro.js
vendored
2
dist/pinyin-pro.js
vendored
File diff suppressed because one or more lines are too long
226
docs/3.4.x.md
Normal file
226
docs/3.4.x.md
Normal file
@@ -0,0 +1,226 @@
|
||||
[](https://github.com/zh-lx/pinyin-pro)
|
||||
|
||||
[](https://www.npmjs.com/package/pinyin-pro)
|
||||
[](https://github.com/zh-lx/pinyin-pro)
|
||||
[](https://travis-ci.com/github/zh-lx/pinyin-pro)
|
||||
[](https://npmcharts.com/compare/pinyin-pro?minimal=true)
|
||||
[](https://coveralls.io/github/zh-lx/pinyin-pro?branch=main)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://github.com/zh-lx/pinyin-pro)
|
||||
|
||||
## 特色功能
|
||||
|
||||
- 支持汉字、词语、句子多种格式输入获取
|
||||
- 获取拼音
|
||||
- 获取声母
|
||||
- 获取韵母
|
||||
- 获取拼音首字母
|
||||
- 获取音调
|
||||
- 获取多音字的多种拼音
|
||||
- 支持人名姓氏模式
|
||||
- 支持自定义拼音
|
||||
- 支持字符串和数组两种输出形式
|
||||
|
||||
## 版本更新
|
||||
|
||||
当前版本: 3.3.1 -> 3.4.0
|
||||
|
||||
- 新增功能
|
||||
- 支持姓氏模式拼音识别,具体用法见[姓氏模式](#surname)
|
||||
- 支持自定义拼音功能,具体用法见[自定义拼音](#custom)
|
||||
- 问题修复
|
||||
修复文本过长时堆栈溢出问题
|
||||
- 字音校准
|
||||
- 褚: 'zhǔ' -> 'chǔ zhǔ'
|
||||
- 俞: 'yù' -> 'yú yù'
|
||||
- 臧: 'zàng' -> 'zāng'
|
||||
- 贲: 'bì' -> 'bēn bì'
|
||||
- 莘: 'xīn' -> 'shēn xīn'
|
||||
- 郦: 'zhí' -> 'lì zhí'
|
||||
|
||||
点击查看 [版本更新文档](./CHANGELOG.md)
|
||||
|
||||
## 安装
|
||||
|
||||
npm 安装
|
||||
|
||||
```
|
||||
npm install pinyin-pro
|
||||
```
|
||||
|
||||
yarn 安装
|
||||
|
||||
```
|
||||
yarn add pinyin-pro
|
||||
```
|
||||
|
||||
## 引入
|
||||
|
||||
浏览器 script 引入:
|
||||
|
||||
```html
|
||||
<!--引入某个版本,如3.3.0版本-->
|
||||
<!-- <script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@3.3.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 引入:
|
||||
|
||||
```javascript
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
|
||||
```
|
||||
|
||||
commonjs 引入:
|
||||
|
||||
```javascript
|
||||
const { pinyin } = require('pinyin-pro');
|
||||
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
|
||||
```
|
||||
|
||||
## 参数
|
||||
|
||||
`pinyin(word, options)` 接收两个参数<br>
|
||||
|
||||
- <b>word:</b>必填。String 类型,需要转化为拼音的中文
|
||||
- <b>options:</b>可选。Object 类型,用于配置各种输出形式,options 的键值配置如下:
|
||||
|
||||
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
||||
| -------- | ------------------------------------------------------------- | ------- | -------------------------------------- | ------ |
|
||||
| pattern | 输出的结果的信息(拼音 / 声母 / 韵母 / 音调 / 首字母) | string | pinyin / initial / final / num / first | pinyin |
|
||||
| toneType | 音调输出形式(拼音符号 / 数字 / 不加音调) | string | symbol / num / none | symbol |
|
||||
| type | 输出结果类型(字符串/数组) | string | string / array | string |
|
||||
| multiple | 输出多音字全部拼音(仅在 word 为长度为 1 的汉字字符串时生效) | boolean | true / false | false |
|
||||
| mode | 拼音查找的模式(常规模式 / 姓氏模式) | string | normal / surname | normal |
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 获取拼音
|
||||
|
||||
```js
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 获取带音调拼音
|
||||
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
|
||||
// 获取不带声调的拼音
|
||||
pinyin('汉语拼音', { toneType: 'none' }); // 'han yu pin yin'
|
||||
// 获取声调转换为数字后缀的拼音
|
||||
pinyin('汉语拼音', { toneType: 'num' }); // 'han4 yu3 pin1 yin1'
|
||||
// 获取数组形式带音调拼音
|
||||
pinyin('汉语拼音', { type: 'array' }); // ["hàn", "yǔ", "pīn", "yīn"]
|
||||
// 获取数组形式不带声调的拼音
|
||||
pinyin('汉语拼音', { toneType: 'none', type: 'array' }); // ["han", "yu", "pin", "yin"]
|
||||
// 获取数组形式声调转换为数字后缀的拼音
|
||||
pinyin('汉语拼音', { toneType: 'num', type: 'array' }); // ["han4", "yu3", "pin1", "yin1"]
|
||||
```
|
||||
|
||||
### 获取声母
|
||||
|
||||
```js
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 获取声母
|
||||
pinyin('汉语拼音', { pattern: 'initial' }); // 'h y p y'
|
||||
// 获取数组形式声母
|
||||
pinyin('汉语拼音', { pattern: 'initial', type: 'array' }); // ["h", "y", "p", "y"]
|
||||
```
|
||||
|
||||
### 获取韵母
|
||||
|
||||
```js
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 获取带音调韵母
|
||||
pinyin('汉语拼音', { pattern: 'final' }); // 'àn ǔ īn īn'
|
||||
// 获取不带音调韵母
|
||||
pinyin('汉语拼音', { pattern: 'final', toneType: 'none' }); // 'an u in in'
|
||||
// 获取音调为数字的韵母
|
||||
pinyin('汉语拼音', { pattern: 'final', toneType: 'num' }); // 'an4 u3 in1 in1'
|
||||
// 获取数组形式带音调韵母
|
||||
pinyin('汉语拼音', { pattern: 'final', type: 'array' }); // ["àn", "ǔ", "īn", "īn"]
|
||||
// 获取数组形式不带音调韵母
|
||||
pinyin('汉语拼音', { pattern: 'final', toneType: 'none', type: 'array' }); // ["an", "u", "in", "in"]
|
||||
// 获取数组形式音调为数字的韵母
|
||||
pinyin('汉语拼音', { pattern: 'final', toneType: 'num', type: 'array' }); // ['an4', 'u3', 'in1', 'in1']
|
||||
```
|
||||
|
||||
### 获取音调
|
||||
|
||||
```js
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 获取音调
|
||||
pinyin('汉语拼音', { pattern: 'num' }); // '4 3 1 1'
|
||||
// 获取数组形式音调
|
||||
pinyin('汉语拼音', { pattern: 'num', type: 'array' }); // ["4", "3", "1", "1"]
|
||||
```
|
||||
|
||||
### 获取拼音首字母
|
||||
|
||||
```js
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 获取拼音首字母
|
||||
pinyin('赵钱孙李额', { pattern: 'first' }); // 'z q s l é'
|
||||
// 获取不带音调拼音首字母
|
||||
pinyin('赵钱孙李额', { pattern: 'first', toneType: 'none' }); // 'z q s l e'
|
||||
// 获取数组形式拼音首字母
|
||||
pinyin('赵钱孙李额', { pattern: 'first', type: 'array' }); // ['z', 'q', 's', 'l', 'é']
|
||||
// 获取数组形式不带音调拼音首字母
|
||||
pinyin('赵钱孙李额', { pattern: 'first', toneType: 'none', type: 'array' }); // ['z', 'q', 's', 'l', 'e']
|
||||
```
|
||||
|
||||
### 获取单个字的多音
|
||||
|
||||
只有单字可以获取到多音模式, 词语、句子无效。同样可以通过配置 options 选项获取数组形式、韵母等格式
|
||||
|
||||
```javascript
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 获取多音
|
||||
pinyin('好', { multiple: true }); // 'hǎo hào'
|
||||
// 获取数组形式多音
|
||||
pinyin('好', { multiple: true, type: 'array' }); // ["hǎo", "hào"]
|
||||
```
|
||||
|
||||
### <a id="surname">姓氏模式</a>
|
||||
|
||||
通过设置 `mode: 'surname'` 开启姓氏模式后,匹配到百家姓中的姓氏优先输出姓氏拼音
|
||||
|
||||
```javascript
|
||||
import { pinyin } from 'pinyin-pro';
|
||||
|
||||
// 不开启姓氏模式
|
||||
pinyin('我叫曾小贤'); // 'wǒ jiào céng xiǎo xián'
|
||||
|
||||
// 开启姓氏模式
|
||||
pinyin('我叫曾小贤', { mode: 'surname' }); // 'wǒ jiào zēng xiǎo xián'
|
||||
```
|
||||
|
||||
### <a id="custom">自定义拼音</a>
|
||||
|
||||
包内部导出了 `customPinyin` 方法,支持用户自定义设置词句拼音,当中文中匹配用户自己定义的词句拼音时,优先使用用户自定义的词句拼音
|
||||
|
||||
```javascript
|
||||
import { pinyin, customPinyin } from 'pinyin-pro';
|
||||
|
||||
customPinyin({
|
||||
干一行行一行: 'gàn yī háng xíng yī háng',
|
||||
});
|
||||
pinyin('干一行行一行'); // 'gàn yī háng xíng yī háng'
|
||||
```
|
||||
|
||||
## 贡献与反馈
|
||||
|
||||
参与开源贡献请参照 [pinyin-pro 贡献](./docs/contribute.md)
|
||||
|
||||
使用遇到问题或者需要功能支持欢迎提 issue。
|
||||
|
||||
交流及参与贡献欢迎加微信:
|
||||
|
||||

|
||||
@@ -6,7 +6,7 @@ import _DICT5 from '../data/dict5';
|
||||
import _INITIAL_LIST from '../data/initial';
|
||||
const _dictArr = [{}, {}, _DICT2, _DICT3, _DICT4, _DICT5];
|
||||
|
||||
function customPinyin(config: { [key: string]: string } = {}) {
|
||||
export function customPinyin(config: { [key: string]: string } = {}) {
|
||||
for (let key in config) {
|
||||
let pinyin = config[key];
|
||||
if (key.length === 1) {
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
// import { DICT1 } from '../data/dict1';
|
||||
// import { DICT2 } from '../data/dict2';
|
||||
// import { DICT3 } from '../data/dict3';
|
||||
// import { DICT4 } from '../data/dict4';
|
||||
// import { DICT5 } from '../data/dict5';
|
||||
import INITIAL_LIST from '../data/initial';
|
||||
// const dictArr = [{}, {}, DICT2, DICT3, DICT4, DICT5];
|
||||
import Surnames from '../data/surname';
|
||||
import { DICT1, dictArr } from './data';
|
||||
|
||||
/**
|
||||
* @description: 获取单个字符的拼音
|
||||
@@ -29,9 +25,14 @@ const getSingleWordPinyin: GetSingleWordPinyin = (word) => {
|
||||
type GetPinYin = (
|
||||
word: string,
|
||||
length: number,
|
||||
mode?: 'default' | 'surname'
|
||||
mode?: 'normal' | 'surname'
|
||||
) => string;
|
||||
const getPinyin: GetPinYin = (word, length, mode = 'default') => {
|
||||
const getPinyin: GetPinYin = (word, length, mode = 'normal') => {
|
||||
// 如果是姓氏模式,则优先替换姓氏拼音
|
||||
if (mode === 'surname') {
|
||||
return getSurnamePinyin(word);
|
||||
}
|
||||
|
||||
// 若length值大于5,返回getPinyin(word, 5)
|
||||
if (length > 5) {
|
||||
return getPinyin(word, 5);
|
||||
@@ -91,9 +92,27 @@ const getPinyin: GetPinYin = (word, length, mode = 'default') => {
|
||||
* @param {string} pinyin
|
||||
* @return {string}
|
||||
*/
|
||||
type GetSurnamePinyin = (pinyin: string) => string;
|
||||
const getSurnamePinyin: GetSurnamePinyin = (pinyin) => {
|
||||
return '';
|
||||
type GetSurnamePinyin = (word: string) => string;
|
||||
const getSurnamePinyin: GetSurnamePinyin = (word) => {
|
||||
let _word = word;
|
||||
for (let key in Surnames) {
|
||||
let index = _word.indexOf(key);
|
||||
if (index > -1) {
|
||||
const left_word = word.slice(0, index);
|
||||
const left_pinyin = left_word
|
||||
? `${getPinyin(left_word, left_word.length)} `
|
||||
: '';
|
||||
// 取出该词后右边拼音
|
||||
const right_word = word.slice(index + key.length);
|
||||
const right_pinyin = right_word
|
||||
? ` ${getPinyin(right_word, right_word.length, 'surname')}`
|
||||
: '';
|
||||
// 取出的词的拼音
|
||||
const word_pinyin = Surnames[key];
|
||||
return `${left_pinyin}${word_pinyin}${right_pinyin}`;
|
||||
}
|
||||
}
|
||||
return getPinyin(word, word.length);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export { pinyinFn as pinyin } from './pinyin';
|
||||
export { pinyin } from './pinyin';
|
||||
export { customPinyin } from './data';
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
getPinyinWithNum,
|
||||
getFirstLetter,
|
||||
} from './handle';
|
||||
import Surnames from '../data/surname';
|
||||
export { DICT1, dictArr } from './data';
|
||||
|
||||
const DEFAULT_OPTIONS: {
|
||||
@@ -24,7 +23,7 @@ const DEFAULT_OPTIONS: {
|
||||
mode: 'normal',
|
||||
};
|
||||
|
||||
function pinyinFn(
|
||||
function pinyin(
|
||||
word: string,
|
||||
options?: {
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
@@ -34,7 +33,7 @@ function pinyinFn(
|
||||
mode?: 'normal' | 'surname';
|
||||
}
|
||||
): string;
|
||||
function pinyinFn(
|
||||
function pinyin(
|
||||
word: string,
|
||||
options?: {
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
@@ -45,7 +44,7 @@ function pinyinFn(
|
||||
}
|
||||
): string[];
|
||||
|
||||
function pinyinFn(word: string, options = DEFAULT_OPTIONS): string | string[] {
|
||||
function pinyin(word: string, options = DEFAULT_OPTIONS): string | string[] {
|
||||
// word传入类型错误时
|
||||
if (typeof word !== 'string') {
|
||||
return word;
|
||||
@@ -56,7 +55,7 @@ function pinyinFn(word: string, options = DEFAULT_OPTIONS): string | string[] {
|
||||
}
|
||||
|
||||
// 获取原始拼音
|
||||
let pinyin = getPinyin(word, word.length);
|
||||
let pinyin = getPinyin(word, word.length, options.mode);
|
||||
|
||||
// 对multiple进行处理
|
||||
if (word.length === 1 && options.multiple) {
|
||||
@@ -100,4 +99,4 @@ function pinyinFn(word: string, options = DEFAULT_OPTIONS): string | string[] {
|
||||
return options.type === 'array' ? pinyin.split(' ') : pinyin;
|
||||
}
|
||||
|
||||
export { pinyinFn };
|
||||
export { pinyin };
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pinyin-pro",
|
||||
"version": "3.3.1",
|
||||
"version": "3.4.0",
|
||||
"description": "汉字转拼音库。获取中文拼音、韵母、声母、声调、首字母等信息。",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./types/lib/index.d.ts",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const { pinyin } = require('../dist/index');
|
||||
const { pinyin, customPinyin } = require('../dist/index');
|
||||
const expect = require('chai').expect;
|
||||
|
||||
describe('aggregate', () => {
|
||||
@@ -244,6 +244,66 @@ describe('getNumOfTone', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('surname', () => {
|
||||
it('multiple surname1', () => {
|
||||
const result = pinyin('万俟', { mode: 'surname' });
|
||||
expect(result).to.be.equal('mò qí');
|
||||
});
|
||||
|
||||
it('multiple surname2', () => {
|
||||
const result = pinyin('我叫令狐冲', { mode: 'surname' });
|
||||
expect(result).to.be.equal('wǒ jiào líng hú chōng');
|
||||
});
|
||||
});
|
||||
|
||||
describe('customConfig', () => {
|
||||
it('custom none', () => {
|
||||
customPinyin();
|
||||
const result = pinyin('干一行行一行');
|
||||
expect(result).to.be.equal('gān yī xíng xíng yī xíng');
|
||||
});
|
||||
|
||||
it('custom1', () => {
|
||||
customPinyin({
|
||||
能: 'nài',
|
||||
});
|
||||
const result = pinyin('我姓能');
|
||||
expect(result).to.be.equal('wǒ xìng nài');
|
||||
});
|
||||
|
||||
it('custom2', () => {
|
||||
customPinyin({
|
||||
好好: 'hào hǎo',
|
||||
});
|
||||
const result = pinyin('爱好好多');
|
||||
expect(result).to.be.equal('ài hào hǎo duō');
|
||||
});
|
||||
|
||||
it('custom3', () => {
|
||||
customPinyin({
|
||||
哈什玛: 'hà shén mǎ',
|
||||
});
|
||||
const result = pinyin('哈什玛');
|
||||
expect(result).to.be.equal('hà shén mǎ');
|
||||
});
|
||||
|
||||
it('custom4', () => {
|
||||
customPinyin({
|
||||
暴虎冯河: 'bào hǔ píng hé',
|
||||
});
|
||||
const result = pinyin('暴虎冯河');
|
||||
expect(result).to.be.equal('bào hǔ píng hé');
|
||||
});
|
||||
|
||||
it('custom>5', () => {
|
||||
customPinyin({
|
||||
干一行行一行: 'gàn yī háng xíng yī háng',
|
||||
});
|
||||
const result = pinyin('干一行行一行');
|
||||
expect(result).to.be.equal('gàn yī háng xíng yī háng');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pinyinFn', () => {
|
||||
it('not string type', () => {
|
||||
const result = pinyin(2222);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"lib": ["ES2021"],
|
||||
"module": "esnext",
|
||||
"strict": true,
|
||||
"jsx": "preserve",
|
||||
|
||||
3
types/data/dict1.d.ts
vendored
3
types/data/dict1.d.ts
vendored
@@ -1 +1,2 @@
|
||||
export declare const DICT1: string[];
|
||||
declare const DICT1: string[];
|
||||
export default DICT1;
|
||||
|
||||
3
types/data/dict2.d.ts
vendored
3
types/data/dict2.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
export declare const DICT2: {
|
||||
declare const DICT2: {
|
||||
[prop: string]: string;
|
||||
};
|
||||
export default DICT2;
|
||||
|
||||
3
types/data/dict3.d.ts
vendored
3
types/data/dict3.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
export declare const DICT3: {
|
||||
declare const DICT3: {
|
||||
[prop: string]: string;
|
||||
};
|
||||
export default DICT3;
|
||||
|
||||
3
types/data/dict4.d.ts
vendored
3
types/data/dict4.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
export declare const DICT4: {
|
||||
declare const DICT4: {
|
||||
[prop: string]: string;
|
||||
};
|
||||
export default DICT4;
|
||||
|
||||
3
types/data/dict5.d.ts
vendored
3
types/data/dict5.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
export declare const DICT5: {
|
||||
declare const DICT5: {
|
||||
[prop: string]: string;
|
||||
};
|
||||
export default DICT5;
|
||||
|
||||
3
types/data/initial.d.ts
vendored
3
types/data/initial.d.ts
vendored
@@ -1 +1,2 @@
|
||||
export declare const INITIAL_LIST: string[];
|
||||
declare const INITIAL_LIST: string[];
|
||||
export default INITIAL_LIST;
|
||||
|
||||
4
types/data/surname.d.ts
vendored
Normal file
4
types/data/surname.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
declare const Surnames: {
|
||||
[key: string]: string;
|
||||
};
|
||||
export default Surnames;
|
||||
7
types/lib/data.d.ts
vendored
Normal file
7
types/lib/data.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
export declare function customPinyin(config?: {
|
||||
[key: string]: string;
|
||||
}): void;
|
||||
export declare const DICT1: string[];
|
||||
export declare const dictArr: {
|
||||
[prop: string]: string;
|
||||
}[];
|
||||
13
types/lib/handle.d.ts
vendored
13
types/lib/handle.d.ts
vendored
@@ -1,18 +1,11 @@
|
||||
/**
|
||||
* @description: 获取长文本字符串带符号音调的拼音
|
||||
* @param {string} word
|
||||
* @param {number} length
|
||||
* @return {string}
|
||||
*/
|
||||
declare type GetTextPinyin = (word: string, length: number) => string;
|
||||
declare const getTextPinyin: GetTextPinyin;
|
||||
/**
|
||||
* @description: 获取字符串带符号音调的拼音
|
||||
* @param {string} word
|
||||
* @param {number} length
|
||||
* @param {string} mode
|
||||
* @return {string}
|
||||
*/
|
||||
declare type GetPinYin = (word: string, length: number) => string;
|
||||
declare type GetPinYin = (word: string, length: number, mode?: 'normal' | 'surname') => string;
|
||||
declare const getPinyin: GetPinYin;
|
||||
/**
|
||||
* @description: 将带音调符号拼音转换为不带音调拼音
|
||||
@@ -59,4 +52,4 @@ declare const getPinyinWithNum: GetPinyinWithNum;
|
||||
*/
|
||||
declare type GetFirstLetter = (pinyin: string) => string;
|
||||
declare const getFirstLetter: GetFirstLetter;
|
||||
export { getPinyin, getTextPinyin, getPinyinWithoutTone, getInitialAndFinal, getMultipleTone, getNumOfTone, getPinyinWithNum, getFirstLetter, };
|
||||
export { getPinyin, getPinyinWithoutTone, getInitialAndFinal, getMultipleTone, getNumOfTone, getPinyinWithNum, getFirstLetter, };
|
||||
|
||||
3
types/lib/index.d.ts
vendored
3
types/lib/index.d.ts
vendored
@@ -1 +1,2 @@
|
||||
export { pinyinFn as pinyin } from './pinyin';
|
||||
export { pinyin } from './pinyin';
|
||||
export { customPinyin } from './data';
|
||||
|
||||
9
types/lib/pinyin.d.ts
vendored
9
types/lib/pinyin.d.ts
vendored
@@ -1,13 +1,16 @@
|
||||
declare function pinyinFn(word: string, options?: {
|
||||
export { DICT1, dictArr } from './data';
|
||||
declare function pinyin(word: string, options?: {
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first';
|
||||
type?: 'string';
|
||||
multiple?: boolean;
|
||||
mode?: 'normal' | 'surname';
|
||||
}): string;
|
||||
declare function pinyinFn(word: string, options?: {
|
||||
declare function pinyin(word: string, options?: {
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first';
|
||||
type: 'array';
|
||||
multiple?: boolean;
|
||||
mode?: 'normal' | 'surname';
|
||||
}): string[];
|
||||
export { pinyinFn };
|
||||
export { pinyin };
|
||||
|
||||
Reference in New Issue
Block a user