spec-cat 0.1.0 → 0.1.2

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.
Files changed (110) hide show
  1. package/.output/nitro.json +1 -1
  2. package/.output/public/_nuxt/{Bqlz6CoK.js → BE_75kPa.js} +1 -1
  3. package/.output/public/_nuxt/{B2wdmh_w.js → BJ7m4fRW.js} +53 -53
  4. package/.output/public/_nuxt/{KNuzSjk0.js → CCNYUZ9m.js} +1 -1
  5. package/.output/public/_nuxt/{BvosqTnx.js → DGtcdWVl.js} +1 -1
  6. package/.output/public/_nuxt/DxEx-kFx.js +1 -0
  7. package/.output/public/_nuxt/{BwcbSlWF.js → DyMq_cQC.js} +2 -2
  8. package/.output/public/_nuxt/{COTT6rNZ.js → _cj5lOdZ.js} +1 -1
  9. package/.output/public/_nuxt/builds/latest.json +1 -1
  10. package/.output/public/_nuxt/builds/meta/3a0aacc1-0bd1-4d15-8b8a-3cee48cbfc69.json +1 -0
  11. package/.output/public/_nuxt/{BUOk7wkI.js → gDut6QrP.js} +1 -1
  12. package/.output/public/_nuxt/{C5wk2twv.js → nJpWpjzg.js} +1 -1
  13. package/.output/public/_nuxt/{DBab5Zcv.js → waQ9fPC1.js} +1 -1
  14. package/.output/server/chunks/_/codexProvider.mjs +64 -18
  15. package/.output/server/chunks/_/codexProvider.mjs.map +1 -1
  16. package/.output/server/chunks/build/client.precomputed.mjs +1 -1
  17. package/.output/server/chunks/build/client.precomputed.mjs.map +1 -1
  18. package/.output/server/chunks/nitro/nitro.mjs +702 -703
  19. package/.output/server/chunks/routes/_ws.mjs +37 -7
  20. package/.output/server/chunks/routes/_ws.mjs.map +1 -1
  21. package/.output/server/node_modules/@huggingface/jinja/dist/index.js +1572 -0
  22. package/.output/server/node_modules/@huggingface/jinja/package.json +55 -0
  23. package/.output/server/node_modules/@xenova/transformers/package.json +84 -0
  24. package/.output/server/node_modules/@xenova/transformers/src/backends/onnx.js +50 -0
  25. package/.output/server/node_modules/@xenova/transformers/src/configs.js +107 -0
  26. package/.output/server/node_modules/@xenova/transformers/src/env.js +128 -0
  27. package/.output/server/node_modules/@xenova/transformers/src/models.js +6267 -0
  28. package/.output/server/node_modules/@xenova/transformers/src/pipelines.js +3287 -0
  29. package/.output/server/node_modules/@xenova/transformers/src/processors.js +2248 -0
  30. package/.output/server/node_modules/@xenova/transformers/src/tokenizers.js +4479 -0
  31. package/.output/server/node_modules/@xenova/transformers/src/transformers.js +24 -0
  32. package/.output/server/node_modules/@xenova/transformers/src/utils/audio.js +672 -0
  33. package/.output/server/node_modules/@xenova/transformers/src/utils/core.js +175 -0
  34. package/.output/server/node_modules/@xenova/transformers/src/utils/data-structures.js +415 -0
  35. package/.output/server/node_modules/@xenova/transformers/src/utils/generation.js +873 -0
  36. package/.output/server/node_modules/@xenova/transformers/src/utils/hub.js +658 -0
  37. package/.output/server/node_modules/@xenova/transformers/src/utils/image.js +731 -0
  38. package/.output/server/node_modules/@xenova/transformers/src/utils/maths.js +985 -0
  39. package/.output/server/node_modules/@xenova/transformers/src/utils/tensor.js +1239 -0
  40. package/.output/server/node_modules/color/index.js +496 -0
  41. package/.output/server/node_modules/color/package.json +47 -0
  42. package/.output/server/node_modules/color-convert/conversions.js +839 -0
  43. package/.output/server/node_modules/color-convert/index.js +81 -0
  44. package/.output/server/node_modules/color-convert/package.json +48 -0
  45. package/.output/server/node_modules/color-convert/route.js +97 -0
  46. package/.output/server/node_modules/color-name/index.js +152 -0
  47. package/.output/server/node_modules/color-name/package.json +28 -0
  48. package/.output/server/node_modules/color-string/index.js +242 -0
  49. package/.output/server/node_modules/color-string/package.json +39 -0
  50. package/.output/server/node_modules/detect-libc/lib/detect-libc.js +313 -0
  51. package/.output/server/node_modules/detect-libc/lib/elf.js +39 -0
  52. package/.output/server/node_modules/detect-libc/lib/filesystem.js +51 -0
  53. package/.output/server/node_modules/detect-libc/lib/process.js +24 -0
  54. package/.output/server/node_modules/detect-libc/package.json +44 -0
  55. package/.output/server/node_modules/is-arrayish/index.js +9 -0
  56. package/.output/server/node_modules/is-arrayish/package.json +45 -0
  57. package/.output/server/node_modules/onnxruntime-common/dist/ort-common.node.js +7 -0
  58. package/.output/server/node_modules/onnxruntime-common/package.json +31 -0
  59. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/darwin/arm64/onnxruntime_binding.node +0 -0
  60. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/darwin/x64/onnxruntime_binding.node +0 -0
  61. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/linux/arm64/libonnxruntime.so.1.14.0 +0 -0
  62. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/linux/arm64/onnxruntime_binding.node +0 -0
  63. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/linux/x64/libonnxruntime.so.1.14.0 +0 -0
  64. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/linux/x64/onnxruntime_binding.node +0 -0
  65. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/win32/arm64/onnxruntime_binding.node +0 -0
  66. package/.output/server/node_modules/onnxruntime-node/bin/napi-v3/win32/x64/onnxruntime_binding.node +0 -0
  67. package/.output/server/node_modules/onnxruntime-node/dist/backend.js +75 -0
  68. package/.output/server/node_modules/onnxruntime-node/dist/binding.js +10 -0
  69. package/.output/server/node_modules/onnxruntime-node/dist/index.js +23 -0
  70. package/.output/server/node_modules/onnxruntime-node/package.json +58 -0
  71. package/.output/server/node_modules/onnxruntime-web/dist/ort-web.node.js +7 -0
  72. package/.output/server/node_modules/onnxruntime-web/package.json +84 -0
  73. package/.output/server/node_modules/semver/classes/semver.js +333 -0
  74. package/.output/server/node_modules/semver/functions/coerce.js +62 -0
  75. package/.output/server/node_modules/semver/functions/compare.js +7 -0
  76. package/.output/server/node_modules/semver/functions/gte.js +5 -0
  77. package/.output/server/node_modules/semver/functions/parse.js +18 -0
  78. package/.output/server/node_modules/semver/internal/constants.js +37 -0
  79. package/.output/server/node_modules/semver/internal/debug.js +11 -0
  80. package/.output/server/node_modules/semver/internal/identifiers.js +29 -0
  81. package/.output/server/node_modules/semver/internal/parse-options.js +17 -0
  82. package/.output/server/node_modules/semver/internal/re.js +223 -0
  83. package/.output/server/node_modules/semver/package.json +78 -0
  84. package/.output/server/node_modules/sharp/build/Release/sharp-linux-x64.node +0 -0
  85. package/.output/server/node_modules/sharp/lib/channel.js +174 -0
  86. package/.output/server/node_modules/sharp/lib/colour.js +184 -0
  87. package/.output/server/node_modules/sharp/lib/composite.js +210 -0
  88. package/.output/server/node_modules/sharp/lib/constructor.js +439 -0
  89. package/.output/server/node_modules/sharp/lib/index.js +16 -0
  90. package/.output/server/node_modules/sharp/lib/input.js +631 -0
  91. package/.output/server/node_modules/sharp/lib/is.js +155 -0
  92. package/.output/server/node_modules/sharp/lib/libvips.js +140 -0
  93. package/.output/server/node_modules/sharp/lib/operation.js +919 -0
  94. package/.output/server/node_modules/sharp/lib/output.js +1413 -0
  95. package/.output/server/node_modules/sharp/lib/platform.js +30 -0
  96. package/.output/server/node_modules/sharp/lib/resize.js +582 -0
  97. package/.output/server/node_modules/sharp/lib/sharp.js +38 -0
  98. package/.output/server/node_modules/sharp/lib/utility.js +287 -0
  99. package/.output/server/node_modules/sharp/package.json +204 -0
  100. package/.output/server/node_modules/sharp/vendor/8.14.5/linux-x64/THIRD-PARTY-NOTICES.md +43 -0
  101. package/.output/server/node_modules/sharp/vendor/8.14.5/linux-x64/lib/libvips-cpp.so.42 +0 -0
  102. package/.output/server/node_modules/sharp/vendor/8.14.5/linux-x64/platform.json +1 -0
  103. package/.output/server/node_modules/sharp/vendor/8.14.5/linux-x64/versions.json +31 -0
  104. package/.output/server/node_modules/simple-swizzle/index.js +29 -0
  105. package/.output/server/node_modules/simple-swizzle/package.json +36 -0
  106. package/.output/server/package.json +15 -1
  107. package/README.md +2 -0
  108. package/package.json +12 -19
  109. package/.output/public/_nuxt/5FxpIoe_.js +0 -1
  110. package/.output/public/_nuxt/builds/meta/21578a05-1b7e-4847-a8ff-7480800ea4a6.json +0 -1
