-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-factory.mjs
More file actions
778 lines (747 loc) · 24.9 KB
/
Copy pathruntime-factory.mjs
File metadata and controls
778 lines (747 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
import {
assertLatticeProvider,
assertSemiringProvider,
assertScalarWfstProvider,
normalizeLatticeProviderOptions,
normalizeSemiringProviderOptions,
normalizeScalarWfstProviderOptions,
} from "@vinary-tree/vinary-tree-interop";
const DEFAULT_BATCH_SIZE = 256;
const semiringOwners = new WeakMap();
const semiringProperties = new Map([
["hashable", 1n],
["idempotent-plus", 2n],
["k-closed", 4n],
["zero-sum-free", 8n],
["commutative-times", 16n],
["totally-ordered", 32n],
["nonnegative", 64n],
]);
function defineResourceMetadata(resource, runtimeIdentity, unitDomain) {
Object.defineProperties(resource, {
interfaceId: { value: "vt.dictionary.v1", enumerable: true },
runtimeIdentity: { value: runtimeIdentity, enumerable: true },
valueDomain: { value: "optional-u64", enumerable: true },
unitDomain: { value: unitDomain, enumerable: true },
});
return resource;
}
function requireDictionary(dictionary, runtimeIdentity) {
if (dictionary?.interfaceId !== "vt.dictionary.v1") {
throw new TypeError("resource does not implement vt.dictionary.v1");
}
if (dictionary.runtimeIdentity !== runtimeIdentity) {
throw new TypeError("resource belongs to a different Vinary Tree runtime");
}
return dictionary;
}
function requireWfst(wfst, runtimeIdentity) {
if (wfst?.interfaceId !== "vt.scalar-wfst.1") {
throw new TypeError("resource does not implement vt.scalar-wfst.1");
}
if (wfst.runtimeIdentity !== runtimeIdentity) {
throw new TypeError("WFST belongs to a different Vinary Tree runtime");
}
return wfst;
}
function requireLattice(lattice, runtimeIdentity, domainId = undefined) {
if (lattice?.interfaceId !== "vt.lattice.val.1") {
throw new TypeError("resource does not implement vt.lattice.val.1");
}
if (lattice.runtimeIdentity !== runtimeIdentity) {
throw new TypeError("lattice belongs to a different Vinary Tree runtime");
}
if (domainId !== undefined && lattice.domainId !== domainId) {
throw new TypeError("lattice belongs to a different semantic domain");
}
return lattice;
}
function requireSemiringWeight(value, runtimeIdentity, semiring) {
if (value?.interfaceId !== "vt.semiring.val1" || value.runtimeIdentity !== runtimeIdentity ||
semiringOwners.get(value) !== semiring) {
throw new TypeError("semiring weight belongs to a different operation context");
}
return value;
}
function installCursorProtocol(raw) {
if (raw.QueryCursor.prototype[Symbol.iterator]) return;
const rawNext = raw.QueryCursor.prototype.next;
Object.defineProperties(raw.QueryCursor.prototype, {
[Symbol.iterator]: {
value() { return this; },
},
next: {
value() {
const result = rawNext.call(this);
if (result.done) this.close();
return result;
},
},
reduceBatches: {
value(reducer, initial, batchSize = DEFAULT_BATCH_SIZE) {
if (!Number.isSafeInteger(batchSize) || batchSize <= 0) {
throw new RangeError("batchSize must be a positive safe integer");
}
let accumulator = initial;
try {
for (;;) {
const batch = this.nextBatch(batchSize);
if (batch.length === 0) return accumulator;
accumulator = reducer(accumulator, batch);
}
} finally {
this.close();
}
},
},
return: {
value() { this.close(); return { done: true, value: undefined }; },
},
[Symbol.dispose]: { value() { this.close(); } },
});
}
class MaterializedEntryCursor {
#entries;
#offset = 0;
constructor(entries) {
this.#entries = entries;
this.size = entries.length;
this.identity = null;
}
[Symbol.iterator]() { return this; }
next() {
if (this.#offset >= this.#entries.length) {
this.close();
return { done: true, value: undefined };
}
return { done: false, value: this.#entries[this.#offset++] };
}
nextBatch(maximum) {
if (!Number.isSafeInteger(maximum) || maximum <= 0) {
throw new RangeError("batch size must be a positive safe integer");
}
const batch = this.#entries.slice(this.#offset, this.#offset + maximum);
this.#offset += batch.length;
return batch;
}
reduceBatches(reducer, initial, batchSize = DEFAULT_BATCH_SIZE) {
let accumulator = initial;
try {
for (;;) {
const batch = this.nextBatch(batchSize);
if (batch.length === 0) return accumulator;
accumulator = reducer(accumulator, batch);
}
} finally {
this.close();
}
}
return() { this.close(); return { done: true, value: undefined }; }
close() { this.#offset = this.#entries.length; }
[Symbol.dispose]() { this.close(); }
}
class WasmEntryCursor {
#raw;
#pending = [];
#offset = 0;
constructor(raw) {
this.#raw = raw;
this.size = raw.size;
this.identity = null;
}
[Symbol.iterator]() { return this; }
next() {
if (this.#offset >= this.#pending.length) {
this.#pending = this.#fetch(DEFAULT_BATCH_SIZE);
this.#offset = 0;
}
if (this.#pending.length === 0) {
this.close();
return { done: true, value: undefined };
}
return { done: false, value: this.#pending[this.#offset++] };
}
nextBatch(maximum) {
if (!Number.isSafeInteger(maximum) || maximum <= 0) {
throw new RangeError("batch size must be a positive safe integer");
}
const result = [];
while (result.length < maximum) {
while (this.#offset < this.#pending.length && result.length < maximum) {
result.push(this.#pending[this.#offset++]);
}
if (result.length === maximum || this.#raw === null) break;
this.#pending = this.#fetch(maximum - result.length);
this.#offset = 0;
if (this.#pending.length === 0) {
this.close();
break;
}
}
return result;
}
#fetch(maximum) {
return this.#raw === null ? [] : Array.from(this.#raw.nextBatch(maximum));
}
reduceBatches(reducer, initial, batchSize = DEFAULT_BATCH_SIZE) {
let accumulator = initial;
try {
for (;;) {
const batch = this.nextBatch(batchSize);
if (batch.length === 0) return accumulator;
accumulator = reducer(accumulator, batch);
}
} finally {
this.close();
}
}
return() { this.close(); return { done: true, value: undefined }; }
close() {
if (this.#raw !== null) {
this.#raw.close();
this.#raw = null;
this.#pending = [];
this.#offset = 0;
}
}
[Symbol.dispose]() { this.close(); }
}
function installDictionaryProtocol(raw, runtimeIdentity) {
const prototype = raw.Dictionary.prototype;
if (prototype.lookup) return;
const rawPut = prototype.put;
const rawPutBytes = prototype.putBytes;
const rawPutU64 = prototype.putU64;
const rawRemove = prototype.remove;
const rawRemoveBytes = prototype.removeBytes;
const rawRemoveU64 = prototype.removeU64;
const rawLookup = prototype.get;
const rawLookupBytes = prototype.getBytes;
const rawLookupU64 = prototype.getU64;
const rawSnapshotEntries = prototype.snapshotEntries;
const rawOpenEntryStream = prototype.openEntryStream;
const rawAlgebra = prototype.algebra;
const select = (term, text, bytes, u64) => {
if (term instanceof BigUint64Array) return u64;
if (term instanceof Uint8Array) return bytes;
if (typeof term === "string") return text;
throw new TypeError("dictionary key must be a string, Uint8Array, or BigUint64Array");
};
Object.defineProperties(prototype, {
put: {
value(term, value = null) {
return select(term, rawPut, rawPutBytes, rawPutU64).call(this, term, value);
},
},
set: {
value(term, value = null) { this.put(term, value); return this; },
},
remove: {
value(term) {
return select(term, rawRemove, rawRemoveBytes, rawRemoveU64).call(this, term);
},
},
delete: { value(term) { return this.remove(term); } },
lookup: {
value(term) {
return select(term, rawLookup, rawLookupBytes, rawLookupU64).call(this, term);
},
},
lookupU64: { value(term) { return rawLookupU64.call(this, term); } },
get: {
value(term) {
const result = this.lookup(term);
return result.found ? result.value : undefined;
},
},
getU64: {
value(term) {
const result = this.lookupU64(term);
return result.found ? result.value : undefined;
},
},
has: { value(term) { return this.lookup(term).found; } },
hasU64: { value(term) { return this.lookupU64(term).found; } },
snapshotEntries: {
value() {
return Object.freeze(Array.from(
rawSnapshotEntries.call(this),
([key, value]) => Object.freeze([key, value]),
));
},
},
entries: { value() { return this.snapshotEntries()[Symbol.iterator](); } },
[Symbol.iterator]: { value() { return this.entries(); } },
keys: {
value: function* keys() { for (const [key] of this) yield key; },
},
values: {
value: function* values() { for (const [, value] of this) yield value; },
},
streamEntries: {
value() {
return typeof rawOpenEntryStream === "function"
? new WasmEntryCursor(rawOpenEntryStream.call(this))
: new MaterializedEntryCursor(this.snapshotEntries());
},
},
algebra: {
value(right, operation, valueMerge = "last") {
const result = rawAlgebra.call(
this,
requireDictionary(right, runtimeIdentity),
operation,
valueMerge,
);
return defineResourceMetadata(result, runtimeIdentity, this.unitDomain);
},
},
union: {
value(right, valueMerge = "last") { return this.algebra(right, "union", valueMerge); },
},
intersection: {
value(right, valueMerge = "lattice-meet") {
return this.algebra(right, "intersection", valueMerge);
},
},
difference: {
value(right) { return this.algebra(right, "difference"); },
},
symmetricDifference: {
value(right) { return this.algebra(right, "symmetric-difference"); },
},
forEach: {
value(callback, thisArg = undefined) {
for (const [key, value] of this) callback.call(thisArg, value, key, this);
},
},
toMap: {
value() {
if (this.unitDomain !== "unicode") {
throw new TypeError("toMap is defined only for value-equal JavaScript string keys");
}
return new Map(this);
},
},
[Symbol.dispose]: { value() { this.close(); } },
});
}
function query(transducer, input, maximumDistance, order = "traversal") {
if (typeof input === "string") {
return transducer.queryText(input, maximumDistance, order);
}
if (input instanceof Uint8Array && !(input instanceof BigUint64Array)) {
return transducer.queryBytes(input, maximumDistance);
}
if (input instanceof BigUint64Array) {
return transducer.queryU64(input, maximumDistance);
}
if (input instanceof transducer.constructor.__phoneticPatternClass) {
return transducer.queryPattern(input, maximumDistance);
}
throw new TypeError("query must be a string, Uint8Array, BigUint64Array, or PhoneticPattern");
}
function cacheLimit(value, fallback, name) {
const selected = value ?? fallback;
if (!Number.isSafeInteger(selected) || selected < 0 || selected > 0xffff_ffff) {
throw new RangeError(`${name} must be an integer from 0 through 4294967295`);
}
return selected;
}
function requireScalarWfstProvider(provider) {
assertScalarWfstProvider(provider);
return provider;
}
function hostWfstOptions(options) {
const { unitDomain, weightDomain, lazy, acyclic } = normalizeScalarWfstProviderOptions(options);
return [unitDomain, weightDomain, lazy, acyclic];
}
function hostLattice(provider, options) {
assertLatticeProvider(provider);
const { domainId } = normalizeLatticeProviderOptions(options);
return [provider, domainId];
}
function hostSemiring(provider, options) {
assertSemiringProvider(provider);
const normalized = normalizeSemiringProviderOptions(options);
const propertyBits = normalized.properties.reduce((bits, property) => {
const bit = semiringProperties.get(property);
if (bit === undefined) throw new TypeError(`unknown semiring property ${property}`);
return bits | bit;
}, 0n);
return { provider, ...normalized, propertyBits };
}
function latticeHandles(values, runtimeIdentity, domainId, maximum) {
if (!Array.isArray(values) || values.length > maximum) {
throw new TypeError(`lattice values must be an array of at most ${maximum} entries`);
}
return Uint32Array.from(values, (value) =>
requireLattice(value, runtimeIdentity, domainId).registryHandle());
}
function semiringHandles(values, runtimeIdentity, semiring, maximum) {
if (!Array.isArray(values) || values.length > maximum) {
throw new TypeError(`semiring values must be an array of at most ${maximum} entries`);
}
return Uint32Array.from(values, (value) =>
requireSemiringWeight(value, runtimeIdentity, semiring).registryHandle());
}
function installQueryCacheProtocol(raw) {
if (!raw.QueryCache || raw.QueryCache.prototype.query) return;
const prototype = raw.QueryCache.prototype;
const rawClear = prototype.clear;
const rawResetStats = prototype.resetStats;
Object.defineProperties(prototype, {
query: {
value(input, maximumDistance, order = "traversal") {
if (typeof input === "string") return this.queryText(input, maximumDistance, order);
if (input instanceof BigUint64Array) return this.queryU64(input, maximumDistance, order);
if (input instanceof Uint8Array) return this.queryBytes(input, maximumDistance, order);
throw new TypeError("cached query requires text, Uint8Array, or BigUint64Array");
},
},
clear: {
value() { rawClear.call(this); return this; },
},
resetStats: {
value() { rawResetStats.call(this); return this; },
},
[Symbol.dispose]: { value() { this.close(); } },
});
}
/** Build all public project namespaces over exactly one initialized runtime. */
export function createRuntime(raw) {
const runtimeIdentity = Object.freeze({ implementation: "vinary-tree-wasm-v1" });
installCursorProtocol(raw);
installDictionaryProtocol(raw, runtimeIdentity);
installQueryCacheProtocol(raw);
Object.defineProperty(raw.Transducer, "__phoneticPatternClass", {
value: raw.PhoneticPattern,
});
if (!raw.Transducer.prototype.query) {
Object.defineProperties(raw.Transducer.prototype, {
query: {
value(input, maximumDistance, order) {
return query(this, input, maximumDistance, order);
},
},
[Symbol.dispose]: { value() { this.close(); } },
});
}
Object.defineProperties(raw.Wfst.prototype, {
interfaceId: { value: "vt.scalar-wfst.1" },
runtimeIdentity: { value: runtimeIdentity },
[Symbol.dispose]: { value() { this.close(); } },
});
if (raw.Lattice && !raw.Lattice.prototype.joinMany) {
const rawJoinMany = raw.Lattice.prototype.joinManyHandles;
const rawMeetMany = raw.Lattice.prototype.meetManyHandles;
Object.defineProperties(raw.Lattice.prototype, {
interfaceId: { value: "vt.lattice.val.1" },
runtimeIdentity: { value: runtimeIdentity },
joinMany: {
value(others) {
return rawJoinMany.call(
this,
latticeHandles(others, runtimeIdentity, this.domainId, DEFAULT_BATCH_SIZE),
);
},
},
meetMany: {
value(others) {
return rawMeetMany.call(
this,
latticeHandles(others, runtimeIdentity, this.domainId, DEFAULT_BATCH_SIZE),
);
},
},
[Symbol.dispose]: { value() { this.close(); } },
});
}
if (raw.Semiring && !raw.Semiring.prototype.plusMany) {
const context = raw.Semiring.prototype;
const weight = raw.SemiringWeight.prototype;
const rawZero = context.zero;
const rawOne = context.one;
const rawPlus = context.plus;
const rawTimes = context.times;
const rawEqual = context.equal;
const rawApproximatelyEqual = context.approximatelyEqual;
const rawNaturalOrder = context.naturalOrder;
const rawStableBytes = context.stableBytes;
const rawDiagnostic = context.diagnostic;
const rawDiagnosticWeight = context.diagnosticWeight;
const rawPlusMany = context.plusManyHandles;
const rawTimesMany = context.timesManyHandles;
const rawDivide = context.divide;
const rawLeftDivide = context.leftDivide;
const rawStar = context.star;
const rawNumericalValue = context.numericalValue;
const rawQuantize = context.quantize;
const rawToProbability = context.toProbability;
const rawValidateLaws = context.validateLawHandles;
const rawClone = weight.clone;
const attach = (value, owner) => {
if (value !== null && value !== undefined) semiringOwners.set(value, owner);
return value ?? null;
};
Object.defineProperties(weight, {
interfaceId: { value: "vt.semiring.val1" },
runtimeIdentity: { value: runtimeIdentity },
clone: {
value() {
const owner = semiringOwners.get(this);
if (owner === undefined) throw new TypeError("semiring weight has no operation context");
return attach(rawClone.call(this), owner);
},
},
[Symbol.dispose]: { value() { this.close(); } },
});
Object.defineProperties(context, {
interfaceId: { value: "vt.semiring.ctx1" },
runtimeIdentity: { value: runtimeIdentity },
zero: { value() { return attach(rawZero.call(this), this); } },
one: { value() { return attach(rawOne.call(this), this); } },
plus: {
value(left, right) {
return attach(rawPlus.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
), this);
},
},
times: {
value(left, right) {
return attach(rawTimes.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
), this);
},
},
equal: {
value(left, right) {
return rawEqual.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
);
},
},
approximatelyEqual: {
value(left, right, epsilon) {
return rawApproximatelyEqual.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
epsilon,
);
},
},
naturalOrder: {
value(left, right) {
return rawNaturalOrder.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
);
},
},
stableBytes: {
value(value) {
return rawStableBytes.call(this, requireSemiringWeight(value, runtimeIdentity, this));
},
},
diagnostic: {
value(value = null) {
return value === null
? rawDiagnostic.call(this)
: rawDiagnosticWeight.call(
this, requireSemiringWeight(value, runtimeIdentity, this),
);
},
},
plusMany: {
value(values) {
return attach(rawPlusMany.call(
this, semiringHandles(values, runtimeIdentity, this, DEFAULT_BATCH_SIZE),
), this);
},
},
timesMany: {
value(values) {
return attach(rawTimesMany.call(
this, semiringHandles(values, runtimeIdentity, this, DEFAULT_BATCH_SIZE),
), this);
},
},
divide: {
value(left, right) {
return attach(rawDivide.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
), this);
},
},
leftDivide: {
value(left, right) {
return attach(rawLeftDivide.call(
this,
requireSemiringWeight(left, runtimeIdentity, this),
requireSemiringWeight(right, runtimeIdentity, this),
), this);
},
},
star: {
value(value) {
return attach(rawStar.call(
this, requireSemiringWeight(value, runtimeIdentity, this),
), this);
},
},
numericalValue: {
value(value) {
return rawNumericalValue.call(
this, requireSemiringWeight(value, runtimeIdentity, this),
);
},
},
quantize: {
value(value, epsilon) {
return rawQuantize.call(
this, requireSemiringWeight(value, runtimeIdentity, this), epsilon,
);
},
},
toProbability: {
value(value) {
return rawToProbability.call(
this, requireSemiringWeight(value, runtimeIdentity, this),
);
},
},
validateLaws: {
value(values, epsilon = 0) {
rawValidateLaws.call(
this, semiringHandles(values, runtimeIdentity, this, 16), epsilon,
);
},
},
[Symbol.dispose]: { value() { this.close(); } },
});
}
const libdictenstein = Object.freeze({
runtimeIdentity,
dynamicDawg(unitDomain = "unicode") {
return defineResourceMetadata(raw.Dictionary.dynamicDawg(unitDomain), runtimeIdentity, unitDomain);
},
doubleArrayTrie(entries, unitDomain = "unicode") {
return defineResourceMetadata(
raw.Dictionary.doubleArrayTrie(entries, unitDomain),
runtimeIdentity,
unitDomain,
);
},
scdawg(unitDomain = "unicode") {
return defineResourceMetadata(raw.Dictionary.scdawg(unitDomain), runtimeIdentity, unitDomain);
},
});
const liblevenshtein = Object.freeze({
runtimeIdentity,
transducer(dictionary, algorithm = "standard") {
return new raw.Transducer(requireDictionary(dictionary, runtimeIdentity), algorithm);
},
queryCache(transducer, options = {}) {
if (!(transducer instanceof raw.Transducer)) {
throw new TypeError("query cache requires a transducer from this runtime");
}
if (options === null || typeof options !== "object") {
throw new TypeError("query cache options must be an object");
}
return new raw.QueryCache(
transducer,
cacheLimit(options.maximumEntries, 1024, "maximumEntries"),
cacheLimit(options.maximumWeight, 64 * 1024 * 1024, "maximumWeight"),
);
},
phoneticPattern(source) {
return raw.PhoneticPattern.compileRegex(source);
},
llrePattern(source) {
return raw.PhoneticPattern.compileLlre(source);
},
phoneticRules(source) {
return raw.PhoneticRuleSet.compile(source);
},
levenshteinDistance: raw.levenshteinDistance,
levenshteinDistanceThreshold: raw.levenshteinDistanceThreshold,
damerauDistance: raw.damerauDistance,
damerauDistanceThreshold: raw.damerauDistanceThreshold,
trueDamerauDistance: raw.trueDamerauDistance,
trueDamerauDistanceThreshold: raw.trueDamerauDistanceThreshold,
});
const llingLlang = Object.freeze({
runtimeIdentity,
vectorWfst() { return new raw.WfstBuilder(); },
lattice(provider, options) {
const [value, domainId] = hostLattice(provider, options);
return raw.createHostLattice(value, domainId);
},
semiring(provider, options) {
const selected = hostSemiring(provider, options);
const value = raw.createHostSemiring(
selected.provider,
selected.domainId,
selected.propertyBits,
selected.closureBound,
);
Object.defineProperty(value, "properties", {
value: selected.properties,
enumerable: true,
});
return value;
},
validateLatticeLaws(values) {
const domainId = values?.[0]?.domainId;
raw.validateLatticeLawHandles(
latticeHandles(values, runtimeIdentity, domainId, 16),
);
},
scalarWfst(provider, options = {}) {
const [unitDomain, weightDomain, lazy, acyclic] = hostWfstOptions(options);
return raw.createHostWfst(
requireScalarWfstProvider(provider),
unitDomain,
weightDomain,
lazy,
acyclic,
);
},
compose(first, second) {
return raw.composeWfst(
requireWfst(first, runtimeIdentity),
requireWfst(second, runtimeIdentity),
);
},
});
const duallity = Object.freeze({
runtimeIdentity,
wfst(
dictionary,
query,
maximumDistance,
algorithm = "standard",
kind = "levenshtein",
) {
return raw.createDuallityWfst(
requireDictionary(dictionary, runtimeIdentity),
query,
maximumDistance,
algorithm,
kind,
);
},
});
return Object.freeze({ runtimeIdentity, libdictenstein, liblevenshtein, llingLlang, duallity });
}