@laisuk/opencc-fmmseg-wasm 0.3.8 → 0.4.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 +809 -731
- package/bin/opencc.js +193 -48
- package/opencc_fmmseg_wasm.d.ts +219 -2
- package/opencc_fmmseg_wasm.js +245 -46
- package/opencc_fmmseg_wasm_bg.wasm +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,731 +1,809 @@
|
|
|
1
|
-
# opencc-fmmseg-wasm
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@laisuk/opencc-fmmseg-wasm)
|
|
4
|
-
[](https://www.npmjs.com/package/@laisuk/opencc-fmmseg-wasm)
|
|
5
|
-
[](LICENSE)
|
|
6
|
-
[](https://webassembly.org/)
|
|
7
|
-
|
|
8
|
-
OpenCC FMM segmentation WebAssembly bindings for browsers and JavaScript runtimes.
|
|
9
|
-
|
|
10
|
-
This package provides high-quality Simplified Chinese ↔ Traditional Chinese conversion powered by the Rust [
|
|
11
|
-
`opencc-fmmseg`](https://github.com/laisuk/opencc-fmmseg) engine.
|
|
12
|
-
|
|
13
|
-
Features:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Package profile:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
---
|
|
36
|
-
|
|
37
|
-
## Installation
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
npm install @laisuk/opencc-fmmseg-wasm
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
---
|
|
44
|
-
|
|
45
|
-
## Quick Start
|
|
46
|
-
|
|
47
|
-
```javascript
|
|
48
|
-
import init, {
|
|
49
|
-
OpenccWasm,
|
|
50
|
-
DetofuLevelWasm
|
|
51
|
-
} from "@laisuk/opencc-fmmseg-wasm";
|
|
52
|
-
|
|
53
|
-
await init();
|
|
54
|
-
|
|
55
|
-
const cc = new OpenccWasm("
|
|
56
|
-
|
|
57
|
-
console.log(cc.convert("
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
console.log(cc.convertDetofu("儼驂騑於上路", false, DetofuLevelWasm.ExtB));
|
|
61
|
-
// 俨骖騑于上路
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
---
|
|
65
|
-
|
|
66
|
-
##
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
Returns
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
```javascript
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
```javascript
|
|
276
|
-
cc.
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
cc.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
| `
|
|
338
|
-
| `
|
|
339
|
-
| `
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
```javascript
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
```
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
```
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
```
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
```
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
```
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
1
|
+
# opencc-fmmseg-wasm
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@laisuk/opencc-fmmseg-wasm)
|
|
4
|
+
[](https://www.npmjs.com/package/@laisuk/opencc-fmmseg-wasm)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://webassembly.org/)
|
|
7
|
+
|
|
8
|
+
OpenCC FMM segmentation WebAssembly bindings for browsers and JavaScript runtimes.
|
|
9
|
+
|
|
10
|
+
This package provides high-quality Simplified Chinese ↔ Traditional Chinese conversion powered by the Rust [
|
|
11
|
+
`opencc-fmmseg`](https://github.com/laisuk/opencc-fmmseg) engine.
|
|
12
|
+
|
|
13
|
+
Features:
|
|
14
|
+
|
|
15
|
+
- OpenCC-compatible conversion configs
|
|
16
|
+
- Pure WebAssembly (no native binaries)
|
|
17
|
+
- Browser-friendly
|
|
18
|
+
- TypeScript-friendly APIs
|
|
19
|
+
- Fast Rust backend
|
|
20
|
+
- FMM-based phrase segmentation
|
|
21
|
+
- Traditional Chinese regional variants
|
|
22
|
+
- Japanese Shinjitai conversion support
|
|
23
|
+
- Chinese script detection (`zho_check`)
|
|
24
|
+
- Optional CJK Compatibility Ideograph normalization
|
|
25
|
+
- In-memory Office / EPUB document conversion
|
|
26
|
+
- Zero-dependency Node.js CLI
|
|
27
|
+
|
|
28
|
+
Package profile:
|
|
29
|
+
|
|
30
|
+
- 0 runtime dependencies
|
|
31
|
+
- 1 WASM file
|
|
32
|
+
- 20 conversion configs
|
|
33
|
+
- 100% offline
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @laisuk/opencc-fmmseg-wasm
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
import init, {
|
|
49
|
+
OpenccWasm,
|
|
50
|
+
DetofuLevelWasm
|
|
51
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
52
|
+
|
|
53
|
+
await init();
|
|
54
|
+
|
|
55
|
+
const cc = new OpenccWasm("t2s");
|
|
56
|
+
|
|
57
|
+
console.log(cc.convert("漢字", false));
|
|
58
|
+
// 汉字
|
|
59
|
+
|
|
60
|
+
console.log(cc.convertDetofu("儼驂騑於上路", false, DetofuLevelWasm.ExtB));
|
|
61
|
+
// 俨骖騑于上路
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
### Constructor
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
const cc = new OpenccWasm("s2t");
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Parameters:
|
|
75
|
+
|
|
76
|
+
- `config` (optional): OpenCC config string
|
|
77
|
+
- default: `"s2t"`
|
|
78
|
+
|
|
79
|
+
Example:
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
const cc = new OpenccWasm("t2s");
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Hong Kong phrase config example:
|
|
86
|
+
|
|
87
|
+
```javascript
|
|
88
|
+
const cc = new OpenccWasm("s2hkp");
|
|
89
|
+
|
|
90
|
+
cc.convert("别随便录影侵犯个人隐私权", false);
|
|
91
|
+
// 別隨便錄影侵犯個人私隱權
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### convert
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
cc.convert(text, punctuation)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Parameters:
|
|
103
|
+
|
|
104
|
+
- `text`: input string
|
|
105
|
+
- `punctuation`: whether to convert punctuation variants
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
|
|
109
|
+
- converted string
|
|
110
|
+
|
|
111
|
+
Example:
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
cc.convert("汉字", false);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
### setConfig
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
cc.setConfig("t2s");
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
|
|
127
|
+
- `true` if valid
|
|
128
|
+
- `false` if invalid
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
### getConfig
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
cc.getConfig();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Returns current config string.
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
### isValidConfig
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
OpenccWasm.isValidConfig("s2t");
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
### getSupportedConfigs
|
|
151
|
+
|
|
152
|
+
```javascript
|
|
153
|
+
OpenccWasm.getSupportedConfigs();
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Returns all supported config strings.
|
|
157
|
+
|
|
158
|
+
Includes `s2hkp`, `hk2sp`, `t2hkp`, and `hk2tp`.
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
### getAvailableSlots
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
const slots = OpenccWasm.getAvailableSlots();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Returns all canonical dictionary slot names accepted by `newWithCustomDicts` as a string array. The list is sourced from
|
|
169
|
+
the core `DictSlot` definitions, so callers can use it to populate selectors or validate custom dictionary input without
|
|
170
|
+
maintaining their own slot list.
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
if (!OpenccWasm.getAvailableSlots().includes(slot)) {
|
|
174
|
+
throw new Error(`Unsupported dictionary slot: ${slot}`);
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
### zhoCheck
|
|
181
|
+
|
|
182
|
+
Detect Chinese script type.
|
|
183
|
+
|
|
184
|
+
```javascript
|
|
185
|
+
cc.zhoCheck(text);
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
|
|
190
|
+
| Value | Meaning |
|
|
191
|
+
|-------|---------------------|
|
|
192
|
+
| `0` | Unknown / mixed |
|
|
193
|
+
| `1` | Traditional Chinese |
|
|
194
|
+
| `2` | Simplified Chinese |
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
### normalizeCompat
|
|
199
|
+
|
|
200
|
+
Normalize Unicode CJK Compatibility Ideographs before conversion.
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
cc.normalizeCompat(text)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Parameters:
|
|
207
|
+
|
|
208
|
+
- `text`: input string
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
|
|
212
|
+
- normalized string
|
|
213
|
+
|
|
214
|
+
Example:
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
const cc = new OpenccWasm("t2s");
|
|
218
|
+
|
|
219
|
+
const input = "天龍八部書裡的喬峰是契丹人";
|
|
220
|
+
const normalized = cc.normalizeCompat(input);
|
|
221
|
+
|
|
222
|
+
console.log(normalized);
|
|
223
|
+
// 天龍八部書裡的喬峰是契丹人
|
|
224
|
+
|
|
225
|
+
console.log(cc.convert(normalized, false));
|
|
226
|
+
// 天龙八部书里的乔峰是契丹人
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
This is an optional pre-conversion pass for text that contains CJK Compatibility Ideographs. Unmapped characters are
|
|
230
|
+
preserved unchanged. Normal OpenCC conversion does not automatically run this pass, so call it explicitly when
|
|
231
|
+
compatibility normalization is desired.
|
|
232
|
+
|
|
233
|
+
### normalizeUnicodeCompat
|
|
234
|
+
|
|
235
|
+
Normalize additional Unicode compatibility forms, CJK radicals, allographs, legacy glyphs, and selected
|
|
236
|
+
compatibility-like punctuation before conversion.
|
|
237
|
+
|
|
238
|
+
```javascript
|
|
239
|
+
cc.normalizeUnicodeCompat(text)
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Parameters:
|
|
243
|
+
|
|
244
|
+
- `text`: input string
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
|
|
248
|
+
- normalized string
|
|
249
|
+
|
|
250
|
+
Example:
|
|
251
|
+
|
|
252
|
+
```javascript
|
|
253
|
+
const cc = new OpenccWasm("t2s");
|
|
254
|
+
|
|
255
|
+
const input = "聼聼竒羙⽟䂖甁噐⾳";
|
|
256
|
+
const normalized = cc.normalizeUnicodeCompat(input);
|
|
257
|
+
|
|
258
|
+
console.log(normalized);
|
|
259
|
+
// 聽聽奇美玉石瓶器音
|
|
260
|
+
|
|
261
|
+
console.log(cc.convert(normalized, false));
|
|
262
|
+
// 听听奇美玉石瓶器音
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
This pass uses the extended Unicode compatibility table and is separate from `normalizeCompat()`. It is useful for text
|
|
266
|
+
containing radical forms, historical or allographic Han forms, and other compatibility-like characters that are not
|
|
267
|
+
covered by the CJK Compatibility Ideograph ranges.
|
|
268
|
+
|
|
269
|
+
Unmapped characters are preserved unchanged.
|
|
270
|
+
|
|
271
|
+
### normalizeCompatExtended
|
|
272
|
+
|
|
273
|
+
Apply complete compatibility normalization before conversion.
|
|
274
|
+
|
|
275
|
+
```javascript
|
|
276
|
+
cc.normalizeCompatExtended(text)
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Parameters:
|
|
280
|
+
|
|
281
|
+
- `text`: input string
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
|
|
285
|
+
- normalized string
|
|
286
|
+
|
|
287
|
+
Example:
|
|
288
|
+
|
|
289
|
+
```javascript
|
|
290
|
+
const cc = new OpenccWasm("t2s");
|
|
291
|
+
|
|
292
|
+
const input = "天龍八部書裡的聼眾";
|
|
293
|
+
const normalized = cc.normalizeCompatExtended(input);
|
|
294
|
+
|
|
295
|
+
console.log(normalized);
|
|
296
|
+
// 天龍八部書裡的聽眾
|
|
297
|
+
|
|
298
|
+
console.log(cc.convert(normalized, false));
|
|
299
|
+
// 天龙八部书里的听众
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
`normalizeCompatExtended()` combines the extended Unicode compatibility table with CJK Compatibility Ideograph
|
|
303
|
+
normalization. Use this when input may contain characters handled by either normalization set.
|
|
304
|
+
|
|
305
|
+
The normalization order is:
|
|
306
|
+
|
|
307
|
+
1. extended Unicode compatibility normalization;
|
|
308
|
+
2. CJK Compatibility Ideograph normalization.
|
|
309
|
+
|
|
310
|
+
Normal OpenCC conversion does not automatically perform compatibility normalization. Call this method explicitly before
|
|
311
|
+
`convert()` when complete compatibility normalization is desired.
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
### detofu
|
|
316
|
+
|
|
317
|
+
Replace tofu-risk rare CJK extension characters with display-compatible fallbacks.
|
|
318
|
+
|
|
319
|
+
```javascript
|
|
320
|
+
cc.detofu(text, level)
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Parameters:
|
|
324
|
+
|
|
325
|
+
- `text`: input string
|
|
326
|
+
- `level`: `DetofuLevelWasm` threshold for the CJK extension ranges to replace
|
|
327
|
+
|
|
328
|
+
Returns:
|
|
329
|
+
|
|
330
|
+
- detofu-safe string
|
|
331
|
+
|
|
332
|
+
Supported levels:
|
|
333
|
+
|
|
334
|
+
| Enum | CLI value |
|
|
335
|
+
|------------------------|-----------|
|
|
336
|
+
| `DetofuLevelWasm.ExtB` | `ext-b` |
|
|
337
|
+
| `DetofuLevelWasm.ExtC` | `ext-c` |
|
|
338
|
+
| `DetofuLevelWasm.ExtD` | `ext-d` |
|
|
339
|
+
| `DetofuLevelWasm.ExtE` | `ext-e` |
|
|
340
|
+
| `DetofuLevelWasm.ExtF` | `ext-f` |
|
|
341
|
+
| `DetofuLevelWasm.ExtG` | `ext-g` |
|
|
342
|
+
| `DetofuLevelWasm.ExtH` | `ext-h` |
|
|
343
|
+
| `DetofuLevelWasm.ExtI` | `ext-i` |
|
|
344
|
+
|
|
345
|
+
Example:
|
|
346
|
+
|
|
347
|
+
```javascript
|
|
348
|
+
import init, {
|
|
349
|
+
OpenccWasm,
|
|
350
|
+
DetofuLevelWasm
|
|
351
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
352
|
+
|
|
353
|
+
await init();
|
|
354
|
+
|
|
355
|
+
const cc = new OpenccWasm("t2s");
|
|
356
|
+
const converted = cc.convert("儼驂騑於上路", false);
|
|
357
|
+
|
|
358
|
+
console.log(converted);
|
|
359
|
+
// 俨骖𬴂于上路
|
|
360
|
+
|
|
361
|
+
console.log(cc.detofu(converted, DetofuLevelWasm.ExtB));
|
|
362
|
+
// 俨骖騑于上路
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
367
|
+
### convertDetofu
|
|
368
|
+
|
|
369
|
+
Convert text and apply detofu in one call.
|
|
370
|
+
|
|
371
|
+
```javascript
|
|
372
|
+
cc.convertDetofu(text, punctuation, level)
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Parameters:
|
|
376
|
+
|
|
377
|
+
- `text`: input string
|
|
378
|
+
- `punctuation`: whether to convert punctuation variants
|
|
379
|
+
- `level`: `DetofuLevelWasm` threshold for the CJK extension ranges to replace
|
|
380
|
+
|
|
381
|
+
Returns:
|
|
382
|
+
|
|
383
|
+
- converted detofu-safe string
|
|
384
|
+
|
|
385
|
+
Example:
|
|
386
|
+
|
|
387
|
+
```javascript
|
|
388
|
+
cc.convertDetofu("儼驂騑於上路", false, DetofuLevelWasm.ExtB);
|
|
389
|
+
// 俨骖騑于上路
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
### newWithCustomDicts
|
|
395
|
+
|
|
396
|
+
Construct a converter with in-memory custom dictionary pairs.
|
|
397
|
+
|
|
398
|
+
```javascript
|
|
399
|
+
const cc = OpenccWasm.newWithCustomDicts(config, specs);
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
Parameters:
|
|
403
|
+
|
|
404
|
+
- `config`: OpenCC config string, such as `"s2t"`
|
|
405
|
+
- `specs`: array of custom dictionary specs
|
|
406
|
+
|
|
407
|
+
TypeScript-style spec shape:
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
type WasmCustomDictSpec = {
|
|
411
|
+
slot: string;
|
|
412
|
+
mode?: "Append" | "Override";
|
|
413
|
+
pairs: Array<[string, string]>;
|
|
414
|
+
};
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
`mode` defaults to `"Append"` when omitted.
|
|
418
|
+
|
|
419
|
+
Each `pairs` entry is a `[source, target]` string tuple for the selected slot.
|
|
420
|
+
|
|
421
|
+
TypeScript example:
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
import init, {OpenccWasm} from "@laisuk/opencc-fmmseg-wasm";
|
|
425
|
+
|
|
426
|
+
await init();
|
|
427
|
+
|
|
428
|
+
const specs: WasmCustomDictSpec[] = [
|
|
429
|
+
{
|
|
430
|
+
slot: "STPhrases",
|
|
431
|
+
pairs: [
|
|
432
|
+
["云端", "雲端"]
|
|
433
|
+
]
|
|
434
|
+
}
|
|
435
|
+
];
|
|
436
|
+
|
|
437
|
+
const cc = OpenccWasm.newWithCustomDicts("s2t", specs);
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
Practical example:
|
|
441
|
+
|
|
442
|
+
```javascript
|
|
443
|
+
import init, {OpenccWasm} from "@laisuk/opencc-fmmseg-wasm";
|
|
444
|
+
|
|
445
|
+
await init();
|
|
446
|
+
|
|
447
|
+
const cc = OpenccWasm.newWithCustomDicts("s2t", [
|
|
448
|
+
{
|
|
449
|
+
slot: "STPhrases",
|
|
450
|
+
mode: "Append",
|
|
451
|
+
pairs: [
|
|
452
|
+
["帕兰蒂尔", "柏蘭蒂爾"],
|
|
453
|
+
["软件", "軟體"]
|
|
454
|
+
]
|
|
455
|
+
}
|
|
456
|
+
]);
|
|
457
|
+
|
|
458
|
+
console.log(cc.convert("帕兰蒂尔软件", false));
|
|
459
|
+
// 柏蘭蒂爾軟體
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
Override example:
|
|
463
|
+
|
|
464
|
+
```javascript
|
|
465
|
+
const cc = OpenccWasm.newWithCustomDicts("s2t", [
|
|
466
|
+
{
|
|
467
|
+
slot: "STPhrases",
|
|
468
|
+
mode: "Override",
|
|
469
|
+
pairs: [
|
|
470
|
+
["软件", "軟體"]
|
|
471
|
+
]
|
|
472
|
+
}
|
|
473
|
+
]);
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
`Override` replaces the selected slot before inserting the provided pairs. It is powerful and should be used only when
|
|
477
|
+
the caller intentionally wants to discard built-in entries for that slot.
|
|
478
|
+
|
|
479
|
+
Custom dictionary specs identify the target dictionary slot by `DictSlot` name. Slot names are trimmed and matched
|
|
480
|
+
case-insensitively, so `"stphrases"`, `" STPhrases "`, and `"STPhrases"` all select `STPhrases`. Canonical names are
|
|
481
|
+
recommended in TypeScript code and docs; use `OpenccWasm.getAvailableSlots()` to retrieve the current list.
|
|
482
|
+
|
|
483
|
+
Suffixes such as `.txt` are not accepted, even though case and surrounding whitespace are normalized. Use
|
|
484
|
+
`"STPhrases"` or `"stphrases"`, not `"STPhrases.txt"`.
|
|
485
|
+
|
|
486
|
+
Merge contract:
|
|
487
|
+
|
|
488
|
+
- Custom dictionaries are loaded from in-memory pairs only; no file I/O is involved.
|
|
489
|
+
- The embedded compressed CBOR dictionary is loaded first.
|
|
490
|
+
- Custom specs are applied to `DictionaryMaxlength` before `OpenCC::from_dictionary(...)`.
|
|
491
|
+
- Conversion hot paths remain immutable after construction.
|
|
492
|
+
- `Append` mode merges into the selected slot.
|
|
493
|
+
- Duplicate or conflicting keys use last-wins semantics.
|
|
494
|
+
- `Override` mode clears the selected slot first, then inserts the provided custom pairs.
|
|
495
|
+
- Multiple specs are applied in array order.
|
|
496
|
+
|
|
497
|
+
This API is useful for browser apps, user-defined terminology, database-loaded terms, generated dictionaries,
|
|
498
|
+
`localStorage` or `IndexedDB` terms, testing, and embedded WASM environments. Customization happens at construction
|
|
499
|
+
time, not during conversion.
|
|
500
|
+
|
|
501
|
+
---
|
|
502
|
+
|
|
503
|
+
## Supported Configs
|
|
504
|
+
|
|
505
|
+
| Config | Enum | Description |
|
|
506
|
+
|---------|--------------------------|-------------------------------------------------------|
|
|
507
|
+
| `s2t` | `OpenccConfigWasm.S2t` | Simplified Chinese → Traditional Chinese |
|
|
508
|
+
| `s2tw` | `OpenccConfigWasm.S2tw` | Simplified Chinese → Taiwan Traditional |
|
|
509
|
+
| `s2twp` | `OpenccConfigWasm.S2twp` | Simplified Chinese → Taiwan Traditional (phrases) |
|
|
510
|
+
| `s2hk` | `OpenccConfigWasm.S2hk` | Simplified Chinese → Hong Kong Traditional |
|
|
511
|
+
| `s2hkp` | `OpenccConfigWasm.S2hkp` | Simplified Chinese → Hong Kong Traditional (phrases) |
|
|
512
|
+
| `t2s` | `OpenccConfigWasm.T2s` | Traditional Chinese → Simplified Chinese |
|
|
513
|
+
| `t2tw` | `OpenccConfigWasm.T2tw` | Traditional Chinese → Taiwan Traditional |
|
|
514
|
+
| `t2twp` | `OpenccConfigWasm.T2twp` | Traditional Chinese → Taiwan Traditional (phrases) |
|
|
515
|
+
| `t2hk` | `OpenccConfigWasm.T2hk` | Traditional Chinese → Hong Kong Traditional |
|
|
516
|
+
| `t2hkp` | `OpenccConfigWasm.T2hkp` | Traditional Chinese → Hong Kong Traditional (phrases) |
|
|
517
|
+
| `tw2s` | `OpenccConfigWasm.Tw2s` | Taiwan Traditional → Simplified Chinese |
|
|
518
|
+
| `tw2sp` | `OpenccConfigWasm.Tw2sp` | Taiwan Traditional → Simplified Chinese (phrases) |
|
|
519
|
+
| `tw2t` | `OpenccConfigWasm.Tw2t` | Taiwan Traditional → Traditional Chinese |
|
|
520
|
+
| `tw2tp` | `OpenccConfigWasm.Tw2tp` | Taiwan Traditional → Traditional Chinese (phrases) |
|
|
521
|
+
| `hk2s` | `OpenccConfigWasm.Hk2s` | Hong Kong Traditional → Simplified Chinese |
|
|
522
|
+
| `hk2sp` | `OpenccConfigWasm.Hk2sp` | Hong Kong Traditional → Simplified Chinese (phrases) |
|
|
523
|
+
| `hk2t` | `OpenccConfigWasm.Hk2t` | Hong Kong Traditional → Traditional Chinese |
|
|
524
|
+
| `hk2tp` | `OpenccConfigWasm.Hk2tp` | Hong Kong Traditional → Traditional Chinese (phrases) |
|
|
525
|
+
| `jp2t` | `OpenccConfigWasm.Jp2t` | Japanese Shinjitai → Traditional Chinese |
|
|
526
|
+
| `t2jp` | `OpenccConfigWasm.T2jp` | Traditional Chinese → Japanese Shinjitai |
|
|
527
|
+
|
|
528
|
+
The numeric enum values match the vendored Rust backend. Existing values are unchanged; `S2hkp = 17`, `Hk2sp = 18`,
|
|
529
|
+
`T2hkp = 19`, and `Hk2tp = 20`.
|
|
530
|
+
|
|
531
|
+
---
|
|
532
|
+
|
|
533
|
+
## Using Config Enums
|
|
534
|
+
|
|
535
|
+
```javascript
|
|
536
|
+
import init, {
|
|
537
|
+
OpenccWasm,
|
|
538
|
+
OpenccConfigWasm
|
|
539
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
540
|
+
|
|
541
|
+
await init();
|
|
542
|
+
|
|
543
|
+
const cc = OpenccWasm.newWithEnum(
|
|
544
|
+
OpenccConfigWasm.S2hkp
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
console.log(cc.convert("别随便录影侵犯个人隐私权", false));
|
|
548
|
+
// 別隨便錄影侵犯個人私隱權
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
---
|
|
552
|
+
|
|
553
|
+
## Office / EPUB Conversion
|
|
554
|
+
|
|
555
|
+
Office and EPUB conversion runs fully locally in the browser or Node.js. Files are passed in and returned as bytes;
|
|
556
|
+
nothing is uploaded to a backend server.
|
|
557
|
+
|
|
558
|
+
This is useful for converting text inside:
|
|
559
|
+
|
|
560
|
+
```text
|
|
561
|
+
docx, xlsx, pptx, odt, ods, odp, epub
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
File size is limited by available browser or Node.js memory, but there is no upload or server-side limit. Font
|
|
565
|
+
preservation is supported with the `keepFont` option.
|
|
566
|
+
|
|
567
|
+
Use the instance method when possible. It reuses the converter configuration and any custom dictionaries already held by
|
|
568
|
+
the `OpenccWasm` instance.
|
|
569
|
+
|
|
570
|
+
```javascript
|
|
571
|
+
cc.convertOfficeBytes(inputBytes, format, punctuation, keepFont)
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
Parameters:
|
|
575
|
+
|
|
576
|
+
- `inputBytes`: `Uint8Array` document bytes
|
|
577
|
+
- `format`: `docx`, `xlsx`, `pptx`, `odt`, `ods`, `odp`, or `epub`
|
|
578
|
+
- `punctuation`: whether to convert punctuation variants
|
|
579
|
+
- `keepFont`: whether to preserve font declarations where supported
|
|
580
|
+
|
|
581
|
+
Returns:
|
|
582
|
+
|
|
583
|
+
- converted output bytes
|
|
584
|
+
|
|
585
|
+
The older free function remains available for compatibility:
|
|
586
|
+
|
|
587
|
+
```javascript
|
|
588
|
+
convert_office_bytes(inputBytes, format, config, punctuation, keepFont)
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
### Browser Office Example
|
|
592
|
+
|
|
593
|
+
```javascript
|
|
594
|
+
import init, {OpenccWasm} from "@laisuk/opencc-fmmseg-wasm";
|
|
595
|
+
|
|
596
|
+
await init();
|
|
597
|
+
|
|
598
|
+
const cc = new OpenccWasm("s2t");
|
|
599
|
+
const file = document.querySelector("input[type=file]").files[0];
|
|
600
|
+
const inputBytes = new Uint8Array(await file.arrayBuffer());
|
|
601
|
+
|
|
602
|
+
const outputBytes = cc.convertOfficeBytes(
|
|
603
|
+
inputBytes,
|
|
604
|
+
"docx",
|
|
605
|
+
true,
|
|
606
|
+
true
|
|
607
|
+
);
|
|
608
|
+
|
|
609
|
+
const blob = new Blob([outputBytes], {
|
|
610
|
+
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
const a = document.createElement("a");
|
|
614
|
+
a.href = URL.createObjectURL(blob);
|
|
615
|
+
a.download = "converted.docx";
|
|
616
|
+
a.click();
|
|
617
|
+
URL.revokeObjectURL(a.href);
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
### Node.js Office Example
|
|
621
|
+
|
|
622
|
+
```javascript
|
|
623
|
+
import fs from "fs";
|
|
624
|
+
import init, {OpenccWasm} from "@laisuk/opencc-fmmseg-wasm";
|
|
625
|
+
|
|
626
|
+
await init();
|
|
627
|
+
|
|
628
|
+
const cc = new OpenccWasm("s2t");
|
|
629
|
+
const inputBytes = fs.readFileSync("input.docx");
|
|
630
|
+
|
|
631
|
+
const outputBytes = cc.convertOfficeBytes(
|
|
632
|
+
inputBytes,
|
|
633
|
+
"docx",
|
|
634
|
+
true,
|
|
635
|
+
true
|
|
636
|
+
);
|
|
637
|
+
|
|
638
|
+
fs.writeFileSync("output.docx", outputBytes);
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
---
|
|
642
|
+
|
|
643
|
+
## Browser Example
|
|
644
|
+
|
|
645
|
+
```html
|
|
646
|
+
<!DOCTYPE html>
|
|
647
|
+
<html lang="en">
|
|
648
|
+
<head>
|
|
649
|
+
<meta charset="UTF-8">
|
|
650
|
+
<title>OpenCC WASM Demo</title>
|
|
651
|
+
</head>
|
|
652
|
+
<body>
|
|
653
|
+
|
|
654
|
+
<script type="module">
|
|
655
|
+
import init, {
|
|
656
|
+
OpenccWasm
|
|
657
|
+
} from "./pkg/opencc_fmmseg_wasm.js";
|
|
658
|
+
|
|
659
|
+
await init();
|
|
660
|
+
|
|
661
|
+
const cc = new OpenccWasm("s2t");
|
|
662
|
+
|
|
663
|
+
console.log(
|
|
664
|
+
cc.convert("汉字", false)
|
|
665
|
+
);
|
|
666
|
+
</script>
|
|
667
|
+
|
|
668
|
+
</body>
|
|
669
|
+
</html>
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
> **Note**
|
|
673
|
+
>
|
|
674
|
+
> Normally, `await init();` is sufficient when using the published npm package.
|
|
675
|
+
>
|
|
676
|
+
> When running directly from a local repository checkout (for example in tests
|
|
677
|
+
> or development scripts), initialize using explicit WASM bytes:
|
|
678
|
+
>
|
|
679
|
+
> ```javascript
|
|
680
|
+
> import fs from "fs";
|
|
681
|
+
> import init from "../pkg/opencc_fmmseg_wasm.js";
|
|
682
|
+
>
|
|
683
|
+
> const wasmBytes = fs.readFileSync(
|
|
684
|
+
> "../pkg/opencc_fmmseg_wasm_bg.wasm"
|
|
685
|
+
> );
|
|
686
|
+
>
|
|
687
|
+
> await init({
|
|
688
|
+
> module_or_path: wasmBytes
|
|
689
|
+
> });
|
|
690
|
+
> ```
|
|
691
|
+
|
|
692
|
+
---
|
|
693
|
+
|
|
694
|
+
## Node.js CLI
|
|
695
|
+
|
|
696
|
+
The package includes a zero-dependency Node.js CLI:
|
|
697
|
+
|
|
698
|
+
```bash
|
|
699
|
+
opencc-fmmseg convert -i input.txt -o output.txt -c s2t -p
|
|
700
|
+
opencc-fmmseg convert -i input.txt -o output.txt -c t2s -p --detofu all
|
|
701
|
+
echo "别随便录影侵犯个人隐私权" | opencc-fmmseg convert -c s2hkp
|
|
702
|
+
echo "天龍八部書裡的喬峰是契丹人" | opencc-fmmseg convert -c t2s --norm-compat
|
|
703
|
+
// 天龙八部书里的乔峰是契丹人
|
|
704
|
+
echo "這個細路哥很靈活" | opencc-fmmseg convert -c hk2sp --custom-dict hkphrasesrev:append:my_hk_dict.txt
|
|
705
|
+
// 这个小男孩很灵活
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
my_hk_dict.txt:
|
|
709
|
+
|
|
710
|
+
```
|
|
711
|
+
# Custom Dictionary
|
|
712
|
+
|
|
713
|
+
細路哥 小男孩
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
```bash
|
|
717
|
+
opencc-fmmseg office -i input.docx -o output.docx -c s2t -p --keep-font
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
### Text Conversion Options
|
|
721
|
+
|
|
722
|
+
```text
|
|
723
|
+
-i, --input <file> Input text file; stdin if omitted
|
|
724
|
+
-o, --output <file> Output text file; stdout if omitted
|
|
725
|
+
-c, --config <conversion> Conversion config (default: s2t)
|
|
726
|
+
-p, --punct Enable punctuation conversion
|
|
727
|
+
--detofu [level] Replace tofu-risk rare CJK extension chars after conversion
|
|
728
|
+
level: all | ext-b | ext-c | ext-d | ext-e | ext-f | ext-g | ext-h | ext-i
|
|
729
|
+
default when omitted value: all
|
|
730
|
+
--keep-ids Preserve complete IDS expressions during conversion (default: false)
|
|
731
|
+
-n, --norm-compat Normalize CJK Compatibility Ideographs before conversion (default: false)
|
|
732
|
+
-E, --norm-compat-extended Normalize extended Unicode compatibility forms before conversion (default: false)
|
|
733
|
+
-D, --custom-dict <slot:mode:file>
|
|
734
|
+
Load a custom dictionary.
|
|
735
|
+
May be specified multiple times.
|
|
736
|
+
Examples:
|
|
737
|
+
--custom-dict hkphrasesrev:append:my_hk_dict.txt
|
|
738
|
+
--custom-dict stphrases:override:terms.txt
|
|
739
|
+
--in-enc <encoding> Input encoding (default: utf8)
|
|
740
|
+
--out-enc <encoding> Output encoding (default: utf8)
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
Supported conversion configs:
|
|
744
|
+
|
|
745
|
+
```text
|
|
746
|
+
s2t, s2tw, s2twp, s2hk, s2hkp, t2s, t2tw, t2twp, t2hk, t2hkp,
|
|
747
|
+
tw2s, tw2sp, tw2t, tw2tp, hk2s, hk2sp, hk2t, hk2tp, jp2t, t2jp
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
### Office / EPUB Options
|
|
751
|
+
|
|
752
|
+
```text
|
|
753
|
+
-i, --input <file> Input Office / EPUB file
|
|
754
|
+
-o, --output <file> Output file
|
|
755
|
+
-c, --config <conversion> Conversion config (default: s2t)
|
|
756
|
+
-p, --punct Enable punctuation conversion
|
|
757
|
+
-f, --format <format> docx | xlsx | pptx | odt | ods | odp | epub
|
|
758
|
+
-F, --convert-filename Convert generated output filename stem (default: false)
|
|
759
|
+
--keep-font Preserve font-family information (default)
|
|
760
|
+
--no-keep-font Do not preserve font-family information
|
|
761
|
+
--custom-dict <slot:mode:file>
|
|
762
|
+
Load a custom dictionary.
|
|
763
|
+
May be specified multiple times.
|
|
764
|
+
Examples:
|
|
765
|
+
--custom-dict hkphrasesrev:append:my_hk_dict.txt
|
|
766
|
+
--custom-dict stphrases:override:terms.txt
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
For `office`, the format is inferred from the input file extension when `--format` is omitted.
|
|
770
|
+
|
|
771
|
+
If `-o, --output` is omitted, `office` writes:
|
|
772
|
+
|
|
773
|
+
```text
|
|
774
|
+
<input-name>_converted.<ext>
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
---
|
|
778
|
+
|
|
779
|
+
## TypeScript Support
|
|
780
|
+
|
|
781
|
+
The package includes generated TypeScript definitions from `wasm-bindgen`.
|
|
782
|
+
|
|
783
|
+
The WASM-facing enum is exported as `OpenccConfigWasm`, alongside `OpenccWasm`.
|
|
784
|
+
|
|
785
|
+
`OpenccConfigWasm.S2hkp`, `OpenccConfigWasm.Hk2sp`, `OpenccConfigWasm.T2hkp`, and
|
|
786
|
+
`OpenccConfigWasm.Hk2tp` are available for Hong Kong phrase conversions and map to backend config IDs `17` through `20`.
|
|
787
|
+
|
|
788
|
+
---
|
|
789
|
+
|
|
790
|
+
## Performance Notes
|
|
791
|
+
|
|
792
|
+
- WebAssembly build disables Rayon parallelism by default.
|
|
793
|
+
- Dictionaries are embedded into the WASM binary.
|
|
794
|
+
- Browser caching significantly improves subsequent loads.
|
|
795
|
+
|
|
796
|
+
---
|
|
797
|
+
|
|
798
|
+
## Related Projects
|
|
799
|
+
|
|
800
|
+
- Rust backend: https://github.com/laisuk/opencc-fmmseg
|
|
801
|
+
- C API: https://github.com/laisuk/opencc-fmmseg/tree/master/capi/opencc-fmmseg-capi
|
|
802
|
+
- .NET: https://github.com/laisuk/OpenccNet
|
|
803
|
+
- Python: https://github.com/laisuk/opencc_purepy
|
|
804
|
+
|
|
805
|
+
---
|
|
806
|
+
|
|
807
|
+
## License
|
|
808
|
+
|
|
809
|
+
MIT
|