@@ -0,0 +1,175 @@
1
+
2
+ /**
3
+ * @file Core utility functions/classes for Transformers.js.
4
+ *
5
+ * These are only used internally, meaning an end-user shouldn't
6
+ * need to access anything here.
7
+ *
8
+ * @module utils/core
9
+ */
10
+
11
+ /**
12
+ * Helper function to dispatch progress callbacks.
13
+ *
14
+ * @param {Function} progress_callback The progress callback function to dispatch.
15
+ * @param {any} data The data to pass to the progress callback function.
16
+ * @returns {void}
17
+ * @private
18
+ */
19
+ export function dispatchCallback(progress_callback, data) {
20
+ if (progress_callback) progress_callback(data);
21
+ }
22
+
23
+ /**
24
+ * Reverses the keys and values of an object.
25
+ *
26
+ * @param {Object} data The object to reverse.
27
+ * @returns {Object} The reversed object.
28
+ * @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript
29
+ */
30
+ export function reverseDictionary(data) {
31
+ // https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript
32
+ return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key]));
33
+ }
34
+
35
+ /**
36
+ * Escapes regular expression special characters from a string by replacing them with their escaped counterparts.
37
+ *
38
+ * @param {string} string The string to escape.
39
+ * @returns {string} The escaped string.
40
+ */
41
+ export function escapeRegExp(string) {
42
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
43
+ }
44
+
45
+ /**
46
+ * A base class for creating callable objects.
47
+ *
48
+ * @type {new () => {(...args: any[]): any, _call(...args: any[]): any}}
49
+ */
50
+ export const Callable = /** @type {any} */ (class {
51
+ /**
52
+ * Creates a new instance of the Callable class.
53
+ */
54
+ constructor() {
55
+ /**
56
+ * Creates a closure that delegates to a private method '_call' with the given arguments.
57
+ * @type {any}
58
+ * @param {...any} args Zero or more arguments to pass to the '_call' method.
59
+ * @returns {*} The result of calling the '_call' method.
60
+ */
61
+ let closure = function (...args) {
62
+ return closure._call(...args)
63
+ }
64
+ return Object.setPrototypeOf(closure, new.target.prototype)
65
+ }
66
+
67
+ /**
68
+ * This method should be implemented in subclasses to provide the
69
+ * functionality of the callable object.
70
+ *
71
+ * @param {any[]} args
72
+ * @throws {Error} If the subclass does not implement the `_call` method.
73
+ */
74
+ _call(...args) {
75
+ throw Error('Must implement _call method in subclass')
76
+ }
77
+ });
78
+
79
+ /**
80
+ * Check if a value is a typed array.
81
+ * @param {*} val The value to check.
82
+ * @returns {boolean} True if the value is a `TypedArray`, false otherwise.
83
+ *
84
+ * Adapted from https://stackoverflow.com/a/71091338/13989043
85
+ */
86
+ export function isTypedArray(val) {
87
+ return val?.prototype?.__proto__?.constructor?.name === 'TypedArray';
88
+ }
89
+
90
+
91
+ /**
92
+ * Check if a value is an integer.
93
+ * @param {*} x The value to check.
94
+ * @returns {boolean} True if the value is a string, false otherwise.
95
+ */
96
+ export function isIntegralNumber(x) {
97
+ return Number.isInteger(x) || typeof x === 'bigint'
98
+ }
99
+
100
+ /**
101
+ * Check if a value is exists.
102
+ * @param {*} x The value to check.
103
+ * @returns {boolean} True if the value exists, false otherwise.
104
+ */
105
+ export function exists(x) {
106
+ return x !== undefined && x !== null;
107
+ }
108
+
109
+ /**
110
+ * Calculates the dimensions of a nested array.
111
+ *
112
+ * @param {any[]} arr The nested array to calculate dimensions for.
113
+ * @returns {number[]} An array containing the dimensions of the input array.
114
+ */
115
+ export function calculateDimensions(arr) {
116
+ const dimensions = [];
117
+ let current = arr;
118
+ while (Array.isArray(current)) {
119
+ dimensions.push(current.length);
120
+ current = current[0];
121
+ }
122
+ return dimensions;
123
+ }
124
+
125
+ /**
126
+ * Replicate python's .pop() method for objects.
127
+ * @param {Object} obj The object to pop from.
128
+ * @param {string} key The key to pop.
129
+ * @param {*} defaultValue The default value to return if the key does not exist.
130
+ * @returns {*} The value of the popped key.
131
+ * @throws {Error} If the key does not exist and no default value is provided.
132
+ */
133
+ export function pop(obj, key, defaultValue = undefined) {
134
+ const value = obj[key];
135
+ if (value !== undefined) {
136
+ delete obj[key];
137
+ return value;
138
+ }
139
+ if (defaultValue === undefined) {
140
+ throw Error(`Key ${key} does not exist in object.`)
141
+ }
142
+ return defaultValue;
143
+ }
144
+
145
+ /**
146
+ * Efficiently merge arrays, creating a new copy.
147
+ * Adapted from https://stackoverflow.com/a/6768642/13989043
148
+ * @param {Array[]} arrs Arrays to merge.
149
+ * @returns {Array} The merged array.
150
+ */
151
+ export function mergeArrays(...arrs) {
152
+ return Array.prototype.concat.apply([], arrs);
153
+ }
154
+
155
+ /**
156
+ * Compute the Cartesian product of given arrays
157
+ * @param {...Array} a Arrays to compute the product
158
+ * @returns {Array} Returns the computed Cartesian product as an array
159
+ * @private
160
+ */
161
+ export function product(...a) {
162
+ // Cartesian product of items
163
+ // Adapted from https://stackoverflow.com/a/43053803
164
+ return a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e])));
165
+ }
166
+
167
+ /**
168
+ * Calculates the index offset for a given index and window size.
169
+ * @param {number} i The index.
170
+ * @param {number} w The window size.
171
+ * @returns {number} The index offset.
172
+ */
173
+ export function calculateReflectOffset(i, w) {
174
+ return Math.abs((i + w) % (2 * w) - w);
175
+ }
@@ -0,0 +1,415 @@
1
+
2
+ /**
3
+ * @file Custom data structures.
4
+ *
5
+ * These are only used internally, meaning an end-user shouldn't
6
+ * need to access anything here.
7
+ *
8
+ * @module utils/data-structures
9
+ */
10
+
11
+
12
+ /**
13
+ * Efficient Heap-based Implementation of a Priority Queue.
14
+ * It uses an array-based binary heap, where the root is at index `0`, and the
15
+ * children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively.
16
+ *
17
+ * Adapted from the following sources:
18
+ * - https://stackoverflow.com/a/42919752/13989043 (original)
19
+ * - https://github.com/belladoreai/llama-tokenizer-js (minor improvements)
20
+ */
21
+ export class PriorityQueue {
22
+
23
+ /**
24
+ * Create a new PriorityQueue.
25
+ * @param {Function} comparator Comparator function to determine priority. Defaults to a MaxHeap.
26
+ */
27
+ constructor(comparator = (a, b) => a > b) {
28
+ this._heap = [];
29
+ this._comparator = comparator;
30
+ }
31
+
32
+ /**
33
+ * The size of the queue
34
+ */
35
+ get size() {
36
+ return this._heap.length;
37
+ }
38
+
39
+ /**
40
+ * Check if the queue is empty.
41
+ * @returns {boolean} `true` if the queue is empty, `false` otherwise.
42
+ */
43
+ isEmpty() {
44
+ return this.size === 0;
45
+ }
46
+
47
+ /**
48
+ * Return the element with the highest priority in the queue.
49
+ * @returns {any} The highest priority element in the queue.
50
+ */
51
+ peek() {
52
+ return this._heap[0];
53
+ }
54
+
55
+ /**
56
+ * Add one or more elements to the queue.
57
+ * @param {...any} values The values to push into the queue.
58
+ * @returns {number} The new size of the queue.
59
+ */
60
+ push(...values) {
61
+ return this.extend(values);
62
+ }
63
+
64
+ /**
65
+ * Add multiple elements to the queue.
66
+ * @param {any[]} values The values to push into the queue.
67
+ * @returns {number} The new size of the queue.
68
+ */
69
+ extend(values) {
70
+ for (const value of values) {
71
+ this._heap.push(value);
72
+ this._siftUp();
73
+ }
74
+ return this.size;
75
+ }
76
+
77
+ /**
78
+ * Remove and return the element with the highest priority in the queue.
79
+ * @returns {any} The element with the highest priority in the queue.
80
+ */
81
+ pop() {
82
+ const poppedValue = this.peek();
83
+ const bottom = this.size - 1;
84
+ if (bottom > 0) {
85
+ this._swap(0, bottom);
86
+ }
87
+ this._heap.pop();
88
+ this._siftDown();
89
+ return poppedValue;
90
+ }
91
+
92
+ /**
93
+ * Replace the element with the highest priority in the queue with a new value.
94
+ * @param {*} value The new value.
95
+ * @returns {*} The replaced value.
96
+ */
97
+ replace(value) {
98
+ const replacedValue = this.peek();
99
+ this._heap[0] = value;
100
+ this._siftDown();
101
+ return replacedValue;
102
+ }
103
+
104
+ /**
105
+ * Compute the index for the parent of the node at index `i`.
106
+ * @param {number} i The index of the node to get the parent of.
107
+ * @returns {number} The index of the parent node.
108
+ * @private
109
+ */
110
+ _parent(i) {
111
+ return ((i + 1) >>> 1) - 1;
112
+ }
113
+
114
+ /**
115
+ * Compute the index for the left child of the node at index `i`.
116
+ * @param {number} i The index of the node to get the left child of.
117
+ * @returns {number} The index of the left child.
118
+ * @private
119
+ */
120
+ _left(i) {
121
+ return (i << 1) + 1;
122
+ }
123
+
124
+ /**
125
+ * Compute the index for the right child of the node at index `i`.
126
+ * @param {number} i The index of the node to get the right child of.
127
+ * @returns {number} The index of the right child.
128
+ * @private
129
+ */
130
+ _right(i) {
131
+ return (i + 1) << 1;
132
+ }
133
+
134
+ /**
135
+ * Check if the element at index `i` is greater than the element at index `j`.
136
+ * @param {number} i The index of the first element to compare.
137
+ * @param {number} j The index of the second element to compare.
138
+ * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise.
139
+ * @private
140
+ */
141
+ _greater(i, j) {
142
+ return this._comparator(this._heap[i], this._heap[j]);
143
+ }
144
+
145
+ /**
146
+ * Swap the elements at indices `i` and `j`.
147
+ * @param {number} i The index of the first element to swap.
148
+ * @param {number} j The index of the second element to swap.
149
+ * @private
150
+ */
151
+ _swap(i, j) {
152
+ const temp = this._heap[i];
153
+ this._heap[i] = this._heap[j];
154
+ this._heap[j] = temp;
155
+ }
156
+
157
+ /**
158
+ * Maintain the heap property by updating positions in the heap,
159
+ * starting at the last element and moving up the heap.
160
+ * @private
161
+ */
162
+ _siftUp() {
163
+ let node = this.size - 1;
164
+ while (node > 0 && this._greater(node, this._parent(node))) {
165
+ this._swap(node, this._parent(node));
166
+ node = this._parent(node);
167
+ }
168
+ }
169
+ /**
170
+ * Maintain the heap property by updating positions in the heap,
171
+ * starting at the first element and moving down the heap.
172
+ * @private
173
+ */
174
+ _siftDown() {
175
+ let node = 0;
176
+ while (
177
+ (this._left(node) < this.size && this._greater(this._left(node), node)) ||
178
+ (this._right(node) < this.size && this._greater(this._right(node), node))
179
+ ) {
180
+ const maxChild = (this._right(node) < this.size && this._greater(this._right(node), this._left(node)))
181
+ ? this._right(node)
182
+ : this._left(node);
183
+ this._swap(node, maxChild);
184
+ node = maxChild;
185
+ }
186
+ }
187
+ }
188
+
189
+ /**
190
+ * A trie structure to efficiently store and search for strings.
191
+ */
192
+ export class CharTrie {
193
+ constructor() {
194
+ this.root = CharTrieNode.default();
195
+ }
196
+
197
+ /**
198
+ * Adds one or more `texts` to the trie.
199
+ * @param {string[]} texts The strings to add to the trie.
200
+ */
201
+ extend(texts) {
202
+ for (let text of texts) {
203
+ this.push(text);
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Adds text to the trie.
209
+ * @param {string} text The string to add to the trie.
210
+ */
211
+ push(text) {
212
+ let node = this.root;
213
+ for (let ch of text) {
214
+ let child = node.children.get(ch);
215
+ if (child === undefined) {
216
+ child = CharTrieNode.default();
217
+ node.children.set(ch, child);
218
+ }
219
+ node = child;
220
+ }
221
+ node.isLeaf = true;
222
+ }
223
+
224
+ /**
225
+ * Searches the trie for all strings with a common prefix of `text`.
226
+ * @param {string} text The common prefix to search for.
227
+ * @yields {string} Each string in the trie that has `text` as a prefix.
228
+ */
229
+ *commonPrefixSearch(text) {
230
+ let node = this.root;
231
+ let prefix = "";
232
+ for (let i = 0; i < text.length && node !== undefined; ++i) {
233
+ const ch = text[i];
234
+ prefix += ch;
235
+ node = node.children.get(ch);
236
+ if (node !== undefined && node.isLeaf) {
237
+ yield prefix;
238
+ }
239
+ }
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Represents a node in a character trie.
245
+ */
246
+ class CharTrieNode {
247
+ /**
248
+ * Create a new CharTrieNode.
249
+ * @param {boolean} isLeaf Whether the node is a leaf node or not.
250
+ * @param {Map<string, CharTrieNode>} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`.
251
+ */
252
+ constructor(isLeaf, children) {
253
+ this.isLeaf = isLeaf;
254
+ this.children = children;
255
+ }
256
+
257
+ /**
258
+ * Returns a new `CharTrieNode` instance with default values.
259
+ * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map.
260
+ */
261
+ static default() {
262
+ return new CharTrieNode(false, new Map());
263
+ }
264
+ }
265
+
266
+ /**
267
+ * A lattice data structure to be used for tokenization.
268
+ */
269
+ export class TokenLattice {
270
+ /**
271
+ * Creates a new TokenLattice instance.
272
+ *
273
+ * @param {string} sentence The input sentence to be tokenized.
274
+ * @param {number} bosTokenId The beginning-of-sequence token ID.
275
+ * @param {number} eosTokenId The end-of-sequence token ID.
276
+ */
277
+ constructor(sentence, bosTokenId, eosTokenId) {
278
+ this.sentence = sentence;
279
+ this.len = sentence.length;
280
+ this.bosTokenId = bosTokenId;
281
+ this.eosTokenId = eosTokenId;
282
+ this.nodes = [];
283
+ this.beginNodes = Array.from({ length: this.len + 1 }, () => []);
284
+ this.endNodes = Array.from({ length: this.len + 1 }, () => []);
285
+
286
+ const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0);
287
+ const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0);
288
+ this.nodes.push(bos.clone());
289
+ this.nodes.push(eos.clone());
290
+ this.beginNodes[this.len].push(eos);
291
+ this.endNodes[0].push(bos);
292
+ }
293
+
294
+ /**
295
+ * Inserts a new token node into the token lattice.
296
+ *
297
+ * @param {number} pos The starting position of the token.
298
+ * @param {number} length The length of the token.
299
+ * @param {number} score The score of the token.
300
+ * @param {number} tokenId The token ID of the token.
301
+ */
302
+ insert(pos, length, score, tokenId) {
303
+ const nodeId = this.nodes.length;
304
+ const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score);
305
+ this.beginNodes[pos].push(node);
306
+ this.endNodes[pos + length].push(node);
307
+ this.nodes.push(node);
308
+ }
309
+
310
+ /**
311
+ * Implements the Viterbi algorithm to compute the most likely sequence of tokens.
312
+ *
313
+ * @returns {TokenLatticeNode[]} The array of nodes representing the most likely sequence of tokens.
314
+ */
315
+ viterbi() {
316
+ const len = this.len;
317
+ let pos = 0;
318
+ while (pos <= len) {
319
+ if (this.beginNodes[pos].length == 0) {
320
+ return [];
321
+ }
322
+ for (let rnode of this.beginNodes[pos]) {
323
+ rnode.prev = null;
324
+ let bestScore = 0.0;
325
+ let bestNode = null;
326
+ for (let lnode of this.endNodes[pos]) {
327
+ const score = lnode.backtraceScore + rnode.score;
328
+ if (bestNode === null || score > bestScore) {
329
+ bestNode = lnode.clone();
330
+ bestScore = score;
331
+ }
332
+ }
333
+
334
+ if (bestNode !== null) {
335
+ rnode.prev = bestNode;
336
+ rnode.backtraceScore = bestScore;
337
+ } else {
338
+ return [];
339
+ }
340
+ }
341
+ ++pos;
342
+ }
343
+
344
+ const results = [];
345
+ const root = this.beginNodes[len][0];
346
+ const prev = root.prev;
347
+ if (prev === null) {
348
+ return [];
349
+ }
350
+
351
+ let node = prev.clone();
352
+ while (node.prev !== null) {
353
+ results.push(node.clone());
354
+ const n = node.clone();
355
+ node = n.prev.clone();
356
+ }
357
+
358
+ results.reverse();
359
+ return results;
360
+ }
361
+
362
+ /**
363
+ * @param {TokenLatticeNode} node
364
+ * @returns {string} The array of nodes representing the most likely sequence of tokens.
365
+ */
366
+ piece(node) {
367
+ return this.sentence.slice(node.pos, node.pos + node.length);
368
+ }
369
+
370
+ /**
371
+ * @returns {Array} The array of nodes representing the most likely sequence of tokens.
372
+ */
373
+ tokens() {
374
+ const nodes = this.viterbi();
375
+ return nodes.map(x => this.piece(x));
376
+ }
377
+
378
+ /**
379
+ * @returns {Array} The array of nodes representing the most likely sequence of tokens.
380
+ */
381
+ tokenIds() {
382
+ const nodes = this.viterbi();
383
+ return nodes.map(x => x.tokenId);
384
+ }
385
+ }
386
+ class TokenLatticeNode {
387
+ /**
388
+ * Represents a node in a token lattice for a given sentence.
389
+ * @param {number} tokenId The ID of the token associated with this node.
390
+ * @param {number} nodeId The ID of this node.
391
+ * @param {number} pos The starting position of the token in the sentence.
392
+ * @param {number} length The length of the token.
393
+ * @param {number} score The score associated with the token.
394
+ */
395
+ constructor(tokenId, nodeId, pos, length, score) {
396
+ this.tokenId = tokenId;
397
+ this.nodeId = nodeId;
398
+ this.pos = pos;
399
+ this.length = length;
400
+ this.score = score;
401
+ this.prev = null;
402
+ this.backtraceScore = 0.0;
403
+ }
404
+
405
+ /**
406
+ * Returns a clone of this node.
407
+ * @returns {TokenLatticeNode} A clone of this node.
408
+ */
409
+ clone() {
410
+ const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score);
411
+ n.prev = this.prev;
412
+ n.backtraceScore = this.backtraceScore;
413
+ return n;
414
+ }
415
+ }