lingotweaker-wasm 0.2.4 → 0.2.6

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
@@ -59,6 +59,47 @@ Or, from an ES module: `import { LtEngine } from "lingotweaker-wasm/node"`.
59
59
  `today` is required in practice: WebAssembly has no clock, so the date is
60
60
  passed in and pinned for the engine's date filters.
61
61
 
62
+ ## Language detection
63
+
64
+ `detect_json` is a free function, not an `LtEngine` method: choosing the
65
+ language is what picks the engine, so you ask before any engine exists. It
66
+ needs no data pack — the identifier model and the Nordum marker list are
67
+ embedded in the wasm binary.
68
+
69
+ ```js
70
+ import init, { detect_json, LtEngine } from "lingotweaker-wasm";
71
+
72
+ await init();
73
+
74
+ const { resolved, detected, candidates } = JSON.parse(
75
+ detect_json("Jag arbetar inte i dag, men jag kommer hem efter jobbet."),
76
+ );
77
+ // resolved: "sv" detected: {language: "sv", confidence: 0.99, source: "model"}
78
+ // candidates: the five best-scoring languages, best first
79
+
80
+ const engine = new LtEngine(
81
+ resolved ?? "en-US",
82
+ pack,
83
+ JSON.stringify({ today: new Date().toISOString() }),
84
+ );
85
+ ```
86
+
87
+ `resolved` is `null` when there is not enough evidence to answer — that is a
88
+ normal outcome, not an error, and the caller keeps the language it already had.
89
+ `detected.source` says which layer decided: `"lexicon"` for a word no other
90
+ language we ship contains (this is how Nordum is detected), `"model"` for the
91
+ statistical model.
92
+
93
+ Detection is deliberately conservative on short fragments, which is what an
94
+ editor sees. The optional second argument overrides the thresholds:
95
+
96
+ ```js
97
+ detect_json(text, JSON.stringify({ minChars: 40, minConfidence: 0.3, minMargin: 2 }));
98
+ ```
99
+
100
+ Each field defaults to the engine default, so `{}` is the same call as no
101
+ argument.
102
+
62
103
  ## Data
63
104
 
64
105
  The package ships **code only** and depends on the code-only
package/node/lt_wasm.d.ts CHANGED
@@ -55,4 +55,24 @@ export class LtEngine {
55
55
  variant(): string | undefined;
56
56
  }
57
57
 
58
+ /**
59
+ * Detect the language of `text` and return the decision as JSON:
60
+ * `{"resolved":"sv","detected":{…},"candidates":[…]}`.
61
+ *
62
+ * `gates` is an optional JSON object overriding the thresholds
63
+ * (`{"minChars":20,"minConfidence":0.6,"minMargin":1.5}`); absent means
64
+ * [`Gates::default()`].
65
+ *
66
+ * `resolved` is `null` when the gates abstain — the text is too short, too
67
+ * ambiguous, or carries no marker. That is a normal outcome, not an error: the
68
+ * caller keeps whatever language it was already using.
69
+ *
70
+ * ```js
71
+ * import init, { detect_json } from "./pkg/lt_wasm.js";
72
+ * await init();
73
+ * JSON.parse(detect_json("Jag kommer hem efter jobbet.")).resolved; // "sv"
74
+ * ```
75
+ */
76
+ export function detect_json(text: string, gates?: string | null): string;
77
+
58
78
  export function start(): void;
