@laisuk/opencc-fmmseg-wasm 0.1.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/LICENSE +21 -0
- package/README.md +249 -0
- package/opencc_fmmseg_wasm.d.ts +84 -0
- package/opencc_fmmseg_wasm.js +400 -0
- package/opencc_fmmseg_wasm_bg.wasm +0 -0
- package/package.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 laisuk@yahoo.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# opencc-fmmseg-wasm
|
|
2
|
+
|
|
3
|
+
OpenCC FMM segmentation WebAssembly bindings for browsers and JavaScript runtimes.
|
|
4
|
+
|
|
5
|
+
This package provides high-quality Simplified Chinese ↔ Traditional Chinese conversion powered by the Rust [
|
|
6
|
+
`opencc-fmmseg`](https://github.com/laisuk/opencc-fmmseg) engine.
|
|
7
|
+
|
|
8
|
+
Features:
|
|
9
|
+
|
|
10
|
+
* OpenCC-compatible conversion configs
|
|
11
|
+
* Pure WebAssembly (no native binaries)
|
|
12
|
+
* Browser-friendly
|
|
13
|
+
* TypeScript-friendly APIs
|
|
14
|
+
* Fast Rust backend
|
|
15
|
+
* FMM-based phrase segmentation
|
|
16
|
+
* Traditional Chinese regional variants
|
|
17
|
+
* Japanese Shinjitai conversion support
|
|
18
|
+
* Chinese script detection (`zho_check`)
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @laisuk/opencc-fmmseg-wasm
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
import init, {
|
|
34
|
+
OpenccWasm
|
|
35
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
36
|
+
|
|
37
|
+
await init();
|
|
38
|
+
|
|
39
|
+
const cc = new OpenccWasm("s2t");
|
|
40
|
+
|
|
41
|
+
console.log(cc.convert("汉字", false));
|
|
42
|
+
// 漢字
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Using Config Enums
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
import init, {
|
|
51
|
+
OpenccWasm,
|
|
52
|
+
WasmOpenccConfig
|
|
53
|
+
} from "@laisuk/opencc-fmmseg-wasm";
|
|
54
|
+
|
|
55
|
+
await init();
|
|
56
|
+
|
|
57
|
+
const cc = OpenccWasm.newWithEnum(
|
|
58
|
+
WasmOpenccConfig.S2t
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
console.log(cc.convert("汉字", false));
|
|
62
|
+
// 漢字
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Supported Configs
|
|
68
|
+
|
|
69
|
+
| Config | Description |
|
|
70
|
+
|---------|----------------------------------------------------|
|
|
71
|
+
| `s2t` | Simplified Chinese → Traditional Chinese |
|
|
72
|
+
| `s2tw` | Simplified Chinese → Taiwan Traditional |
|
|
73
|
+
| `s2twp` | Simplified Chinese → Taiwan Traditional (phrases) |
|
|
74
|
+
| `s2hk` | Simplified Chinese → Hong Kong Traditional |
|
|
75
|
+
| `t2s` | Traditional Chinese → Simplified Chinese |
|
|
76
|
+
| `t2tw` | Traditional Chinese → Taiwan Traditional |
|
|
77
|
+
| `t2twp` | Traditional Chinese → Taiwan Traditional (phrases) |
|
|
78
|
+
| `t2hk` | Traditional Chinese → Hong Kong Traditional |
|
|
79
|
+
| `tw2s` | Taiwan Traditional → Simplified Chinese |
|
|
80
|
+
| `tw2sp` | Taiwan Traditional → Simplified Chinese (phrases) |
|
|
81
|
+
| `tw2t` | Taiwan Traditional → Traditional Chinese |
|
|
82
|
+
| `tw2tp` | Taiwan Traditional → Traditional Chinese (phrases) |
|
|
83
|
+
| `hk2s` | Hong Kong Traditional → Simplified Chinese |
|
|
84
|
+
| `hk2t` | Hong Kong Traditional → Traditional Chinese |
|
|
85
|
+
| `jp2t` | Japanese Shinjitai → Traditional Chinese |
|
|
86
|
+
| `t2jp` | Traditional Chinese → Japanese Shinjitai |
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## API
|
|
91
|
+
|
|
92
|
+
### Constructor
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
const cc = new OpenccWasm("s2t");
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Parameters:
|
|
99
|
+
|
|
100
|
+
* `config` (optional): OpenCC config string
|
|
101
|
+
* default: `"s2t"`
|
|
102
|
+
|
|
103
|
+
Example:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const cc = new OpenccWasm("t2s");
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### convert
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
cc.convert(text, punctuation)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Parameters:
|
|
118
|
+
|
|
119
|
+
* `text`: input string
|
|
120
|
+
* `punctuation`: whether to convert punctuation variants
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
|
|
124
|
+
* converted string
|
|
125
|
+
|
|
126
|
+
Example:
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
cc.convert("汉字", false);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
### setConfig
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
cc.setConfig("t2s");
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
|
|
142
|
+
* `true` if valid
|
|
143
|
+
* `false` if invalid
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### getConfig
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
cc.getConfig();
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Returns current config string.
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### isValidConfig
|
|
158
|
+
|
|
159
|
+
```javascript
|
|
160
|
+
OpenccWasm.isValidConfig("s2t");
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
### getSupportedConfigs
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
OpenccWasm.getSupportedConfigs();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Returns all supported config strings.
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### zhoCheck
|
|
176
|
+
|
|
177
|
+
Detect Chinese script type.
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
cc.zhoCheck(text);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
|
|
185
|
+
| Value | Meaning |
|
|
186
|
+
|-------|---------------------|
|
|
187
|
+
| `0` | Unknown / mixed |
|
|
188
|
+
| `1` | Traditional Chinese |
|
|
189
|
+
| `2` | Simplified Chinese |
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Browser Example
|
|
194
|
+
|
|
195
|
+
```html
|
|
196
|
+
<!DOCTYPE html>
|
|
197
|
+
<html lang="en">
|
|
198
|
+
<head>
|
|
199
|
+
<meta charset="UTF-8">
|
|
200
|
+
<title>OpenCC WASM Demo</title>
|
|
201
|
+
</head>
|
|
202
|
+
<body>
|
|
203
|
+
|
|
204
|
+
<script type="module">
|
|
205
|
+
import init, {
|
|
206
|
+
OpenccWasm
|
|
207
|
+
} from "./pkg/opencc_fmmseg_wasm.js";
|
|
208
|
+
|
|
209
|
+
await init();
|
|
210
|
+
|
|
211
|
+
const cc = new OpenccWasm("s2t");
|
|
212
|
+
|
|
213
|
+
console.log(
|
|
214
|
+
cc.convert("汉字", false)
|
|
215
|
+
);
|
|
216
|
+
</script>
|
|
217
|
+
|
|
218
|
+
</body>
|
|
219
|
+
</html>
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## TypeScript Support
|
|
225
|
+
|
|
226
|
+
The package includes generated TypeScript definitions from `wasm-bindgen`.
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## Performance Notes
|
|
231
|
+
|
|
232
|
+
* WebAssembly build disables Rayon parallelism by default.
|
|
233
|
+
* Dictionaries are embedded into the WASM binary.
|
|
234
|
+
* Browser caching significantly improves subsequent loads.
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## Related Projects
|
|
239
|
+
|
|
240
|
+
* Rust backend: https://github.com/laisuk/opencc-fmmseg
|
|
241
|
+
* C API: https://github.com/laisuk/opencc-fmmseg/tree/master/capi/opencc-fmmseg-capi
|
|
242
|
+
* .NET: https://github.com/laisuk/OpenccNet
|
|
243
|
+
* Python: https://github.com/laisuk/opencc_purepy
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class OpenccWasm {
|
|
5
|
+
free(): void;
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
convert(text: string, punctuation: boolean): string;
|
|
8
|
+
debugPing(): string;
|
|
9
|
+
getConfig(): string;
|
|
10
|
+
getConfigId(): number;
|
|
11
|
+
static getSupportedConfigs(): string[];
|
|
12
|
+
static isValidConfig(config: string): boolean;
|
|
13
|
+
constructor(config?: string | null);
|
|
14
|
+
static newWithEnum(config?: WasmOpenccConfig | null): OpenccWasm;
|
|
15
|
+
setConfig(config: string): boolean;
|
|
16
|
+
setConfigEnum(config: WasmOpenccConfig): void;
|
|
17
|
+
zhoCheck(text: string): number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export enum WasmOpenccConfig {
|
|
21
|
+
S2t = 1,
|
|
22
|
+
S2tw = 2,
|
|
23
|
+
S2twp = 3,
|
|
24
|
+
S2hk = 4,
|
|
25
|
+
T2s = 5,
|
|
26
|
+
T2tw = 6,
|
|
27
|
+
T2twp = 7,
|
|
28
|
+
T2hk = 8,
|
|
29
|
+
Tw2s = 9,
|
|
30
|
+
Tw2sp = 10,
|
|
31
|
+
Tw2t = 11,
|
|
32
|
+
Tw2tp = 12,
|
|
33
|
+
Hk2s = 13,
|
|
34
|
+
Hk2t = 14,
|
|
35
|
+
Jp2t = 15,
|
|
36
|
+
T2jp = 16,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
40
|
+
|
|
41
|
+
export interface InitOutput {
|
|
42
|
+
readonly memory: WebAssembly.Memory;
|
|
43
|
+
readonly __wbg_openccwasm_free: (a: number, b: number) => void;
|
|
44
|
+
readonly openccwasm_convert: (a: number, b: number, c: number, d: number) => [number, number];
|
|
45
|
+
readonly openccwasm_debugPing: (a: number) => [number, number];
|
|
46
|
+
readonly openccwasm_getConfig: (a: number) => [number, number];
|
|
47
|
+
readonly openccwasm_getConfigId: (a: number) => number;
|
|
48
|
+
readonly openccwasm_getSupportedConfigs: () => [number, number];
|
|
49
|
+
readonly openccwasm_isValidConfig: (a: number, b: number) => number;
|
|
50
|
+
readonly openccwasm_new: (a: number, b: number) => [number, number, number];
|
|
51
|
+
readonly openccwasm_newWithEnum: (a: number) => [number, number, number];
|
|
52
|
+
readonly openccwasm_setConfig: (a: number, b: number, c: number) => number;
|
|
53
|
+
readonly openccwasm_setConfigEnum: (a: number, b: number) => void;
|
|
54
|
+
readonly openccwasm_zhoCheck: (a: number, b: number, c: number) => number;
|
|
55
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
56
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
57
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
58
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
59
|
+
readonly __externref_drop_slice: (a: number, b: number) => void;
|
|
60
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
61
|
+
readonly __wbindgen_start: () => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
68
|
+
* a precompiled `WebAssembly.Module`.
|
|
69
|
+
*
|
|
70
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
71
|
+
*
|
|
72
|
+
* @returns {InitOutput}
|
|
73
|
+
*/
|
|
74
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
78
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
79
|
+
*
|
|
80
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
81
|
+
*
|
|
82
|
+
* @returns {Promise<InitOutput>}
|
|
83
|
+
*/
|
|
84
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
/* @ts-self-types="./opencc_fmmseg_wasm.d.ts" */
|
|
2
|
+
|
|
3
|
+
export class OpenccWasm {
|
|
4
|
+
static __wrap(ptr) {
|
|
5
|
+
const obj = Object.create(OpenccWasm.prototype);
|
|
6
|
+
obj.__wbg_ptr = ptr;
|
|
7
|
+
OpenccWasmFinalization.register(obj, obj.__wbg_ptr, obj);
|
|
8
|
+
return obj;
|
|
9
|
+
}
|
|
10
|
+
__destroy_into_raw() {
|
|
11
|
+
const ptr = this.__wbg_ptr;
|
|
12
|
+
this.__wbg_ptr = 0;
|
|
13
|
+
OpenccWasmFinalization.unregister(this);
|
|
14
|
+
return ptr;
|
|
15
|
+
}
|
|
16
|
+
free() {
|
|
17
|
+
const ptr = this.__destroy_into_raw();
|
|
18
|
+
wasm.__wbg_openccwasm_free(ptr, 0);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @param {string} text
|
|
22
|
+
* @param {boolean} punctuation
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
convert(text, punctuation) {
|
|
26
|
+
let deferred2_0;
|
|
27
|
+
let deferred2_1;
|
|
28
|
+
try {
|
|
29
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
30
|
+
const len0 = WASM_VECTOR_LEN;
|
|
31
|
+
const ret = wasm.openccwasm_convert(this.__wbg_ptr, ptr0, len0, punctuation);
|
|
32
|
+
deferred2_0 = ret[0];
|
|
33
|
+
deferred2_1 = ret[1];
|
|
34
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
35
|
+
} finally {
|
|
36
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
debugPing() {
|
|
43
|
+
let deferred1_0;
|
|
44
|
+
let deferred1_1;
|
|
45
|
+
try {
|
|
46
|
+
const ret = wasm.openccwasm_debugPing(this.__wbg_ptr);
|
|
47
|
+
deferred1_0 = ret[0];
|
|
48
|
+
deferred1_1 = ret[1];
|
|
49
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
50
|
+
} finally {
|
|
51
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
getConfig() {
|
|
58
|
+
let deferred1_0;
|
|
59
|
+
let deferred1_1;
|
|
60
|
+
try {
|
|
61
|
+
const ret = wasm.openccwasm_getConfig(this.__wbg_ptr);
|
|
62
|
+
deferred1_0 = ret[0];
|
|
63
|
+
deferred1_1 = ret[1];
|
|
64
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
65
|
+
} finally {
|
|
66
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* @returns {number}
|
|
71
|
+
*/
|
|
72
|
+
getConfigId() {
|
|
73
|
+
const ret = wasm.openccwasm_getConfigId(this.__wbg_ptr);
|
|
74
|
+
return ret >>> 0;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @returns {string[]}
|
|
78
|
+
*/
|
|
79
|
+
static getSupportedConfigs() {
|
|
80
|
+
const ret = wasm.openccwasm_getSupportedConfigs();
|
|
81
|
+
var v1 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice();
|
|
82
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
|
|
83
|
+
return v1;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @param {string} config
|
|
87
|
+
* @returns {boolean}
|
|
88
|
+
*/
|
|
89
|
+
static isValidConfig(config) {
|
|
90
|
+
const ptr0 = passStringToWasm0(config, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
91
|
+
const len0 = WASM_VECTOR_LEN;
|
|
92
|
+
const ret = wasm.openccwasm_isValidConfig(ptr0, len0);
|
|
93
|
+
return ret !== 0;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @param {string | null} [config]
|
|
97
|
+
*/
|
|
98
|
+
constructor(config) {
|
|
99
|
+
var ptr0 = isLikeNone(config) ? 0 : passStringToWasm0(config, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
100
|
+
var len0 = WASM_VECTOR_LEN;
|
|
101
|
+
const ret = wasm.openccwasm_new(ptr0, len0);
|
|
102
|
+
if (ret[2]) {
|
|
103
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
104
|
+
}
|
|
105
|
+
this.__wbg_ptr = ret[0];
|
|
106
|
+
OpenccWasmFinalization.register(this, this.__wbg_ptr, this);
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @param {WasmOpenccConfig | null} [config]
|
|
111
|
+
* @returns {OpenccWasm}
|
|
112
|
+
*/
|
|
113
|
+
static newWithEnum(config) {
|
|
114
|
+
const ret = wasm.openccwasm_newWithEnum(isLikeNone(config) ? 0 : config);
|
|
115
|
+
if (ret[2]) {
|
|
116
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
117
|
+
}
|
|
118
|
+
return OpenccWasm.__wrap(ret[0]);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @param {string} config
|
|
122
|
+
* @returns {boolean}
|
|
123
|
+
*/
|
|
124
|
+
setConfig(config) {
|
|
125
|
+
const ptr0 = passStringToWasm0(config, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
126
|
+
const len0 = WASM_VECTOR_LEN;
|
|
127
|
+
const ret = wasm.openccwasm_setConfig(this.__wbg_ptr, ptr0, len0);
|
|
128
|
+
return ret !== 0;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @param {WasmOpenccConfig} config
|
|
132
|
+
*/
|
|
133
|
+
setConfigEnum(config) {
|
|
134
|
+
wasm.openccwasm_setConfigEnum(this.__wbg_ptr, config);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* @param {string} text
|
|
138
|
+
* @returns {number}
|
|
139
|
+
*/
|
|
140
|
+
zhoCheck(text) {
|
|
141
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
142
|
+
const len0 = WASM_VECTOR_LEN;
|
|
143
|
+
const ret = wasm.openccwasm_zhoCheck(this.__wbg_ptr, ptr0, len0);
|
|
144
|
+
return ret;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (Symbol.dispose) OpenccWasm.prototype[Symbol.dispose] = OpenccWasm.prototype.free;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @enum {1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16}
|
|
151
|
+
*/
|
|
152
|
+
export const WasmOpenccConfig = Object.freeze({
|
|
153
|
+
S2t: 1, "1": "S2t",
|
|
154
|
+
S2tw: 2, "2": "S2tw",
|
|
155
|
+
S2twp: 3, "3": "S2twp",
|
|
156
|
+
S2hk: 4, "4": "S2hk",
|
|
157
|
+
T2s: 5, "5": "T2s",
|
|
158
|
+
T2tw: 6, "6": "T2tw",
|
|
159
|
+
T2twp: 7, "7": "T2twp",
|
|
160
|
+
T2hk: 8, "8": "T2hk",
|
|
161
|
+
Tw2s: 9, "9": "Tw2s",
|
|
162
|
+
Tw2sp: 10, "10": "Tw2sp",
|
|
163
|
+
Tw2t: 11, "11": "Tw2t",
|
|
164
|
+
Tw2tp: 12, "12": "Tw2tp",
|
|
165
|
+
Hk2s: 13, "13": "Hk2s",
|
|
166
|
+
Hk2t: 14, "14": "Hk2t",
|
|
167
|
+
Jp2t: 15, "15": "Jp2t",
|
|
168
|
+
T2jp: 16, "16": "T2jp",
|
|
169
|
+
});
|
|
170
|
+
function __wbg_get_imports() {
|
|
171
|
+
const import0 = {
|
|
172
|
+
__proto__: null,
|
|
173
|
+
__wbg___wbindgen_throw_1506f2235d1bdba0: function(arg0, arg1) {
|
|
174
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
175
|
+
},
|
|
176
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
177
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
178
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
179
|
+
return ret;
|
|
180
|
+
},
|
|
181
|
+
__wbindgen_init_externref_table: function() {
|
|
182
|
+
const table = wasm.__wbindgen_externrefs;
|
|
183
|
+
const offset = table.grow(4);
|
|
184
|
+
table.set(0, undefined);
|
|
185
|
+
table.set(offset + 0, undefined);
|
|
186
|
+
table.set(offset + 1, null);
|
|
187
|
+
table.set(offset + 2, true);
|
|
188
|
+
table.set(offset + 3, false);
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
return {
|
|
192
|
+
__proto__: null,
|
|
193
|
+
"./opencc_fmmseg_wasm_bg.js": import0,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const OpenccWasmFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
198
|
+
? { register: () => {}, unregister: () => {} }
|
|
199
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_openccwasm_free(ptr, 1));
|
|
200
|
+
|
|
201
|
+
function getArrayJsValueFromWasm0(ptr, len) {
|
|
202
|
+
ptr = ptr >>> 0;
|
|
203
|
+
const mem = getDataViewMemory0();
|
|
204
|
+
const result = [];
|
|
205
|
+
for (let i = ptr; i < ptr + 4 * len; i += 4) {
|
|
206
|
+
result.push(wasm.__wbindgen_externrefs.get(mem.getUint32(i, true)));
|
|
207
|
+
}
|
|
208
|
+
wasm.__externref_drop_slice(ptr, len);
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let cachedDataViewMemory0 = null;
|
|
213
|
+
function getDataViewMemory0() {
|
|
214
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
215
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
216
|
+
}
|
|
217
|
+
return cachedDataViewMemory0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function getStringFromWasm0(ptr, len) {
|
|
221
|
+
return decodeText(ptr >>> 0, len);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let cachedUint8ArrayMemory0 = null;
|
|
225
|
+
function getUint8ArrayMemory0() {
|
|
226
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
227
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
228
|
+
}
|
|
229
|
+
return cachedUint8ArrayMemory0;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function isLikeNone(x) {
|
|
233
|
+
return x === undefined || x === null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
237
|
+
if (realloc === undefined) {
|
|
238
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
239
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
240
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
241
|
+
WASM_VECTOR_LEN = buf.length;
|
|
242
|
+
return ptr;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let len = arg.length;
|
|
246
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
247
|
+
|
|
248
|
+
const mem = getUint8ArrayMemory0();
|
|
249
|
+
|
|
250
|
+
let offset = 0;
|
|
251
|
+
|
|
252
|
+
for (; offset < len; offset++) {
|
|
253
|
+
const code = arg.charCodeAt(offset);
|
|
254
|
+
if (code > 0x7F) break;
|
|
255
|
+
mem[ptr + offset] = code;
|
|
256
|
+
}
|
|
257
|
+
if (offset !== len) {
|
|
258
|
+
if (offset !== 0) {
|
|
259
|
+
arg = arg.slice(offset);
|
|
260
|
+
}
|
|
261
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
262
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
263
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
264
|
+
|
|
265
|
+
offset += ret.written;
|
|
266
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
WASM_VECTOR_LEN = offset;
|
|
270
|
+
return ptr;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function takeFromExternrefTable0(idx) {
|
|
274
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
275
|
+
wasm.__externref_table_dealloc(idx);
|
|
276
|
+
return value;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
280
|
+
cachedTextDecoder.decode();
|
|
281
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
282
|
+
let numBytesDecoded = 0;
|
|
283
|
+
function decodeText(ptr, len) {
|
|
284
|
+
numBytesDecoded += len;
|
|
285
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
286
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
287
|
+
cachedTextDecoder.decode();
|
|
288
|
+
numBytesDecoded = len;
|
|
289
|
+
}
|
|
290
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const cachedTextEncoder = new TextEncoder();
|
|
294
|
+
|
|
295
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
296
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
297
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
298
|
+
view.set(buf);
|
|
299
|
+
return {
|
|
300
|
+
read: arg.length,
|
|
301
|
+
written: buf.length
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
let WASM_VECTOR_LEN = 0;
|
|
307
|
+
|
|
308
|
+
let wasmModule, wasmInstance, wasm;
|
|
309
|
+
function __wbg_finalize_init(instance, module) {
|
|
310
|
+
wasmInstance = instance;
|
|
311
|
+
wasm = instance.exports;
|
|
312
|
+
wasmModule = module;
|
|
313
|
+
cachedDataViewMemory0 = null;
|
|
314
|
+
cachedUint8ArrayMemory0 = null;
|
|
315
|
+
wasm.__wbindgen_start();
|
|
316
|
+
return wasm;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function __wbg_load(module, imports) {
|
|
320
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
321
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
322
|
+
try {
|
|
323
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
324
|
+
} catch (e) {
|
|
325
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
326
|
+
|
|
327
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
328
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
329
|
+
|
|
330
|
+
} else { throw e; }
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const bytes = await module.arrayBuffer();
|
|
335
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
336
|
+
} else {
|
|
337
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
338
|
+
|
|
339
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
340
|
+
return { instance, module };
|
|
341
|
+
} else {
|
|
342
|
+
return instance;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function expectedResponseType(type) {
|
|
347
|
+
switch (type) {
|
|
348
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
349
|
+
}
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function initSync(module) {
|
|
355
|
+
if (wasm !== undefined) return wasm;
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
if (module !== undefined) {
|
|
359
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
360
|
+
({module} = module)
|
|
361
|
+
} else {
|
|
362
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const imports = __wbg_get_imports();
|
|
367
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
368
|
+
module = new WebAssembly.Module(module);
|
|
369
|
+
}
|
|
370
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
371
|
+
return __wbg_finalize_init(instance, module);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async function __wbg_init(module_or_path) {
|
|
375
|
+
if (wasm !== undefined) return wasm;
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
if (module_or_path !== undefined) {
|
|
379
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
380
|
+
({module_or_path} = module_or_path)
|
|
381
|
+
} else {
|
|
382
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (module_or_path === undefined) {
|
|
387
|
+
module_or_path = new URL('opencc_fmmseg_wasm_bg.wasm', import.meta.url);
|
|
388
|
+
}
|
|
389
|
+
const imports = __wbg_get_imports();
|
|
390
|
+
|
|
391
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
392
|
+
module_or_path = fetch(module_or_path);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
396
|
+
|
|
397
|
+
return __wbg_finalize_init(instance, module);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@laisuk/opencc-fmmseg-wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "WebAssembly bindings for opencc-fmmseg, a high-performance OpenCC-compatible Simplified/Traditional Chinese converter.",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/laisuk/opencc-fmmseg-wasm"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"opencc_fmmseg_wasm_bg.wasm",
|
|
13
|
+
"opencc_fmmseg_wasm.js",
|
|
14
|
+
"opencc_fmmseg_wasm.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"main": "opencc_fmmseg_wasm.js",
|
|
17
|
+
"types": "opencc_fmmseg_wasm.d.ts",
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./snippets/*"
|
|
20
|
+
]
|
|
21
|
+
}
|