tokana 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 +164 -0
- package/dist/cli.js +39 -0
- package/dist/cli.js.map +1 -0
- package/dist/compile-VQJF62SJ.js +741 -0
- package/dist/compile-VQJF62SJ.js.map +1 -0
- package/dist/index.cjs +1478 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +566 -0
- package/dist/index.d.ts +566 -0
- package/dist/index.js +1432 -0
- package/dist/index.js.map +1 -0
- package/dist/info-OVE32SWZ.js +54 -0
- package/dist/info-OVE32SWZ.js.map +1 -0
- package/package.json +63 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token type definitions for different dictionary formats.
|
|
3
|
+
*/
|
|
4
|
+
/** Base token properties common to all formats */
|
|
5
|
+
interface BaseToken {
|
|
6
|
+
/** Word ID in the dictionary */
|
|
7
|
+
wordId: number;
|
|
8
|
+
/** Token type: "KNOWN", "UNKNOWN", "BOS", "EOS" */
|
|
9
|
+
type: TokenType;
|
|
10
|
+
/** Surface form (the actual text) */
|
|
11
|
+
surface: string;
|
|
12
|
+
/** Start position in the input string */
|
|
13
|
+
offset: number;
|
|
14
|
+
/** Length of the surface form */
|
|
15
|
+
length: number;
|
|
16
|
+
/** Cumulative cost from Viterbi */
|
|
17
|
+
cost: number;
|
|
18
|
+
}
|
|
19
|
+
type TokenType = "KNOWN" | "UNKNOWN" | "BOS" | "EOS";
|
|
20
|
+
/** IPAdic token with detailed grammatical information */
|
|
21
|
+
interface IpadicToken extends BaseToken {
|
|
22
|
+
/** Part-of-speech (品詞) */
|
|
23
|
+
pos: string;
|
|
24
|
+
/** POS detail 1 (品詞細分類1) */
|
|
25
|
+
posDetail1: string;
|
|
26
|
+
/** POS detail 2 (品詞細分類2) */
|
|
27
|
+
posDetail2: string;
|
|
28
|
+
/** POS detail 3 (品詞細分類3) */
|
|
29
|
+
posDetail3: string;
|
|
30
|
+
/** Conjugation type (活用型) */
|
|
31
|
+
conjugationType: string;
|
|
32
|
+
/** Conjugation form (活用形) */
|
|
33
|
+
conjugationForm: string;
|
|
34
|
+
/** Base form (原形) */
|
|
35
|
+
baseForm: string;
|
|
36
|
+
/** Reading (読み) */
|
|
37
|
+
reading: string;
|
|
38
|
+
/** Pronunciation (発音) */
|
|
39
|
+
pronunciation: string;
|
|
40
|
+
}
|
|
41
|
+
/** UniDic token with more detailed linguistic annotations */
|
|
42
|
+
interface UnidicToken extends BaseToken {
|
|
43
|
+
/** Part-of-speech 1 */
|
|
44
|
+
pos1: string;
|
|
45
|
+
/** Part-of-speech 2 */
|
|
46
|
+
pos2: string;
|
|
47
|
+
/** Part-of-speech 3 */
|
|
48
|
+
pos3: string;
|
|
49
|
+
/** Part-of-speech 4 */
|
|
50
|
+
pos4: string;
|
|
51
|
+
/** Conjugation type (活用型) */
|
|
52
|
+
cType: string;
|
|
53
|
+
/** Conjugation form (活用形) */
|
|
54
|
+
cForm: string;
|
|
55
|
+
/** Lemma reading form (語彙素読み) */
|
|
56
|
+
lForm: string;
|
|
57
|
+
/** Lemma (語彙素) */
|
|
58
|
+
lemma: string;
|
|
59
|
+
/** Orthographic form (書字形) */
|
|
60
|
+
orth: string;
|
|
61
|
+
/** Orthographic base form (書字形出現形) */
|
|
62
|
+
orthBase: string;
|
|
63
|
+
/** Pronunciation (発音形) */
|
|
64
|
+
pron: string;
|
|
65
|
+
/** Pronunciation base form */
|
|
66
|
+
pronBase: string;
|
|
67
|
+
/** Word origin (語種) */
|
|
68
|
+
goshu: string;
|
|
69
|
+
/** Accent type */
|
|
70
|
+
iType: string;
|
|
71
|
+
/** Accent form */
|
|
72
|
+
iForm: string;
|
|
73
|
+
/** Initial sound change type */
|
|
74
|
+
fType: string;
|
|
75
|
+
/** Initial sound change form */
|
|
76
|
+
fForm: string;
|
|
77
|
+
}
|
|
78
|
+
/** Discriminated union of all token types */
|
|
79
|
+
type Token = IpadicToken | UnidicToken;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Double Array Trie for efficient prefix matching.
|
|
83
|
+
* Supports exact lookup and common prefix search.
|
|
84
|
+
*/
|
|
85
|
+
interface KeyValue {
|
|
86
|
+
/** The value associated with the key */
|
|
87
|
+
value: number;
|
|
88
|
+
/** Length of the matched key (in code units) */
|
|
89
|
+
length: number;
|
|
90
|
+
}
|
|
91
|
+
declare class DoubleArray {
|
|
92
|
+
private base;
|
|
93
|
+
private check;
|
|
94
|
+
constructor(base?: Int32Array, check?: Int32Array);
|
|
95
|
+
/**
|
|
96
|
+
* Build a DoubleArray from sorted string keys.
|
|
97
|
+
* Keys must be sorted lexicographically.
|
|
98
|
+
*/
|
|
99
|
+
static build(keys: {
|
|
100
|
+
key: string;
|
|
101
|
+
value: number;
|
|
102
|
+
}[]): DoubleArray;
|
|
103
|
+
/**
|
|
104
|
+
* Exact lookup of a key string.
|
|
105
|
+
* Returns the associated value, or -1 if not found.
|
|
106
|
+
*/
|
|
107
|
+
lookup(key: string): number;
|
|
108
|
+
/**
|
|
109
|
+
* Find all keys that are prefixes of the given string.
|
|
110
|
+
* Returns values and their matched lengths.
|
|
111
|
+
*/
|
|
112
|
+
commonPrefixSearch(key: string): KeyValue[];
|
|
113
|
+
/**
|
|
114
|
+
* Check if any key exists that starts with the given prefix.
|
|
115
|
+
*/
|
|
116
|
+
contain(key: string): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Get the raw base array (for serialization).
|
|
119
|
+
*/
|
|
120
|
+
getBase(): Int32Array;
|
|
121
|
+
/**
|
|
122
|
+
* Get the raw check array (for serialization).
|
|
123
|
+
*/
|
|
124
|
+
getCheck(): Int32Array;
|
|
125
|
+
/**
|
|
126
|
+
* Create a DoubleArray from raw base and check arrays (for deserialization).
|
|
127
|
+
*/
|
|
128
|
+
static fromArrays(base: Int32Array, check: Int32Array): DoubleArray;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Connection cost matrix.
|
|
133
|
+
* Stores the cost of transitioning from one morpheme to the next.
|
|
134
|
+
* Matrix is indexed by [right_id][left_id].
|
|
135
|
+
*/
|
|
136
|
+
declare class ConnectionCosts {
|
|
137
|
+
private readonly forwardSize;
|
|
138
|
+
private readonly backwardSize;
|
|
139
|
+
private readonly costs;
|
|
140
|
+
constructor(forwardSize: number, backwardSize: number);
|
|
141
|
+
put(forwardId: number, backwardId: number, cost: number): void;
|
|
142
|
+
get(forwardId: number, backwardId: number): number;
|
|
143
|
+
getForwardSize(): number;
|
|
144
|
+
getBackwardSize(): number;
|
|
145
|
+
/**
|
|
146
|
+
* Load from a raw Int16Array buffer.
|
|
147
|
+
* Format: [forwardSize (as int16), backwardSize (as int16), ...costs]
|
|
148
|
+
*/
|
|
149
|
+
static fromBuffer(buffer: Int16Array): ConnectionCosts;
|
|
150
|
+
/**
|
|
151
|
+
* Serialize to Int16Array buffer.
|
|
152
|
+
*/
|
|
153
|
+
toBuffer(): Int16Array;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Growable byte buffer for binary data manipulation.
|
|
158
|
+
* Used for reading/writing dictionary binary data.
|
|
159
|
+
*/
|
|
160
|
+
declare class ByteBuffer {
|
|
161
|
+
private buffer;
|
|
162
|
+
private view;
|
|
163
|
+
private position;
|
|
164
|
+
constructor(arg?: number | Uint8Array);
|
|
165
|
+
private ensureCapacity;
|
|
166
|
+
size(): number;
|
|
167
|
+
getPosition(): number;
|
|
168
|
+
setPosition(pos: number): void;
|
|
169
|
+
getInt8(pos: number): number;
|
|
170
|
+
getInt16(pos: number): number;
|
|
171
|
+
getInt32(pos: number): number;
|
|
172
|
+
getUint8(pos: number): number;
|
|
173
|
+
getUint16(pos: number): number;
|
|
174
|
+
getUint32(pos: number): number;
|
|
175
|
+
putInt8(value: number): void;
|
|
176
|
+
putInt16(value: number): void;
|
|
177
|
+
putInt32(value: number): void;
|
|
178
|
+
putUint8(value: number): void;
|
|
179
|
+
putUint16(value: number): void;
|
|
180
|
+
putUint32(value: number): void;
|
|
181
|
+
readInt8(): number;
|
|
182
|
+
readInt16(): number;
|
|
183
|
+
readInt32(): number;
|
|
184
|
+
readUint8(): number;
|
|
185
|
+
readUint16(): number;
|
|
186
|
+
readUint32(): number;
|
|
187
|
+
readString(length: number): string;
|
|
188
|
+
putString(str: string): void;
|
|
189
|
+
shrink(): Uint8Array;
|
|
190
|
+
toUint8Array(): Uint8Array;
|
|
191
|
+
getArrayBuffer(): ArrayBuffer;
|
|
192
|
+
getDataView(): DataView;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Token information dictionary.
|
|
197
|
+
* Stores morpheme features (POS, reading, pronunciation, etc.)
|
|
198
|
+
* indexed by word ID.
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
declare class TokenInfoDictionary {
|
|
202
|
+
/** Feature data for each word, stored as concatenated strings */
|
|
203
|
+
private dictionary;
|
|
204
|
+
/** Mapping from word ID to position in features buffer */
|
|
205
|
+
private targetMap;
|
|
206
|
+
constructor();
|
|
207
|
+
buildDictionary(entries: {
|
|
208
|
+
wordId: number;
|
|
209
|
+
leftId: number;
|
|
210
|
+
rightId: number;
|
|
211
|
+
cost: number;
|
|
212
|
+
}[]): {
|
|
213
|
+
wordId: number;
|
|
214
|
+
leftId: number;
|
|
215
|
+
rightId: number;
|
|
216
|
+
cost: number;
|
|
217
|
+
}[];
|
|
218
|
+
/**
|
|
219
|
+
* Add a token entry to the dictionary.
|
|
220
|
+
*/
|
|
221
|
+
put(wordId: number, leftId: number, rightId: number, cost: number, features: string): void;
|
|
222
|
+
/**
|
|
223
|
+
* Add a mapping from word ID to buffer position.
|
|
224
|
+
*/
|
|
225
|
+
addMapping(wordId: number, position: number): void;
|
|
226
|
+
/**
|
|
227
|
+
* Get left context ID for a word.
|
|
228
|
+
*/
|
|
229
|
+
getLeftId(wordId: number): number;
|
|
230
|
+
/**
|
|
231
|
+
* Get right context ID for a word.
|
|
232
|
+
*/
|
|
233
|
+
getRightId(wordId: number): number;
|
|
234
|
+
/**
|
|
235
|
+
* Get word cost for a word.
|
|
236
|
+
*/
|
|
237
|
+
getWordCost(wordId: number): number;
|
|
238
|
+
/**
|
|
239
|
+
* Get feature string for a word.
|
|
240
|
+
*/
|
|
241
|
+
getFeatures(wordId: number): string;
|
|
242
|
+
/**
|
|
243
|
+
* Load the dictionary data buffer.
|
|
244
|
+
*/
|
|
245
|
+
loadDictionary(data: Uint8Array): this;
|
|
246
|
+
/**
|
|
247
|
+
* Load the position data buffer.
|
|
248
|
+
*/
|
|
249
|
+
loadPosVector(_data: Uint8Array): this;
|
|
250
|
+
/**
|
|
251
|
+
* Load the target map buffer.
|
|
252
|
+
* Format: sequence of int32 pairs [wordId, position]
|
|
253
|
+
*/
|
|
254
|
+
loadTargetMap(data: Uint8Array): this;
|
|
255
|
+
getDictionary(): ByteBuffer;
|
|
256
|
+
getTargetMap(): Record<number, number>;
|
|
257
|
+
getTargetMapData(): Uint8Array;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Dictionary-related type definitions.
|
|
262
|
+
*/
|
|
263
|
+
/** Character class definition entry */
|
|
264
|
+
interface CharacterClass {
|
|
265
|
+
/** Class name (e.g., DEFAULT, SPACE, KANJI) */
|
|
266
|
+
name: string;
|
|
267
|
+
/** Whether to invoke unknown word processing */
|
|
268
|
+
invoke: boolean;
|
|
269
|
+
/** Whether to group consecutive same-class chars */
|
|
270
|
+
group: boolean;
|
|
271
|
+
/** Maximum unknown word length (0 = unlimited) */
|
|
272
|
+
length: number;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Character type definition for unknown word processing.
|
|
277
|
+
* Maps Unicode characters to character classes used in MeCab's char.def.
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
declare class CharacterDefinition {
|
|
281
|
+
/** Character class definitions */
|
|
282
|
+
private characterClasses;
|
|
283
|
+
/** Mapping from character code to class index */
|
|
284
|
+
private characterCategoryMap;
|
|
285
|
+
/** Mapping from character code to compatible class bitmask */
|
|
286
|
+
private compatibleCategoryMap;
|
|
287
|
+
constructor();
|
|
288
|
+
addCharacterClass(charClass: CharacterClass): number;
|
|
289
|
+
setCharacterCategory(start: number, end: number, classId: number, compatibleClasses: number[]): void;
|
|
290
|
+
/**
|
|
291
|
+
* Lookup the character class for a given character code.
|
|
292
|
+
*/
|
|
293
|
+
lookup(charCode: number): CharacterClass;
|
|
294
|
+
/**
|
|
295
|
+
* Get the character class index for a character code.
|
|
296
|
+
*/
|
|
297
|
+
getCharacterClass(charCode: number): number;
|
|
298
|
+
/**
|
|
299
|
+
* Check if two character codes are in compatible classes.
|
|
300
|
+
*/
|
|
301
|
+
isCompatible(charCode1: number, charCode2: number): boolean;
|
|
302
|
+
/**
|
|
303
|
+
* Get the invoke flag for a character class.
|
|
304
|
+
*/
|
|
305
|
+
isInvoke(charCode: number): boolean;
|
|
306
|
+
/**
|
|
307
|
+
* Get the group flag for a character class.
|
|
308
|
+
*/
|
|
309
|
+
isGroup(charCode: number): boolean;
|
|
310
|
+
/**
|
|
311
|
+
* Get the max length for a character class.
|
|
312
|
+
*/
|
|
313
|
+
getMaxLength(charCode: number): number;
|
|
314
|
+
getCharacterClasses(): CharacterClass[];
|
|
315
|
+
getCategoryMap(): Uint8Array;
|
|
316
|
+
getCompatibleCategoryMap(): Uint32Array;
|
|
317
|
+
/**
|
|
318
|
+
* Load from binary buffers.
|
|
319
|
+
*/
|
|
320
|
+
static fromBuffers(categoryMap: Uint8Array, compatMap: Uint32Array, invokeDefBuf: Uint8Array): CharacterDefinition;
|
|
321
|
+
/**
|
|
322
|
+
* Serialize invoke definitions to buffer.
|
|
323
|
+
*/
|
|
324
|
+
toInvokeBuffer(): Uint8Array;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Unknown word dictionary.
|
|
329
|
+
* Handles words not found in the main dictionary by using character type information.
|
|
330
|
+
*/
|
|
331
|
+
|
|
332
|
+
declare class UnknownDictionary extends TokenInfoDictionary {
|
|
333
|
+
private characterDefinition;
|
|
334
|
+
constructor();
|
|
335
|
+
setCharacterDefinition(charDef: CharacterDefinition): void;
|
|
336
|
+
getCharacterDefinition(): CharacterDefinition;
|
|
337
|
+
/**
|
|
338
|
+
* Lookup unknown word entries for a given character class.
|
|
339
|
+
* Returns word IDs for the given character class index.
|
|
340
|
+
*/
|
|
341
|
+
lookup(charCode: number): number[];
|
|
342
|
+
/**
|
|
343
|
+
* Lookup word IDs by character class index.
|
|
344
|
+
*/
|
|
345
|
+
lookupByCharClass(_classId: number): number[];
|
|
346
|
+
/**
|
|
347
|
+
* Load from category map and dictionary data.
|
|
348
|
+
*/
|
|
349
|
+
loadCharacterDefinition(charDef: CharacterDefinition): this;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Container that holds all dictionary components needed for tokenization.
|
|
354
|
+
*/
|
|
355
|
+
|
|
356
|
+
declare class DictionaryContainer {
|
|
357
|
+
readonly trie: DoubleArray;
|
|
358
|
+
readonly tokenInfoDictionary: TokenInfoDictionary;
|
|
359
|
+
readonly connectionCosts: ConnectionCosts;
|
|
360
|
+
readonly unknownDictionary: UnknownDictionary;
|
|
361
|
+
constructor(trie: DoubleArray, tokenInfoDictionary: TokenInfoDictionary, connectionCosts: ConnectionCosts, unknownDictionary: UnknownDictionary);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Dictionary format interface.
|
|
366
|
+
* Abstracts the differences between IPAdic, UniDic, and NEologd.
|
|
367
|
+
*/
|
|
368
|
+
|
|
369
|
+
interface DictionaryFormat<T extends BaseToken = BaseToken> {
|
|
370
|
+
/** Format name */
|
|
371
|
+
readonly name: string;
|
|
372
|
+
/**
|
|
373
|
+
* Parse features string into a typed token.
|
|
374
|
+
* Features is a comma-separated string from the dictionary.
|
|
375
|
+
*/
|
|
376
|
+
parseToken(base: Omit<BaseToken, "cost"> & {
|
|
377
|
+
cost: number;
|
|
378
|
+
}, features: string): T;
|
|
379
|
+
/**
|
|
380
|
+
* Get the number of feature fields expected in this format.
|
|
381
|
+
*/
|
|
382
|
+
getFeatureCount(): number;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Main tokenizer class.
|
|
387
|
+
* Performs Japanese morphological analysis using Viterbi algorithm.
|
|
388
|
+
*/
|
|
389
|
+
|
|
390
|
+
declare class Tokenizer<T extends BaseToken = BaseToken> {
|
|
391
|
+
private readonly viterbiBuilder;
|
|
392
|
+
private readonly viterbiSearcher;
|
|
393
|
+
private readonly dictionary;
|
|
394
|
+
private readonly format;
|
|
395
|
+
constructor(dictionary: DictionaryContainer, format: DictionaryFormat<T>);
|
|
396
|
+
/**
|
|
397
|
+
* Tokenize input text into morphemes.
|
|
398
|
+
*/
|
|
399
|
+
tokenize(text: string): T[];
|
|
400
|
+
/**
|
|
401
|
+
* Get the dictionary format handler.
|
|
402
|
+
*/
|
|
403
|
+
getFormat(): DictionaryFormat<T>;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Tokenizer configuration options.
|
|
408
|
+
*/
|
|
409
|
+
type DictionaryFormatType = "ipadic" | "unidic" | "neologd";
|
|
410
|
+
interface TokenizerOptions {
|
|
411
|
+
/** Dictionary format to use */
|
|
412
|
+
format?: DictionaryFormatType;
|
|
413
|
+
/** Path to compiled dictionary directory */
|
|
414
|
+
dicPath: string;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Dictionary loader interface.
|
|
419
|
+
* Abstracts loading of compiled dictionary files across environments.
|
|
420
|
+
*/
|
|
421
|
+
interface DictionaryLoader {
|
|
422
|
+
/**
|
|
423
|
+
* Load a gzipped binary file and return the decompressed data.
|
|
424
|
+
*/
|
|
425
|
+
loadArrayBuffer(path: string): Promise<ArrayBuffer>;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Asynchronous tokenizer builder.
|
|
430
|
+
* Loads dictionary files and constructs a ready-to-use Tokenizer.
|
|
431
|
+
*/
|
|
432
|
+
|
|
433
|
+
declare class TokenizerBuilder {
|
|
434
|
+
/**
|
|
435
|
+
* Build a tokenizer with the given options.
|
|
436
|
+
*/
|
|
437
|
+
static build<T extends BaseToken>(_options: TokenizerOptions, loader: DictionaryLoader, format: DictionaryFormat<T>): Promise<Tokenizer<T>>;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* IPAdic dictionary format parser.
|
|
442
|
+
* Parses features in the IPAdic format (MeCab-IPADIC).
|
|
443
|
+
*/
|
|
444
|
+
|
|
445
|
+
declare class IpadicFormatHandler implements DictionaryFormat<IpadicToken> {
|
|
446
|
+
readonly name: string;
|
|
447
|
+
parseToken(base: Omit<BaseToken, "cost"> & {
|
|
448
|
+
cost: number;
|
|
449
|
+
}, features: string): IpadicToken;
|
|
450
|
+
getFeatureCount(): number;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* UniDic dictionary format parser.
|
|
455
|
+
*/
|
|
456
|
+
|
|
457
|
+
declare class UnidicFormatHandler implements DictionaryFormat<UnidicToken> {
|
|
458
|
+
readonly name = "unidic";
|
|
459
|
+
parseToken(base: Omit<BaseToken, "cost"> & {
|
|
460
|
+
cost: number;
|
|
461
|
+
}, features: string): UnidicToken;
|
|
462
|
+
getFeatureCount(): number;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* NEologd dictionary format parser.
|
|
467
|
+
* NEologd is based on IPAdic format with the same feature structure.
|
|
468
|
+
*/
|
|
469
|
+
|
|
470
|
+
declare class NeologdFormatHandler extends IpadicFormatHandler {
|
|
471
|
+
readonly name: string;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Builds a double array trie from a set of sorted keys.
|
|
476
|
+
*
|
|
477
|
+
* The double array structure uses two parallel arrays (base and check)
|
|
478
|
+
* to represent a trie compactly. This is the standard Aoe algorithm.
|
|
479
|
+
*/
|
|
480
|
+
interface DoubleArrayBuildResult {
|
|
481
|
+
base: Int32Array;
|
|
482
|
+
check: Int32Array;
|
|
483
|
+
}
|
|
484
|
+
declare class DoubleArrayBuilder {
|
|
485
|
+
/**
|
|
486
|
+
* Build a double array from sorted key-value pairs.
|
|
487
|
+
* Keys must be sorted in lexicographic order.
|
|
488
|
+
* Each key is an array of character codes (unsigned integers > 0).
|
|
489
|
+
*/
|
|
490
|
+
static build(keys: {
|
|
491
|
+
key: number[];
|
|
492
|
+
value: number;
|
|
493
|
+
}[]): DoubleArrayBuildResult;
|
|
494
|
+
private base;
|
|
495
|
+
private check;
|
|
496
|
+
private nextCheckPos;
|
|
497
|
+
constructor();
|
|
498
|
+
private buildFromKeys;
|
|
499
|
+
private buildTrie;
|
|
500
|
+
private ensureSize;
|
|
501
|
+
private insert;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Dictionary loader for Node.js using native fs and zlib.
|
|
506
|
+
*/
|
|
507
|
+
|
|
508
|
+
declare class NodeLoader implements DictionaryLoader {
|
|
509
|
+
private readonly basePath;
|
|
510
|
+
constructor(basePath: string);
|
|
511
|
+
loadArrayBuffer(fileName: string): Promise<ArrayBuffer>;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Dictionary loader for browser using fetch and DecompressionStream.
|
|
516
|
+
*/
|
|
517
|
+
|
|
518
|
+
declare class BrowserLoader implements DictionaryLoader {
|
|
519
|
+
private readonly baseUrl;
|
|
520
|
+
constructor(baseUrl: string);
|
|
521
|
+
loadArrayBuffer(fileName: string): Promise<ArrayBuffer>;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Unicode-aware string that correctly handles surrogate pairs.
|
|
526
|
+
* Provides character-level indexing that treats surrogate pairs as single characters.
|
|
527
|
+
*/
|
|
528
|
+
declare class SurrogateAwareString {
|
|
529
|
+
private readonly codePoints;
|
|
530
|
+
readonly length: number;
|
|
531
|
+
constructor(str: string);
|
|
532
|
+
charAt(index: number): string;
|
|
533
|
+
charCodeAt(index: number): number;
|
|
534
|
+
substring(start: number, end?: number): string;
|
|
535
|
+
slice(start: number, end?: number): string;
|
|
536
|
+
toString(): string;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* tokana - Modern Japanese Morphological Analyzer
|
|
541
|
+
*/
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Create a tokenizer with the given options.
|
|
545
|
+
*
|
|
546
|
+
* @example
|
|
547
|
+
* ```typescript
|
|
548
|
+
* const tokenizer = await createTokenizer({
|
|
549
|
+
* format: "ipadic",
|
|
550
|
+
* dicPath: "./dict",
|
|
551
|
+
* });
|
|
552
|
+
* const tokens = tokenizer.tokenize("東京都に住んでいる");
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
declare function createTokenizer(options: TokenizerOptions & {
|
|
556
|
+
format: "ipadic";
|
|
557
|
+
}): Promise<Tokenizer<IpadicToken>>;
|
|
558
|
+
declare function createTokenizer(options: TokenizerOptions & {
|
|
559
|
+
format: "unidic";
|
|
560
|
+
}): Promise<Tokenizer<UnidicToken>>;
|
|
561
|
+
declare function createTokenizer(options: TokenizerOptions & {
|
|
562
|
+
format: "neologd";
|
|
563
|
+
}): Promise<Tokenizer<IpadicToken>>;
|
|
564
|
+
declare function createTokenizer(options: TokenizerOptions): Promise<Tokenizer<BaseToken>>;
|
|
565
|
+
|
|
566
|
+
export { type BaseToken, BrowserLoader, ByteBuffer, CharacterDefinition, ConnectionCosts, DictionaryContainer, type DictionaryFormat, type DictionaryFormatType, type DictionaryLoader, DoubleArray, DoubleArrayBuilder, IpadicFormatHandler, type IpadicToken, NeologdFormatHandler, NodeLoader, SurrogateAwareString, type Token, TokenInfoDictionary, type TokenType, Tokenizer, TokenizerBuilder, type TokenizerOptions, UnidicFormatHandler, type UnidicToken, UnknownDictionary, createTokenizer };
|