parse-pinyin 1.3.2 → 1.3.4

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,252 +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
- - 🧩 **混合输入支持**: 支持包含汉字和非汉字字符的字符串处理
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
-
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
252
  MIT