|
1 |
| -"use strict"; |
2 |
| -Object.defineProperty(exports, "__esModule", { value: true }); |
3 |
| -const SuggestedItem_1 = require("./SuggestedItem"); |
4 |
| -var Verbosity; |
5 |
| -(function (Verbosity) { |
6 |
| - // Top suggestion with the highest term frequency of the suggestions of smallest edit distance found. |
7 |
| - Verbosity[Verbosity["Top"] = 0] = "Top"; |
8 |
| - // All suggestions of smallest edit distance found, suggestions ordered by term frequency. |
9 |
| - Verbosity[Verbosity["Closest"] = 1] = "Closest"; |
10 |
| - //All suggestions within maxEditDistance, suggestions ordered by edit distance |
11 |
| - // , then by term frequency (slower, no early termination).</summary> |
12 |
| - Verbosity[Verbosity["All"] = 2] = "All"; |
13 |
| -})(Verbosity = exports.Verbosity || (exports.Verbosity = {})); |
14 |
| -exports.defaultOptions = { |
15 |
| - dictionaryEditDistance: 2, |
16 |
| - countThreshold: 1 |
17 |
| -}; |
18 |
| -exports.defaultCheckSpellingOptions = { |
19 |
| - includeUnknown: false, |
20 |
| - includeSelf: false, |
21 |
| - maxEditDistance: 2, |
22 |
| - verbosity: Verbosity.Closest |
23 |
| -}; |
24 |
| -/** |
25 |
| - * This class provides the wrapper for the spellcheck-wasm.wasm functionality. |
26 |
| - */ |
27 |
| -class SpellcheckerBase { |
28 |
| - constructor(resultHandler) { |
29 |
| - /** |
30 |
| - * @internal |
31 |
| - * |
32 |
| - * Traps responses form the wasm and converts them into |
33 |
| - * their respective SuggestedItem instances. |
34 |
| - * |
35 |
| - * @param ptr number The pointer in memory where this result set is located |
36 |
| - * @param length number The number of total bytes in the set |
37 |
| - */ |
38 |
| - this.resultTrap = (ptr, length) => { |
39 |
| - const { memory } = this.wasmSymSpell; |
40 |
| - this.resultHandler(SuggestedItem_1.deserializeSuggestedItems(memory.buffer, ptr, length)); |
41 |
| - }; |
42 |
| - this.resultHandler = resultHandler; |
43 |
| - } |
44 |
| - /** |
45 |
| - * Writes a chunk of bytes to the dictionary. This operation is |
46 |
| - * useful when implementing a custom dictionary where additional |
47 |
| - * entries are required beyond the supplied corpus. |
48 |
| - * |
49 |
| - * Caution should be used since writing multiple megabytes at once |
50 |
| - * often results in a memory out of bounds error. Streaming at 32-64kb |
51 |
| - * chunks is recommended. |
52 |
| - * |
53 |
| - * @param chunk Uint8Array The chunk containing the bytes to write |
54 |
| - * @param isBigram boolean Indicates whether this chunk should be written to the bigram dictionary instead. |
55 |
| - */ |
56 |
| - writeToDictionary(chunk, isBigram = false) { |
57 |
| - this.writeToBuffer(chunk, this.wasmSymSpell.memory); |
58 |
| - this.wasmSymSpell.write_to_dictionary(0, chunk.byteLength, isBigram); |
59 |
| - } |
60 |
| - /** |
61 |
| - * Performs a single spelling check based on the supplied word and options. |
62 |
| - * The suggestions list will be provided to the resultHandler(). |
63 |
| - * |
64 |
| - * @param word string The word to perform spell checking on. |
65 |
| - * @param options CheckSpellingOptions The options to use for this spell check lookup |
66 |
| - */ |
67 |
| - checkSpelling(word, options = exports.defaultCheckSpellingOptions) { |
68 |
| - const { lookup, memory } = this.wasmSymSpell; |
69 |
| - let encodedString; |
70 |
| - if (word instanceof Uint8Array) { |
71 |
| - encodedString = word; |
72 |
| - } |
73 |
| - else { |
74 |
| - encodedString = this.encodeString(word); |
75 |
| - } |
76 |
| - this.writeToBuffer(encodedString, memory); |
77 |
| - lookup(0, encodedString.byteLength, options.verbosity, options.maxEditDistance, options.includeUnknown, options.includeSelf); |
78 |
| - } |
79 |
| - /** |
80 |
| - * Performs a spelling check based on the supplied sentence and options. |
81 |
| - * The suggestions list will be provided to the resultHandler(). |
82 |
| - * |
83 |
| - * @param sentence string The sentence to perform spell checking on. |
84 |
| - * @param options CheckSpellingOptions The options to use for this spell check lookup |
85 |
| - */ |
86 |
| - checkSpellingCompound(sentence, options = exports.defaultCheckSpellingOptions) { |
87 |
| - const { lookup_compound, memory } = this.wasmSymSpell; |
88 |
| - let encodedString; |
89 |
| - if (sentence instanceof Uint8Array) { |
90 |
| - encodedString = sentence; |
91 |
| - } |
92 |
| - else { |
93 |
| - encodedString = this.encodeString(sentence); |
94 |
| - } |
95 |
| - this.writeToBuffer(encodedString, memory); |
96 |
| - lookup_compound(0, encodedString.byteLength, options.maxEditDistance); |
97 |
| - } |
98 |
| - /** |
99 |
| - * Allocations within the WASM process |
100 |
| - * detach reference to the memory buffer. |
101 |
| - * We check for this and create a new Uint8Array |
102 |
| - * with the new memory buffer reference if needed. |
103 |
| - * |
104 |
| - * @param chunk |
105 |
| - * @param memory |
106 |
| - */ |
107 |
| - writeToBuffer(chunk, memory) { |
108 |
| - if (!this.writeBuffer || this.writeBuffer.buffer !== memory.buffer || this.writeBuffer.byteLength < chunk.byteLength) { |
109 |
| - if (memory.buffer.byteLength < chunk.byteLength) { |
110 |
| - const delta = Math.ceil((chunk.byteLength - memory.buffer.byteLength) / 65536); // 64 * 1024; |
111 |
| - memory.grow(delta); |
112 |
| - } |
113 |
| - this.writeBuffer = new Uint8Array(memory.buffer, 0, chunk.byteLength); |
114 |
| - } |
115 |
| - this.writeBuffer.set(chunk, 0); |
116 |
| - } |
117 |
| -} |
118 |
| -exports.SpellcheckerBase = SpellcheckerBase; |
0 commit comments