package/node/lt_wasm.js CHANGED
@@ -188,6 +188,51 @@ class LtEngine {
188
188
  if (Symbol.dispose) LtEngine.prototype[Symbol.dispose] = LtEngine.prototype.free;
189
189
  exports.LtEngine = LtEngine;
190
190
 
191
+ /**
192
+ * Detect the language of `text` and return the decision as JSON:
193
+ * `{"resolved":"sv","detected":{…},"candidates":[…]}`.
194
+ *
195
+ * `gates` is an optional JSON object overriding the thresholds
196
+ * (`{"minChars":20,"minConfidence":0.6,"minMargin":1.5}`); absent means
197
+ * [`Gates::default()`].
198
+ *
199
+ * `resolved` is `null` when the gates abstain — the text is too short, too
200
+ * ambiguous, or carries no marker. That is a normal outcome, not an error: the
201
+ * caller keeps whatever language it was already using.
202
+ *
203
+ * ```js
204
+ * import init, { detect_json } from "./pkg/lt_wasm.js";
205
+ * await init();
206
+ * JSON.parse(detect_json("Jag kommer hem efter jobbet.")).resolved; // "sv"
207
+ * ```
208
+ * @param {string} text
209
+ * @param {string | null} [gates]
210
+ * @returns {string}
211
+ */
212
+ function detect_json(text, gates) {
213
+ let deferred4_0;
214
+ let deferred4_1;
215
+ try {
216
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
217
+ const len0 = WASM_VECTOR_LEN;
218
+ var ptr1 = isLikeNone(gates) ? 0 : passStringToWasm0(gates, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
219
+ var len1 = WASM_VECTOR_LEN;
220
+ const ret = wasm.detect_json(ptr0, len0, ptr1, len1);
221
+ var ptr3 = ret[0];
222
+ var len3 = ret[1];
223
+ if (ret[3]) {
224
+ ptr3 = 0; len3 = 0;
225
+ throw takeFromExternrefTable0(ret[2]);
226
+ }
227
+ deferred4_0 = ptr3;
228
+ deferred4_1 = len3;
229
+ return getStringFromWasm0(ptr3, len3);
230
+ } finally {
231
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
232
+ }
233
+ }
234
+ exports.detect_json = detect_json;
235
+
191
236
  function start() {
192
237
  wasm.start();
193
238
  }
Binary file
@@ -2,6 +2,7 @@
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
4
  export const __wbg_ltengine_free: (a: number, b: number) => void;
5
+ export const detect_json: (a: number, b: number, c: number, d: number) => [number, number, number, number];
5
6
  export const ltengine_active_rule_count: (a: number) => number;
6
7
  export const ltengine_check_json: (a: number, b: number, c: number) => [number, number, number, number];
7
8
  export const ltengine_check_matches_json: (a: number, b: number, c: number) => [number, number, number, number];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lingotweaker-wasm",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "LingoTweaker proofreading engine compiled to WebAssembly (browser and Node)",
5
5
  "license": "LGPL-2.1-or-later",
6
6
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "module": "./web/lt_wasm.js",
23
23
  "types": "./web/lt_wasm.d.ts",
24
24
  "dependencies": {
25
- "lingotweaker-data": "0.2.4"
25
+ "lingotweaker-data": "0.2.6"
26
26
  },
27
27
  "exports": {
28
28
  ".": {
package/web/lt_wasm.d.ts CHANGED
@@ -55,6 +55,26 @@ export class LtEngine {
55
55
  variant(): string | undefined;
56
56
  }
57
57
 
58
+ /**
59
+ * Detect the language of `text` and return the decision as JSON:
60
+ * `{"resolved":"sv","detected":{…},"candidates":[…]}`.
61
+ *
62
+ * `gates` is an optional JSON object overriding the thresholds
63
+ * (`{"minChars":20,"minConfidence":0.6,"minMargin":1.5}`); absent means
64
+ * [`Gates::default()`].
65
+ *
66
+ * `resolved` is `null` when the gates abstain — the text is too short, too
67
+ * ambiguous, or carries no marker. That is a normal outcome, not an error: the
68
+ * caller keeps whatever language it was already using.
69
+ *
70
+ * ```js
71
+ * import init, { detect_json } from "./pkg/lt_wasm.js";
72
+ * await init();
73
+ * JSON.parse(detect_json("Jag kommer hem efter jobbet.")).resolved; // "sv"
74
+ * ```
75
+ */
76
+ export function detect_json(text: string, gates?: string | null): string;
77
+
58
78
  export function start(): void;
59
79
 
60
80
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
@@ -62,6 +82,7 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
62
82
  export interface InitOutput {
63
83
  readonly memory: WebAssembly.Memory;
64
84
  readonly __wbg_ltengine_free: (a: number, b: number) => void;
85
+ readonly detect_json: (a: number, b: number, c: number, d: number) => [number, number, number, number];
65
86
  readonly ltengine_active_rule_count: (a: number) => number;
66
87
  readonly ltengine_check_json: (a: number, b: number, c: number) => [number, number, number, number];
67
88
  readonly ltengine_check_matches_json: (a: number, b: number, c: number) => [number, number, number, number];
package/web/lt_wasm.js CHANGED
@@ -187,6 +187,50 @@ export class LtEngine {
187
187
  }
188
188
  if (Symbol.dispose) LtEngine.prototype[Symbol.dispose] = LtEngine.prototype.free;
189
189
 
190
+ /**
191
+ * Detect the language of `text` and return the decision as JSON:
192
+ * `{"resolved":"sv","detected":{…},"candidates":[…]}`.
193
+ *
194
+ * `gates` is an optional JSON object overriding the thresholds
195
+ * (`{"minChars":20,"minConfidence":0.6,"minMargin":1.5}`); absent means
196
+ * [`Gates::default()`].
197
+ *
198
+ * `resolved` is `null` when the gates abstain — the text is too short, too
199
+ * ambiguous, or carries no marker. That is a normal outcome, not an error: the
200
+ * caller keeps whatever language it was already using.
201
+ *
202
+ * ```js
203
+ * import init, { detect_json } from "./pkg/lt_wasm.js";
204
+ * await init();
205
+ * JSON.parse(detect_json("Jag kommer hem efter jobbet.")).resolved; // "sv"
206
+ * ```
207
+ * @param {string} text
208
+ * @param {string | null} [gates]
209
+ * @returns {string}
210
+ */
211
+ export function detect_json(text, gates) {
212
+ let deferred4_0;
213
+ let deferred4_1;
214
+ try {
215
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
216
+ const len0 = WASM_VECTOR_LEN;
217
+ var ptr1 = isLikeNone(gates) ? 0 : passStringToWasm0(gates, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
218
+ var len1 = WASM_VECTOR_LEN;
219
+ const ret = wasm.detect_json(ptr0, len0, ptr1, len1);
220
+ var ptr3 = ret[0];
221
+ var len3 = ret[1];
222
+ if (ret[3]) {
223
+ ptr3 = 0; len3 = 0;
224
+ throw takeFromExternrefTable0(ret[2]);
225
+ }
226
+ deferred4_0 = ptr3;
227
+ deferred4_1 = len3;
228
+ return getStringFromWasm0(ptr3, len3);
229
+ } finally {
230
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
231
+ }
232
+ }
233
+
190
234
  export function start() {
191
235
  wasm.start();
192
236
  }
Binary file
@@ -2,6 +2,7 @@
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
4
  export const __wbg_ltengine_free: (a: number, b: number) => void;
5
+ export const detect_json: (a: number, b: number, c: number, d: number) => [number, number, number, number];
5
6
  export const ltengine_active_rule_count: (a: number) => number;
6
7
  export const ltengine_check_json: (a: number, b: number, c: number) => [number, number, number, number];
7
8
  export const ltengine_check_matches_json: (a: number, b: number, c: number) => [number, number, number, number];