parse-pinyin 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,202 +1,252 @@
1
- # parse-pinyin
2
-
3
- [![NPM version](https://img.shields.io/npm/v/parse-pinyin.svg?style=flat-square)](https://npmjs.org/package/parse-pinyin)
4
- [![NPM downloads](https://img.shields.io/npm/dm/parse-pinyin.svg?style=flat-square)](https://npmjs.org/package/parse-pinyin)
5
- [![License](https://img.shields.io/npm/l/parse-pinyin.svg?style=flat-square)](https://github.com/your-username/parse-pinyin/blob/main/LICENSE)
6
-
7
- 一个高效的汉字拼音查询库,使用极致优化的 JSON 格式存储拼音数据,支持多种输出格式和反向查询功能。
8
-
9
- ## 特性
10
-
11
- - 🚀 **高性能**: 预处理数据结构,查询速度快
12
- - 📦 **轻量级**: 无外部依赖,包体积小
13
- - 🌐 **多格式支持**: 支持声调符号、数字声调、数组格式等多种输出
14
- - 🔁 **双向查询**: 支持汉字查拼音和拼音查汉字
15
- - 📱 **跨平台**: 支持 Node.js、浏览器和 Webpack/Vite 等构建工具
16
-
17
- ## 安装
18
-
19
- ```bash
20
- npm install parse-pinyin
21
- ```
22
-
23
- ## 快速开始
24
-
25
- ```javascript
26
- import { toPinyin, toHanzi } from 'parse-pinyin';
27
-
28
- // 汉字转拼音 (默认格式 - 带声调符号)
29
- console.log(toPinyin('中'));
30
- // 输出: ["zhōng", "zhòng"]
31
-
32
- // 汉字转拼音 (数字声调格式)
33
- console.log(toPinyin('中', { toneType: 'number' }));
34
- // 输出: ["zhong1", "zhong4"]
35
-
36
- // 汉字转拼音 (数组格式)
37
- console.log(toPinyin('中', { toneType: 'array' }));
38
- // 输出: [["zhong", 1], ["zhong", 4]]
39
-
40
- // 汉字转拼音 (无音调格式)
41
- console.log(toPinyin('中', { tone: false }));
42
- // 输出: ["zhong", "zhong"]
43
-
44
- // 拼音反查汉字
45
- console.log(toHanzi('xíng'));
46
- // 输出: ["行", "形", "型", ...]
47
- ```
48
-
49
- ## API 参考
50
-
51
- ### `toPinyin(char: string, options?: ToneOptions)`
52
-
53
- 将汉字转换为拼音
54
-
55
- #### 参数
56
-
57
- - `char`: 要查询的汉字 (单个字符)
58
- - `options`: 音调选项
59
- - `tone`: 是否保留音调 (默认: `true`)
60
- - `toneType`: 音调表示方式
61
- - `'symbol'`: 声调符号 (默认)
62
- - `'number'`: 数字声调
63
- - `'array'`: 数组格式 `[拼音, 声调数字]`
64
-
65
- #### 返回值
66
-
67
- - 带音调符号: `string[]`
68
- - 数字声调: `string[]`
69
- - 数组格式: `(string | [string, number])[]`
70
-
71
- ### `toHanzi(pinyin: string)`
72
-
73
- 根据拼音查询匹配的汉字
74
-
75
- #### 参数
76
-
77
- - `pinyin`: 要查询的拼音
78
-
79
- #### 返回值
80
-
81
- - `string[]`: 匹配的汉字数组
82
-
83
- ## 使用示例
84
-
85
- ```javascript
86
- import { toPinyin, toHanzi } from 'parse-pinyin';
87
- // 多音字处理
88
- console.log(toPinyin('中'));
89
- // 输出: ["zhōng", "zhòng"]
90
-
91
- // 不同输出格式
92
- console.log(toPinyin('中', { toneType: 'number' }));
93
- // 输出: ["zhong1", "zhong4"]
94
-
95
- console.log(toPinyin('中', { toneType: 'array' }));
96
- // 输出: [["zhong", 1], ["zhong", 4]]
97
-
98
- console.log(toPinyin('中', { tone: false }));
99
- // 输出: ["zhong", "zhong"]
100
-
101
- // 拼音反查汉字
102
- console.log(toHanzi('zhong'));
103
- // 输出: ["中", "忠", "钟", "终", "种", "踵", "盅", "衷", "冢", "柊", ...]
104
- ```
105
-
106
- ## 性能对比
107
-
108
- 在典型的使用场景中,parse-pinyin 相比其他拼音库具有显著的性能优势:
109
-
110
- ### 与 pinyin-pro 对比
111
-
112
- 实际测试结果(硬件:Apple M1 Pro,Node.js v18.17.0):
113
-
114
- ```javascript
115
- // 测试代码
116
- import { toPinyin } from 'parse-pinyin';
117
- import { pinyin } from 'pinyin-pro';
118
-
119
- const testChars = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十',
120
- '中', '国', '人', '民', '大', '家', '好', '学', '习', '天',
121
- '行', '动', '快', '乐', '幸', '福', '健', '康', '安', '全'];
122
-
123
- console.time('parse-pinyin');
124
- for (let i = 0; i < 10000; i++) {
125
- testChars.forEach(char => {
126
- toPinyin(char);
127
- });
128
- }
129
- console.timeEnd('parse-pinyin');
130
- // 输出: parse-pinyin: 13.269ms
131
-
132
- console.time('pinyin-pro');
133
- for (let i = 0; i < 10000; i++) {
134
- testChars.forEach(char => {
135
- pinyin(char, { toneType: 'symbol' });
136
- });
137
- }
138
- console.timeEnd('pinyin-pro');
139
- // 输出: pinyin-pro: 166.447ms
140
- ```
141
-
142
- ### 性能测试结果
143
-
144
- | 测试项目 | parse-pinyin | pinyin-pro | 性能提升 |
145
- |---------|-------------|------------|---------|
146
- | 单字符拼音转换 (10,000次) | 13.27ms | 166.45ms | 12.54x |
147
- | 批量处理 (100,000字符) | ~130ms | ~1660ms | 12.77x |
148
-
149
- ### 文件大小
150
-
151
- | parse-pinyin | pinyin-pro |
152
- |-------------|------------|
153
- | 229 kB | 323 kB |
154
-
155
-
156
- ## 浏览器使用
157
-
158
- ```html
159
- <script src="https://unpkg.com/parse-pinyin@latest/dist/index.global.js"></script>
160
- <script>
161
- console.log(pinyin.toPinyin('')); // ["zhōng", "zhòng"]
162
- console.log(pinyin.toHanzi('xíng')); // ["行", "形", "型", ...]
163
- </script>
164
- ```
165
-
166
- ## Node.js 使用
167
-
168
- ```javascript
169
- const { toPinyin, toHanzi } = require('parse-pinyin');
170
-
171
- console.log(toPinyin('中')); // ["zhōng", "zhòng"]
172
- console.log(toHanzi('xíng')); // ["行", "形", "型", ...]
173
- ```
174
-
175
- ## 构建和开发
176
-
177
- ```bash
178
- # 安装依赖
179
- npm install
180
-
181
- # 构建项目
182
- npm run build
183
-
184
- # 运行测试
185
- npm test
186
-
187
- # 运行基准测试
188
- npm run bench
189
- ```
190
-
191
- ## 数据来源
192
-
193
- 拼音数据来源于 CC-CEDICT 词典,经过预处理和优化以适应高性能查询需求。
194
-
195
- ## 许可证
196
-
197
- MIT
198
-
199
- ## 贡献
200
-
201
- 欢迎提交 Issue 和 Pull Request 来改进这个项目!
202
- ```
1
+ # parse-pinyin
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/parse-pinyin.svg?style=flat-square)](https://npmjs.org/package/parse-pinyin)
4
+ [![NPM downloads](https://img.shields.io/npm/dm/parse-pinyin.svg?style=flat-square)](https://npmjs.org/package/parse-pinyin)
5
+ [![License](https://img.shields.io/npm/l/parse-pinyin.svg?style=flat-square)](https://github.com/your-username/parse-pinyin/blob/main/LICENSE)
6
+
7
+ 一个高效的汉字拼音查询库,使用极致优化的 JSON 格式存储拼音数据,支持多种输出格式和反向查询功能,并提供拼音解析为声母、韵母和声调的能力。
8
+
9
+ ## 特性
10
+
11
+ - 🚀 **高性能**: 预处理数据结构,查询速度快
12
+ - 📦 **轻量级**: 无外部依赖,包体积小
13
+ - 🌐 **多格式支持**: 支持声调符号、数字声调、数组格式等多种输出
14
+ - 🔁 **双向查询**: 支持汉字查拼音和拼音查汉字
15
+ - 🧩 **混合输入支持**: 支持包含汉字和非汉字字符的字符串处理
16
+ - 🔍 **拼音解析**: 支持将拼音解析为声母、韵母和声调、韵母类型、整体认读类型
17
+ - 📱 **跨平台**: 支持 Node.js、浏览器和 Webpack/Vite 等构建工具
18
+
19
+ ## 安装
20
+
21
+ ```bash
22
+ npm install parse-pinyin
23
+ ```
24
+
25
+ ## 快速开始
26
+
27
+ ```javascript
28
+ import { toPinyin, toHanzi, parse } from 'parse-pinyin';
29
+
30
+ // 汉字转拼音 (默认格式 - 带声调符号)
31
+ console.log(toPinyin('中'));
32
+ // 输出: ["zhōng", "zhòng"]
33
+
34
+ // 汉字转拼音 (数字声调格式)
35
+ console.log(toPinyin('中', { toneType: 'number' }));
36
+ // 输出: ["zhong1", "zhong4"]
37
+
38
+ // 汉字转拼音 (数组格式)
39
+ console.log(toPinyin('中', { toneType: 'array' }));
40
+ // 输出: [["zhong", 1], ["zhong", 4]]
41
+
42
+ // 汉字转拼音 (无音调格式)
43
+ console.log(toPinyin('中', { tone: false }));
44
+ // 输出: ["zhong", "zhong"]
45
+
46
+ // 拼音反查汉字
47
+ console.log(toHanzi('xíng'));
48
+ // 输出: ["行", "形", "型", ...]
49
+
50
+ // 解析拼音为声母、韵母和声调
51
+ console.log(parse('zhōng'));
52
+ // 输出: { shengmu: 'zh', yunmu: 'ong', shengdiao: 1 }
53
+ console.log(parse('ān'));
54
+ // 输出: { shengmu: '', yunmu: 'an', shengdiao: 1 }
55
+ console.log(parse('yi'));
56
+ // 输出: { shengmu: 'y', yunmu: 'i', shengdiao: 0 }
57
+ console.log(parse('invalid'));
58
+ // 输出: null
59
+ ```
60
+
61
+ ## API 参考
62
+
63
+ ### `toPinyin(char: string, options?: ToneOptions)`
64
+
65
+ 将汉字或字符串转换为拼音。
66
+
67
+ #### 参数
68
+
69
+ - `char`: 输入的字符或字符串(可以包含非汉字字符)
70
+ - `options`: 音调选项
71
+ - `tone`: 是否保留音调 (默认: `true`)
72
+ - `toneType`: 音调表示方式
73
+ - `'symbol'`: 声调符号 (默认)
74
+ - `'number'`: 数字声调
75
+ - `'array'`: 数组格式 `[拼音, 声调数字]`
76
+
77
+ #### 返回值
78
+
79
+ - 如果输入是单个汉字,返回其拼音数组。
80
+ - 如果输入是字符串,返回一个混合数组(汉字对应的拼音数组 + 非汉字字符)。
81
+
82
+ #### 示例
83
+
84
+ ```javascript
85
+ // 单个汉字
86
+ console.log(toPinyin(''));
87
+ // 输出: ["zhōng", "zhòng"]
88
+
89
+ // 包含非汉字字符的字符串
90
+ console.log(toPinyin('Hello, 世界'));
91
+ // 输出: ['H', 'e', 'l', 'l', 'o', ',', ' ', ['shì'], ['jiè']]
92
+ ```
93
+
94
+ ---
95
+
96
+ ### `toHanzi(pinyin: string)`
97
+
98
+ 根据拼音查询匹配的汉字。
99
+
100
+ #### 参数
101
+
102
+ - `pinyin`: 要查询的拼音(可以带声调符号)
103
+
104
+ #### 返回值
105
+
106
+ - `string[]`: 匹配该拼音的所有汉字组成的数组
107
+
108
+ #### 示例
109
+
110
+ ```javascript
111
+ console.log(toHanzi('zhōng'));
112
+ // 输出: ['中']
113
+ console.log(toHanzi('yī'));
114
+ // 输出: ['一', '衣', ...]
115
+ ```
116
+
117
+ ---
118
+
119
+ ### `parse(pinyin: string)`
120
+
121
+ 解析拼音为声母、韵母和声调。
122
+
123
+ #### 参数
124
+
125
+ - `pinyin`: 要解析的拼音(可以带声调符号)
126
+
127
+ #### 返回值
128
+
129
+ - `{ shengmu: string, yunmu: string, shengdiao: number } | null`
130
+ - `shengmu`: 声母部分
131
+ - `yunmu`: 韵母部分
132
+ - `shengdiao`: 声调数字(1-5,5 表示轻声)
133
+
134
+ 如果输入无效或无法解析,则返回 `null`。
135
+
136
+ #### 示例
137
+
138
+ ```javascript
139
+ console.log(parse('zhōng'));
140
+ // 输出: { shengmu: 'zh', yunmu: 'ong', shengdiao: 1,yunmuType: '后鼻韵母'}
141
+ console.log(parse('ān'));
142
+ // 输出: { shengmu: '', yunmu: 'an', shengdiao: 1,yunmuType: '前鼻韵母', }
143
+ console.log(parse('yi'));
144
+ // 输出: { shengmu: 'y', yunmu: 'i', shengdiao: 0,zhengtirendu: '舌面音及其他' }
145
+ console.log(parse('invalid'));
146
+ // 输出: null
147
+ ```
148
+
149
+ ---
150
+
151
+ ## 性能对比
152
+
153
+ 在典型的使用场景中,`parse-pinyin` 相比其他拼音库具有显著的性能优势:
154
+
155
+ ### 与 pinyin-pro 对比
156
+
157
+ 实际测试结果(硬件:Apple M1 Pro,Node.js v18.17.0):
158
+
159
+ ```javascript
160
+ // 测试代码
161
+ import { toPinyin } from 'parse-pinyin';
162
+ import { pinyin } from 'pinyin-pro';
163
+
164
+ const testChars = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十',
165
+ '中', '国', '人', '民', '大', '家', '好', '学', '习', '天',
166
+ '行', '动', '快', '乐', '幸', '福', '健', '康', '安', '全'];
167
+
168
+ console.time('parse-pinyin');
169
+ for (let i = 0; i < 10000; i++) {
170
+ testChars.forEach(char => {
171
+ toPinyin(char);
172
+ });
173
+ }
174
+ console.timeEnd('parse-pinyin');
175
+ // 输出: parse-pinyin: 13.269ms
176
+
177
+ console.time('pinyin-pro');
178
+ for (let i = 0; i < 10000; i++) {
179
+ testChars.forEach(char => {
180
+ pinyin(char, { toneType: 'symbol' });
181
+ });
182
+ }
183
+ console.timeEnd('pinyin-pro');
184
+ // 输出: pinyin-pro: 166.447ms
185
+ ```
186
+
187
+ ### 性能测试结果
188
+
189
+ | 测试项目 | parse-pinyin | pinyin-pro | 性能提升 |
190
+ |----------|--------------|------------|----------|
191
+ | 单字符拼音转换 (10,000次) | 13.27ms | 166.45ms | 12.54x |
192
+ | 批量处理 (100,000字符) | ~130ms | ~1660ms | 12.77x |
193
+
194
+ ### 文件大小
195
+
196
+ | parse-pinyin | pinyin-pro |
197
+ |--------------|------------|
198
+ | 229 kB | 323 kB |
199
+
200
+ ---
201
+
202
+ ## 浏览器使用
203
+
204
+ ```html
205
+ <script src="https://unpkg.com/parse-pinyin@latest/dist/index.global.js"></script>
206
+ <script>
207
+ console.log(pinyin.toPinyin('中')); // ["zhōng", "zhòng"]
208
+ console.log(pinyin.toHanzi('xíng')); // ["行", "形", "型", ...]
209
+ </script>
210
+ ```
211
+
212
+ ---
213
+
214
+ ## Node.js 使用
215
+
216
+ ```javascript
217
+ const { toPinyin, toHanzi, parse } = require('parse-pinyin');
218
+
219
+ console.log(toPinyin('中')); // ["zhōng", "zhòng"]
220
+ console.log(toHanzi('xíng')); // ["行", "形", "型", ...]
221
+ console.log(parse('zhōng')); // { shengmu: 'zh', yunmu: 'ong', shengdiao: 1 }
222
+ ```
223
+
224
+ ---
225
+
226
+ ## 构建和开发
227
+
228
+ ```bash
229
+ # 安装依赖
230
+ npm install
231
+
232
+ # 构建项目
233
+ npm run build
234
+
235
+ # 运行测试
236
+ npm test
237
+
238
+ # 运行基准测试
239
+ npm run bench
240
+ ```
241
+
242
+ ---
243
+
244
+ ## 数据来源
245
+
246
+ 拼音数据来源于 CC-CEDICT 词典,经过预处理和优化以适应高性能查询需求。
247
+
248
+ ---
249
+
250
+ ## 许可证
251
+
252
+ MIT
package/dist/index.d.mts CHANGED
@@ -1,3 +1,15 @@
1
+ type ParsedPinyin = {
2
+ shengmu: string;
3
+ yunmu: string;
4
+ shengdiao: number;
5
+ yunmuType?: string;
6
+ zhengtirendu?: string;
7
+ } | null;
8
+
9
+ /**
10
+ * 极致优化的拼音查询器 - 从极致优化的TS数据中查询拼音数据
11
+ */
12
+
1
13
  /**
2
14
  * 音调转换选项
3
15
  */
@@ -53,8 +65,56 @@ declare class UltraOptimizedPinyinJsonQuery {
53
65
  * @returns 匹配的汉字数组
54
66
  */
55
67
  toHanzi(pinyin: string): string[];
68
+ /**
69
+ * 将拼音解析为声母、韵母和声调
70
+ * @param pinyin 要解析的带音调符号的拼音
71
+ * @returns 解析后的拼音信息 {shengmu, yunmu, shengdiao} 或者 null
72
+ */
73
+ parse(pinyin: string): ParsedPinyin;
74
+ /**
75
+ * 检查是否为整体认读音节
76
+ */
77
+ private isWholeReading;
78
+ /**
79
+ * 提取整体认读音节的声母和韵母
80
+ */
81
+ private extractWholeReadingParts;
82
+ /**
83
+ * 验证拼音是否有效
84
+ */
85
+ private isValidPinyin;
56
86
  }
87
+ /**
88
+ * 导出便捷查询函数
89
+ */
90
+ /**
91
+ * 将汉字转换为拼音
92
+ *
93
+ * @param char 输入的字符或字符串
94
+ * @param options 音调选项,用于控制返回拼音的格式
95
+ * @returns 如果输入是单个汉字,返回其拼音数组;如果输入是字符串,返回一个混合数组(汉字对应的拼音数组 + 非汉字字符)
96
+ */
57
97
  declare const toPinyin: (char: string, options?: ToneOptions) => (string | [string, number])[];
98
+ /**
99
+ * 根据拼音查询所有匹配的汉字
100
+ *
101
+ * @param pinyin 要查询的拼音(可以带声调符号)
102
+ * @returns 匹配该拼音的所有汉字组成的数组
103
+ */
58
104
  declare const toHanzi: (pinyin: string) => string[];
105
+ /**
106
+ * 解析拼音为声母、韵母和声调
107
+ *
108
+ * @param pinyin 要解析的拼音(可以带声调符号)
109
+ * @returns 解析结果对象 `{ shengmu: string, yunmu: string, shengdiao: number }` 或 `null`
110
+ */
111
+ declare const parse: (pinyin: string) => ParsedPinyin;
112
+ /**
113
+ * 检查输入的字符串是否为有效的拼音
114
+ *
115
+ * @param pinyin 要检查的拼音(可以带声调符号)
116
+ * @returns 如果输入的字符串为有效的拼音,返回 `true`;否则返回 `false`
117
+ */
118
+ declare const isPinyin: (pinyin: string) => boolean;
59
119
 
60
- export { type ToneOptions, UltraOptimizedPinyinJsonQuery, toHanzi, toPinyin };
120
+ export { type ToneOptions, UltraOptimizedPinyinJsonQuery, isPinyin, parse, toHanzi, toPinyin };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,15 @@
1
+ type ParsedPinyin = {
2
+ shengmu: string;
3
+ yunmu: string;
4
+ shengdiao: number;
5
+ yunmuType?: string;
6
+ zhengtirendu?: string;
7
+ } | null;
8
+
9
+ /**
10
+ * 极致优化的拼音查询器 - 从极致优化的TS数据中查询拼音数据
11
+ */
12
+
1
13
  /**
2
14
  * 音调转换选项
3
15
  */
@@ -53,8 +65,56 @@ declare class UltraOptimizedPinyinJsonQuery {
53
65
  * @returns 匹配的汉字数组
54
66
  */
55
67
  toHanzi(pinyin: string): string[];
68
+ /**
69
+ * 将拼音解析为声母、韵母和声调
70
+ * @param pinyin 要解析的带音调符号的拼音
71
+ * @returns 解析后的拼音信息 {shengmu, yunmu, shengdiao} 或者 null
72
+ */
73
+ parse(pinyin: string): ParsedPinyin;
74
+ /**
75
+ * 检查是否为整体认读音节
76
+ */
77
+ private isWholeReading;
78
+ /**
79
+ * 提取整体认读音节的声母和韵母
80
+ */
81
+ private extractWholeReadingParts;
82
+ /**
83
+ * 验证拼音是否有效
84
+ */
85
+ private isValidPinyin;
56
86
  }
87
+ /**
88
+ * 导出便捷查询函数
89
+ */
90
+ /**
91
+ * 将汉字转换为拼音
92
+ *
93
+ * @param char 输入的字符或字符串
94
+ * @param options 音调选项,用于控制返回拼音的格式
95
+ * @returns 如果输入是单个汉字,返回其拼音数组;如果输入是字符串,返回一个混合数组(汉字对应的拼音数组 + 非汉字字符)
96
+ */
57
97
  declare const toPinyin: (char: string, options?: ToneOptions) => (string | [string, number])[];
98
+ /**
99
+ * 根据拼音查询所有匹配的汉字
100
+ *
101
+ * @param pinyin 要查询的拼音(可以带声调符号)
102
+ * @returns 匹配该拼音的所有汉字组成的数组
103
+ */
58
104
  declare const toHanzi: (pinyin: string) => string[];
105
+ /**
106
+ * 解析拼音为声母、韵母和声调
107
+ *
108
+ * @param pinyin 要解析的拼音(可以带声调符号)
109
+ * @returns 解析结果对象 `{ shengmu: string, yunmu: string, shengdiao: number }` 或 `null`
110
+ */
111
+ declare const parse: (pinyin: string) => ParsedPinyin;
112
+ /**
113
+ * 检查输入的字符串是否为有效的拼音
114
+ *
115
+ * @param pinyin 要检查的拼音(可以带声调符号)
116
+ * @returns 如果输入的字符串为有效的拼音,返回 `true`;否则返回 `false`
117
+ */
118
+ declare const isPinyin: (pinyin: string) => boolean;
59
119
 
60
- export { type ToneOptions, UltraOptimizedPinyinJsonQuery, toHanzi, toPinyin };
120
+ export { type ToneOptions, UltraOptimizedPinyinJsonQuery, isPinyin, parse, toHanzi, toPinyin };