@laisuk/opencc-fmmseg-wasm 0.2.3 → 0.2.5
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 +105 -18
- package/bin/opencc.js +77 -2
- package/opencc_fmmseg_wasm.d.ts +15 -0
- package/opencc_fmmseg_wasm.js +53 -0
- package/opencc_fmmseg_wasm_bg.wasm +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,7 +38,8 @@ npm install @laisuk/opencc-fmmseg-wasm
|
|
|
38
38
|
|
|
39
39
|
```javascript
|
|
40
40
|
import init, {
|
|
41
|
-
OpenccWasm
|
|
41
|
+
OpenccWasm,
|
|
42
|
+
DetofuLevelWasm
|
|
42
43
|
} from "@laisuk/opencc-fmmseg-wasm";
|
|
43
44
|
|
|
44
45
|
await init();
|
|
@@ -47,6 +48,9 @@ const cc = new OpenccWasm("s2t");
|
|
|
47
48
|
|
|
48
49
|
console.log(cc.convert("汉字", false));
|
|
49
50
|
// 漢字
|
|
51
|
+
|
|
52
|
+
console.log(cc.convertDetofu("儼驂騑於上路", false, DetofuLevelWasm.ExtB));
|
|
53
|
+
// 俨骖騑于上路
|
|
50
54
|
```
|
|
51
55
|
|
|
52
56
|
---
|
|
@@ -54,16 +58,16 @@ console.log(cc.convert("汉字", false));
|
|
|
54
58
|
## Using Config Enums
|
|
55
59
|
|
|
56
60
|
```javascript
|
|
57
|
-
import init, {
|
|
58
|
-
OpenccWasm,
|
|
59
|
-
OpenccConfigWasm
|
|
60
|
-
} from "@laisuk/opencc-fmmseg-wasm";
|
|
61
|
-
|
|
62
|
-
await init();
|
|
63
|
-
|
|
64
|
-
const cc = OpenccWasm.newWithEnum(
|
|
65
|
-
OpenccConfigWasm.S2t
|
|
66
|
-
);
|
|
61
|
+
import init, {
|
|
62
|
+
OpenccWasm,
|
|
63
|
+
OpenccConfigWasm
|
|
64
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
65
|
+
|
|
66
|
+
await init();
|
|
67
|
+
|
|
68
|
+
const cc = OpenccWasm.newWithEnum(
|
|
69
|
+
OpenccConfigWasm.S2t
|
|
70
|
+
);
|
|
67
71
|
|
|
68
72
|
console.log(cc.convert("汉字", false));
|
|
69
73
|
// 漢字
|
|
@@ -138,6 +142,85 @@ cc.convert("汉字", false);
|
|
|
138
142
|
|
|
139
143
|
---
|
|
140
144
|
|
|
145
|
+
### detofu
|
|
146
|
+
|
|
147
|
+
Replace tofu-risk rare CJK extension characters with display-compatible fallbacks.
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
cc.detofu(text, level)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Parameters:
|
|
154
|
+
|
|
155
|
+
* `text`: input string
|
|
156
|
+
* `level`: `DetofuLevelWasm` threshold for the CJK extension ranges to replace
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
|
|
160
|
+
* detofu-safe string
|
|
161
|
+
|
|
162
|
+
Supported levels:
|
|
163
|
+
|
|
164
|
+
| Enum | CLI value |
|
|
165
|
+
|------------------------|-----------|
|
|
166
|
+
| `DetofuLevelWasm.ExtB` | `ext-b` |
|
|
167
|
+
| `DetofuLevelWasm.ExtC` | `ext-c` |
|
|
168
|
+
| `DetofuLevelWasm.ExtD` | `ext-d` |
|
|
169
|
+
| `DetofuLevelWasm.ExtE` | `ext-e` |
|
|
170
|
+
| `DetofuLevelWasm.ExtF` | `ext-f` |
|
|
171
|
+
| `DetofuLevelWasm.ExtG` | `ext-g` |
|
|
172
|
+
| `DetofuLevelWasm.ExtH` | `ext-h` |
|
|
173
|
+
| `DetofuLevelWasm.ExtI` | `ext-i` |
|
|
174
|
+
|
|
175
|
+
Example:
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
import init, {
|
|
179
|
+
OpenccWasm,
|
|
180
|
+
DetofuLevelWasm
|
|
181
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
182
|
+
|
|
183
|
+
await init();
|
|
184
|
+
|
|
185
|
+
const cc = new OpenccWasm("t2s");
|
|
186
|
+
const converted = cc.convert("儼驂騑於上路", false);
|
|
187
|
+
|
|
188
|
+
console.log(converted);
|
|
189
|
+
// 俨骖𬴂于上路
|
|
190
|
+
|
|
191
|
+
console.log(cc.detofu(converted, DetofuLevelWasm.ExtB));
|
|
192
|
+
// 俨骖騑于上路
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
### convertDetofu
|
|
198
|
+
|
|
199
|
+
Convert text and apply detofu in one call.
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
cc.convertDetofu(text, punctuation, level)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Parameters:
|
|
206
|
+
|
|
207
|
+
* `text`: input string
|
|
208
|
+
* `punctuation`: whether to convert punctuation variants
|
|
209
|
+
* `level`: `DetofuLevelWasm` threshold for the CJK extension ranges to replace
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
|
|
213
|
+
* converted detofu-safe string
|
|
214
|
+
|
|
215
|
+
Example:
|
|
216
|
+
|
|
217
|
+
```javascript
|
|
218
|
+
cc.convertDetofu("儼驂騑於上路", false, DetofuLevelWasm.ExtB);
|
|
219
|
+
// 俨骖騑于上路
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
141
224
|
### setConfig
|
|
142
225
|
|
|
143
226
|
```javascript
|
|
@@ -316,6 +399,7 @@ The package includes a zero-dependency Node.js CLI:
|
|
|
316
399
|
|
|
317
400
|
```bash
|
|
318
401
|
opencc-fmmseg convert -i input.txt -o output.txt -c s2t -p
|
|
402
|
+
opencc-fmmseg convert -i input.txt -o output.txt -c t2s -p --detofu all
|
|
319
403
|
```
|
|
320
404
|
|
|
321
405
|
```bash
|
|
@@ -329,6 +413,9 @@ opencc-fmmseg office -i input.docx -o output.docx -c s2t -p --keep-font
|
|
|
329
413
|
-o, --output <file> Output text file
|
|
330
414
|
-c, --config <conversion> Conversion config
|
|
331
415
|
-p, --punct Enable punctuation conversion
|
|
416
|
+
--detofu [level] Replace tofu-risk rare CJK extension chars after conversion
|
|
417
|
+
level: all | ext-b | ext-c | ext-d | ext-e | ext-f | ext-g | ext-h | ext-i
|
|
418
|
+
default when omitted value: all
|
|
332
419
|
--in-enc <encoding> Input encoding
|
|
333
420
|
--out-enc <encoding> Output encoding
|
|
334
421
|
```
|
|
@@ -356,13 +443,13 @@ If `-o, --output` is omitted, `office` writes:
|
|
|
356
443
|
|
|
357
444
|
---
|
|
358
445
|
|
|
359
|
-
## TypeScript Support
|
|
360
|
-
|
|
361
|
-
The package includes generated TypeScript definitions from `wasm-bindgen`.
|
|
362
|
-
|
|
363
|
-
The WASM-facing enum is exported as `OpenccConfigWasm`, alongside `OpenccWasm`.
|
|
364
|
-
|
|
365
|
-
---
|
|
446
|
+
## TypeScript Support
|
|
447
|
+
|
|
448
|
+
The package includes generated TypeScript definitions from `wasm-bindgen`.
|
|
449
|
+
|
|
450
|
+
The WASM-facing enum is exported as `OpenccConfigWasm`, alongside `OpenccWasm`.
|
|
451
|
+
|
|
452
|
+
---
|
|
366
453
|
|
|
367
454
|
## Performance Notes
|
|
368
455
|
|
package/bin/opencc.js
CHANGED
|
@@ -6,6 +6,7 @@ import process from "process";
|
|
|
6
6
|
|
|
7
7
|
import init, {
|
|
8
8
|
OpenccWasm,
|
|
9
|
+
DetofuLevelWasm,
|
|
9
10
|
convert_office_bytes
|
|
10
11
|
} from "../opencc_fmmseg_wasm.js";
|
|
11
12
|
|
|
@@ -53,6 +54,9 @@ Convert options:
|
|
|
53
54
|
-o, --output <file> Output text file; stdout if omitted
|
|
54
55
|
-c, --config <conversion> Conversion config (default: s2t)
|
|
55
56
|
-p, --punct Enable punctuation conversion
|
|
57
|
+
--detofu [level] Replace tofu-risk rare CJK extension chars after conversion
|
|
58
|
+
level: all | ext-b | ext-c | ext-d | ext-e | ext-f | ext-g | ext-h | ext-i
|
|
59
|
+
default when omitted value: all
|
|
56
60
|
--in-enc <encoding> Input encoding (default: utf8)
|
|
57
61
|
--out-enc <encoding> Output encoding (default: utf8)
|
|
58
62
|
|
|
@@ -73,6 +77,8 @@ Examples:
|
|
|
73
77
|
npx opencc-fmmseg convert -i a.txt -o b.txt -c s2t
|
|
74
78
|
npx opencc-fmmseg convert -i a.txt -o b.txt -c s2tw -p
|
|
75
79
|
cat a.txt | npx opencc-fmmseg convert -c t2s
|
|
80
|
+
npx opencc-fmmseg convert -i a.txt -o b.txt -c t2s --detofu
|
|
81
|
+
npx opencc-fmmseg convert -i a.txt -o b.txt -c t2s --detofu ext-c
|
|
76
82
|
|
|
77
83
|
npx opencc-fmmseg office -i a.docx -o b.docx -c s2t -p
|
|
78
84
|
npx opencc-fmmseg office -i a.epub -c s2tw --auto-ext
|
|
@@ -179,6 +185,56 @@ function applyAutoExt(outputFile, officeFormat, autoExt) {
|
|
|
179
185
|
return `${outputFile}.${officeFormat}`;
|
|
180
186
|
}
|
|
181
187
|
|
|
188
|
+
function parseDetofuLevel(value) {
|
|
189
|
+
if (value === null || value === undefined || value === true) {
|
|
190
|
+
return DetofuLevelWasm.ExtB; // "all"
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const normalized = String(value).trim().toLowerCase();
|
|
194
|
+
|
|
195
|
+
switch (normalized) {
|
|
196
|
+
case "":
|
|
197
|
+
case "all":
|
|
198
|
+
case "ext-b":
|
|
199
|
+
case "extb":
|
|
200
|
+
case "b":
|
|
201
|
+
return DetofuLevelWasm.ExtB;
|
|
202
|
+
case "ext-c":
|
|
203
|
+
case "extc":
|
|
204
|
+
case "c":
|
|
205
|
+
return DetofuLevelWasm.ExtC;
|
|
206
|
+
case "ext-d":
|
|
207
|
+
case "extd":
|
|
208
|
+
case "d":
|
|
209
|
+
return DetofuLevelWasm.ExtD;
|
|
210
|
+
case "ext-e":
|
|
211
|
+
case "exte":
|
|
212
|
+
case "e":
|
|
213
|
+
return DetofuLevelWasm.ExtE;
|
|
214
|
+
case "ext-f":
|
|
215
|
+
case "extf":
|
|
216
|
+
case "f":
|
|
217
|
+
return DetofuLevelWasm.ExtF;
|
|
218
|
+
case "ext-g":
|
|
219
|
+
case "extg":
|
|
220
|
+
case "g":
|
|
221
|
+
return DetofuLevelWasm.ExtG;
|
|
222
|
+
case "ext-h":
|
|
223
|
+
case "exth":
|
|
224
|
+
case "h":
|
|
225
|
+
return DetofuLevelWasm.ExtH;
|
|
226
|
+
case "ext-i":
|
|
227
|
+
case "exti":
|
|
228
|
+
case "i":
|
|
229
|
+
return DetofuLevelWasm.ExtI;
|
|
230
|
+
default:
|
|
231
|
+
throw new Error(
|
|
232
|
+
`Invalid detofu level: ${value}. ` +
|
|
233
|
+
`Valid values: all | ext-b | ext-c | ext-d | ext-e | ext-f | ext-g | ext-h | ext-i`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
182
238
|
async function runConvert(args) {
|
|
183
239
|
const input = getArg(args, "-i", "--input");
|
|
184
240
|
const output = getArg(args, "-o", "--output");
|
|
@@ -187,6 +243,20 @@ async function runConvert(args) {
|
|
|
187
243
|
const outEnc = getArg(args, null, "--out-enc", "utf8");
|
|
188
244
|
const punct = hasFlag(args, "-p", "--punct");
|
|
189
245
|
|
|
246
|
+
const detofuIndex = args.indexOf("--detofu");
|
|
247
|
+
const detofuEnabled = detofuIndex !== -1;
|
|
248
|
+
let detofuLevel = null;
|
|
249
|
+
|
|
250
|
+
if (detofuEnabled) {
|
|
251
|
+
const next = args[detofuIndex + 1];
|
|
252
|
+
|
|
253
|
+
if (!next || next.startsWith("-")) {
|
|
254
|
+
detofuLevel = parseDetofuLevel("all");
|
|
255
|
+
} else {
|
|
256
|
+
detofuLevel = parseDetofuLevel(next);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
190
260
|
await ensureWasmInitialized();
|
|
191
261
|
|
|
192
262
|
const cc = new OpenccWasm(config);
|
|
@@ -197,7 +267,11 @@ async function runConvert(args) {
|
|
|
197
267
|
}
|
|
198
268
|
|
|
199
269
|
const inputText = readInputText(input, inEnc);
|
|
200
|
-
|
|
270
|
+
let outputText = cc.convert(inputText, punct);
|
|
271
|
+
|
|
272
|
+
if (detofuEnabled) {
|
|
273
|
+
outputText = cc.detofu(outputText, detofuLevel);
|
|
274
|
+
}
|
|
201
275
|
|
|
202
276
|
writeOutputText(output, outputText, outEnc);
|
|
203
277
|
|
|
@@ -209,7 +283,8 @@ async function runConvert(args) {
|
|
|
209
283
|
console.error();
|
|
210
284
|
}
|
|
211
285
|
|
|
212
|
-
|
|
286
|
+
const suffix = detofuEnabled ? ", detofu" : "";
|
|
287
|
+
console.error(`Conversion completed (${config}${suffix}): ${inFrom} -> ${outTo}`);
|
|
213
288
|
}
|
|
214
289
|
}
|
|
215
290
|
|
package/opencc_fmmseg_wasm.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
|
|
4
|
+
export enum DetofuLevelWasm {
|
|
5
|
+
ExtB = 2,
|
|
6
|
+
ExtC = 3,
|
|
7
|
+
ExtD = 4,
|
|
8
|
+
ExtE = 5,
|
|
9
|
+
ExtF = 6,
|
|
10
|
+
ExtG = 7,
|
|
11
|
+
ExtH = 8,
|
|
12
|
+
ExtI = 9,
|
|
13
|
+
}
|
|
14
|
+
|
|
4
15
|
export enum OpenccConfigWasm {
|
|
5
16
|
S2t = 1,
|
|
6
17
|
S2tw = 2,
|
|
@@ -24,7 +35,9 @@ export class OpenccWasm {
|
|
|
24
35
|
free(): void;
|
|
25
36
|
[Symbol.dispose](): void;
|
|
26
37
|
convert(text: string, punctuation: boolean): string;
|
|
38
|
+
convertDetofu(text: string, punctuation: boolean, level: DetofuLevelWasm): string;
|
|
27
39
|
debugPing(): string;
|
|
40
|
+
detofu(text: string, level: DetofuLevelWasm): string;
|
|
28
41
|
getConfig(): string;
|
|
29
42
|
getConfigId(): number;
|
|
30
43
|
static getSupportedConfigs(): string[];
|
|
@@ -45,7 +58,9 @@ export interface InitOutput {
|
|
|
45
58
|
readonly __wbg_openccwasm_free: (a: number, b: number) => void;
|
|
46
59
|
readonly convert_office_bytes: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number];
|
|
47
60
|
readonly openccwasm_convert: (a: number, b: number, c: number, d: number) => [number, number];
|
|
61
|
+
readonly openccwasm_convertDetofu: (a: number, b: number, c: number, d: number, e: number) => [number, number];
|
|
48
62
|
readonly openccwasm_debugPing: (a: number) => [number, number];
|
|
63
|
+
readonly openccwasm_detofu: (a: number, b: number, c: number, d: number) => [number, number];
|
|
49
64
|
readonly openccwasm_getConfig: (a: number) => [number, number];
|
|
50
65
|
readonly openccwasm_getConfigId: (a: number) => number;
|
|
51
66
|
readonly openccwasm_getSupportedConfigs: () => [number, number];
|
package/opencc_fmmseg_wasm.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
/* @ts-self-types="./opencc_fmmseg_wasm.d.ts" */
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @enum {2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}
|
|
5
|
+
*/
|
|
6
|
+
export const DetofuLevelWasm = Object.freeze({
|
|
7
|
+
ExtB: 2, "2": "ExtB",
|
|
8
|
+
ExtC: 3, "3": "ExtC",
|
|
9
|
+
ExtD: 4, "4": "ExtD",
|
|
10
|
+
ExtE: 5, "5": "ExtE",
|
|
11
|
+
ExtF: 6, "6": "ExtF",
|
|
12
|
+
ExtG: 7, "7": "ExtG",
|
|
13
|
+
ExtH: 8, "8": "ExtH",
|
|
14
|
+
ExtI: 9, "9": "ExtI",
|
|
15
|
+
});
|
|
16
|
+
|
|
3
17
|
/**
|
|
4
18
|
* @enum {1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16}
|
|
5
19
|
*/
|
|
@@ -58,6 +72,26 @@ export class OpenccWasm {
|
|
|
58
72
|
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
59
73
|
}
|
|
60
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* @param {string} text
|
|
77
|
+
* @param {boolean} punctuation
|
|
78
|
+
* @param {DetofuLevelWasm} level
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
convertDetofu(text, punctuation, level) {
|
|
82
|
+
let deferred2_0;
|
|
83
|
+
let deferred2_1;
|
|
84
|
+
try {
|
|
85
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
86
|
+
const len0 = WASM_VECTOR_LEN;
|
|
87
|
+
const ret = wasm.openccwasm_convertDetofu(this.__wbg_ptr, ptr0, len0, punctuation, level);
|
|
88
|
+
deferred2_0 = ret[0];
|
|
89
|
+
deferred2_1 = ret[1];
|
|
90
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
91
|
+
} finally {
|
|
92
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
61
95
|
/**
|
|
62
96
|
* @returns {string}
|
|
63
97
|
*/
|
|
@@ -73,6 +107,25 @@ export class OpenccWasm {
|
|
|
73
107
|
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
74
108
|
}
|
|
75
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* @param {string} text
|
|
112
|
+
* @param {DetofuLevelWasm} level
|
|
113
|
+
* @returns {string}
|
|
114
|
+
*/
|
|
115
|
+
detofu(text, level) {
|
|
116
|
+
let deferred2_0;
|
|
117
|
+
let deferred2_1;
|
|
118
|
+
try {
|
|
119
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
120
|
+
const len0 = WASM_VECTOR_LEN;
|
|
121
|
+
const ret = wasm.openccwasm_detofu(this.__wbg_ptr, ptr0, len0, level);
|
|
122
|
+
deferred2_0 = ret[0];
|
|
123
|
+
deferred2_1 = ret[1];
|
|
124
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
125
|
+
} finally {
|
|
126
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
76
129
|
/**
|
|
77
130
|
* @returns {string}
|
|
78
131
|
*/
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@laisuk/opencc-fmmseg-wasm",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "WebAssembly bindings for opencc-fmmseg, a high-performance OpenCC-compatible Simplified/Traditional Chinese converter.",
|
|
5
|
-
"version": "0.2.
|
|
5
|
+
"version": "0.2.5",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|