tinyfix-tr 0.0.0-stage → 0.1.1

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +139 -2
  3. package/dist/pkg/src/context.d.ts +13 -0
  4. package/dist/pkg/src/context.js +80 -0
  5. package/dist/pkg/src/data.d.ts +53 -0
  6. package/dist/pkg/src/data.js +498 -0
  7. package/dist/pkg/src/engine.d.ts +39 -0
  8. package/dist/pkg/src/engine.js +357 -0
  9. package/dist/pkg/src/index.d.ts +30 -0
  10. package/dist/pkg/src/index.js +85 -0
  11. package/dist/pkg/src/protocol.d.ts +64 -0
  12. package/dist/pkg/src/protocol.js +1 -0
  13. package/dist/pkg/src/types.d.ts +184 -0
  14. package/dist/pkg/src/types.js +16 -0
  15. package/dist/pkg/src/worker.d.ts +11 -0
  16. package/dist/pkg/src/worker.js +111 -0
  17. package/dist/pkg/src/workerClient.d.ts +3 -0
  18. package/dist/pkg/src/workerClient.js +202 -0
  19. package/dist/src/context-vocab.d.ts +1 -0
  20. package/dist/src/context-vocab.js +2 -0
  21. package/dist/src/context.d.ts +92 -0
  22. package/dist/src/context.js +390 -0
  23. package/dist/src/corrector.d.ts +338 -0
  24. package/dist/src/corrector.js +2684 -0
  25. package/dist/src/known.d.ts +88 -0
  26. package/dist/src/known.js +286 -0
  27. package/dist/src/lexicon-binary.d.ts +85 -0
  28. package/dist/src/lexicon-binary.js +805 -0
  29. package/dist/src/lexicon.d.ts +24 -0
  30. package/dist/src/lexicon.js +79 -0
  31. package/dist/src/morphology/analyzer.d.ts +30 -0
  32. package/dist/src/morphology/analyzer.js +311 -0
  33. package/dist/src/morphology/binary.d.ts +22 -0
  34. package/dist/src/morphology/binary.js +372 -0
  35. package/dist/src/morphology/dictionary.d.ts +21 -0
  36. package/dist/src/morphology/dictionary.js +559 -0
  37. package/dist/src/morphology/fuzzy.d.ts +44 -0
  38. package/dist/src/morphology/fuzzy.js +599 -0
  39. package/dist/src/morphology/index.d.ts +40 -0
  40. package/dist/src/morphology/index.js +81 -0
  41. package/dist/src/morphology/morphotactics.d.ts +148 -0
  42. package/dist/src/morphology/morphotactics.js +1871 -0
  43. package/dist/src/morphology/phonetics.d.ts +71 -0
  44. package/dist/src/morphology/phonetics.js +309 -0
  45. package/dist/src/morphology/prior.d.ts +61 -0
  46. package/dist/src/morphology/prior.js +457 -0
  47. package/dist/src/morphology/types.d.ts +110 -0
  48. package/dist/src/morphology/types.js +32 -0
  49. package/dist/src/rules/proper-names.d.ts +145 -0
  50. package/dist/src/rules/proper-names.js +1217 -0
  51. package/dist/src/rules/turkish-rules.d.ts +126 -0
  52. package/dist/src/rules/turkish-rules.js +365 -0
  53. package/dist/src/sentence.d.ts +83 -0
  54. package/dist/src/sentence.js +2408 -0
  55. package/dist/src/turkish.d.ts +20 -0
  56. package/dist/src/turkish.js +94 -0
  57. package/package.json +60 -4
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Compact binary serialization (`AMR1`) for parsed Zemberek morphology dictionaries.
3
+ * Browser-safe (standard TypedArrays + TextEncoder/TextDecoder only) and lossless:
4
+ * decoding reconstructs the exact DictionaryItem[] produced by `parseDictionaries()`.
5
+ */
6
+ import { normalizeCircumflex } from "./phonetics.js";
7
+ import { decodeMorphPrior, encodeMorphPrior } from "./prior.js";
8
+ import { RA_DUMMY, } from "./types.js";
9
+ const MAGIC_0 = 0x41; // 'A'
10
+ const MAGIC_1 = 0x4d; // 'M'
11
+ const MAGIC_2 = 0x52; // 'R'
12
+ const MAGIC_3 = 0x31; // '1'
13
+ const FORMAT_VERSION_V1 = 1;
14
+ const FORMAT_VERSION_V2 = 2;
15
+ const PRIMARY_POS_LIST = [
16
+ "Noun",
17
+ "Adj",
18
+ "Verb",
19
+ "Pron",
20
+ "Adv",
21
+ "Conj",
22
+ "Ques",
23
+ "Postp",
24
+ "Det",
25
+ "Num",
26
+ "Dup",
27
+ "Interj",
28
+ "Punc",
29
+ ];
30
+ const SECONDARY_POS_LIST = [
31
+ "None",
32
+ "ProperNoun",
33
+ "Abbreviation",
34
+ "Time",
35
+ "PersonalPron",
36
+ "DemonstrativePron",
37
+ "QuantitivePron",
38
+ "QuestionPron",
39
+ "ReflexivePron",
40
+ "Card",
41
+ "Ord",
42
+ "Dist",
43
+ "PCDat",
44
+ "PCAbl",
45
+ "PCNom",
46
+ "PCGen",
47
+ "PCIns",
48
+ "PCAcc",
49
+ ];
50
+ const PRIMARY_POS_INDEX = new Map(PRIMARY_POS_LIST.map((p, i) => [p, i]));
51
+ const SECONDARY_POS_INDEX = new Map(SECONDARY_POS_LIST.map((p, i) => [p, i]));
52
+ const LEMMA_MODE_MASK = 0x03;
53
+ const LEMMA_MODE_ROOT = 0;
54
+ const LEMMA_MODE_VERB_MAK = 1;
55
+ const LEMMA_MODE_VERB_MEK = 2;
56
+ const LEMMA_MODE_EXPLICIT = 3;
57
+ const FLAG_HAS_PRON = 1 << 2;
58
+ const FLAG_HAS_REF = 1 << 3;
59
+ const FLAG_HAS_INDEX = 1 << 4;
60
+ const FLAG_HAS_KEY = 1 << 5;
61
+ function defaultItemKey(root, lemma, primaryPos, secondaryPos, attributes) {
62
+ if ((attributes & RA_DUMMY) !== 0) {
63
+ return `${root}_Noun_CompoundRoot`;
64
+ }
65
+ const normLemma = normalizeCircumflex(lemma);
66
+ return secondaryPos === "None" ? `${normLemma}_${primaryPos}` : `${normLemma}_${primaryPos}_${secondaryPos}`;
67
+ }
68
+ function writeVarint(out, value) {
69
+ let v = value >>> 0;
70
+ while (v >= 0x80) {
71
+ out.push((v & 0x7f) | 0x80);
72
+ v >>>= 7;
73
+ }
74
+ out.push(v);
75
+ }
76
+ /**
77
+ * Encodes parsed `DictionaryItem[]` into a compact, columnar, front-coded `AMR1` (or `AMR2` with prior) binary payload.
78
+ */
79
+ export function encodeBinaryDictionary(items, prior) {
80
+ const encoder = new TextEncoder();
81
+ // Sort items by UTF-8 root bytes for optimal front-coding, preserving deterministic tie-breaking.
82
+ const entries = items.map((item, origIdx) => ({
83
+ item,
84
+ origIdx,
85
+ rootBytes: encoder.encode(item.root),
86
+ }));
87
+ entries.sort((a, b) => {
88
+ const minLen = Math.min(a.rootBytes.length, b.rootBytes.length);
89
+ for (let i = 0; i < minLen; i++) {
90
+ const diff = a.rootBytes[i] - b.rootBytes[i];
91
+ if (diff !== 0)
92
+ return diff;
93
+ }
94
+ if (a.rootBytes.length !== b.rootBytes.length)
95
+ return a.rootBytes.length - b.rootBytes.length;
96
+ return a.origIdx - b.origIdx;
97
+ });
98
+ const itemCount = entries.length;
99
+ const newIndexOf = new Map();
100
+ for (let i = 0; i < itemCount; i++) {
101
+ newIndexOf.set(entries[i].item, i);
102
+ }
103
+ // Build attribute bitmask palette (typically < 50 unique masks across ~31K items).
104
+ const attrPalette = [];
105
+ const attrToIdx = new Map();
106
+ for (let i = 0; i < itemCount; i++) {
107
+ const attrs = entries[i].item.attributes >>> 0;
108
+ if (!attrToIdx.has(attrs)) {
109
+ attrToIdx.set(attrs, attrPalette.length);
110
+ attrPalette.push(attrs);
111
+ }
112
+ }
113
+ const wideAttr = attrPalette.length > 255;
114
+ const prefixLens = new Uint8Array(itemCount);
115
+ const suffixLens = new Uint8Array(itemCount);
116
+ const rootStream = [];
117
+ const posSec = new Uint8Array(itemCount);
118
+ const attrCol = wideAttr ? new Uint8Array(itemCount * 2) : new Uint8Array(itemCount);
119
+ const flagsCol = new Uint8Array(itemCount);
120
+ const extras = [];
121
+ let prevRoot = new Uint8Array(0);
122
+ const secCount = SECONDARY_POS_LIST.length;
123
+ for (let i = 0; i < itemCount; i++) {
124
+ const { item, rootBytes } = entries[i];
125
+ let prefix = 0;
126
+ const maxPrefix = Math.min(prevRoot.length, rootBytes.length, 255);
127
+ while (prefix < maxPrefix && prevRoot[prefix] === rootBytes[prefix]) {
128
+ prefix++;
129
+ }
130
+ const suffix = rootBytes.length - prefix;
131
+ if (suffix > 255) {
132
+ throw new Error(`morph binary: root suffix exceeds 255 bytes ("${item.root}")`);
133
+ }
134
+ prefixLens[i] = prefix;
135
+ suffixLens[i] = suffix;
136
+ for (let j = prefix; j < rootBytes.length; j++) {
137
+ rootStream.push(rootBytes[j]);
138
+ }
139
+ prevRoot = rootBytes;
140
+ const pIdx = PRIMARY_POS_INDEX.get(item.primaryPos);
141
+ const sIdx = SECONDARY_POS_INDEX.get(item.secondaryPos);
142
+ if (pIdx === undefined || sIdx === undefined) {
143
+ throw new Error(`morph binary: unknown POS (${item.primaryPos}, ${item.secondaryPos})`);
144
+ }
145
+ posSec[i] = pIdx * secCount + sIdx;
146
+ const aIdx = attrToIdx.get(item.attributes >>> 0);
147
+ if (wideAttr) {
148
+ attrCol[i * 2] = aIdx & 0xff;
149
+ attrCol[i * 2 + 1] = (aIdx >>> 8) & 0xff;
150
+ }
151
+ else {
152
+ attrCol[i] = aIdx;
153
+ }
154
+ let flags = 0;
155
+ if (item.lemma === item.root) {
156
+ flags |= LEMMA_MODE_ROOT;
157
+ }
158
+ else if (item.primaryPos === "Verb" && item.lemma === `${item.root}mak`) {
159
+ flags |= LEMMA_MODE_VERB_MAK;
160
+ }
161
+ else if (item.primaryPos === "Verb" && item.lemma === `${item.root}mek`) {
162
+ flags |= LEMMA_MODE_VERB_MEK;
163
+ }
164
+ else {
165
+ flags |= LEMMA_MODE_EXPLICIT;
166
+ const lemmaBytes = encoder.encode(item.lemma);
167
+ writeVarint(extras, lemmaBytes.length);
168
+ for (let j = 0; j < lemmaBytes.length; j++)
169
+ extras.push(lemmaBytes[j]);
170
+ }
171
+ if (item.pronunciation !== item.root) {
172
+ flags |= FLAG_HAS_PRON;
173
+ const pronBytes = encoder.encode(item.pronunciation);
174
+ writeVarint(extras, pronBytes.length);
175
+ for (let j = 0; j < pronBytes.length; j++)
176
+ extras.push(pronBytes[j]);
177
+ }
178
+ if (item.referenceItem !== null) {
179
+ const refIdx = newIndexOf.get(item.referenceItem);
180
+ if (refIdx !== undefined) {
181
+ flags |= FLAG_HAS_REF;
182
+ writeVarint(extras, refIdx);
183
+ }
184
+ }
185
+ if (item.index !== 0) {
186
+ flags |= FLAG_HAS_INDEX;
187
+ writeVarint(extras, item.index);
188
+ }
189
+ const expectedKey = defaultItemKey(item.root, item.lemma, item.primaryPos, item.secondaryPos, item.attributes);
190
+ if (item.key !== expectedKey) {
191
+ flags |= FLAG_HAS_KEY;
192
+ const keyBytes = encoder.encode(item.key);
193
+ writeVarint(extras, keyBytes.length);
194
+ for (let j = 0; j < keyBytes.length; j++)
195
+ extras.push(keyBytes[j]);
196
+ }
197
+ flagsCol[i] = flags;
198
+ }
199
+ const rootStreamBytes = new Uint8Array(rootStream);
200
+ const extrasBytes = new Uint8Array(extras);
201
+ const sortedItems = entries.map((e) => e.item);
202
+ const priorBytes = prior ? encodeMorphPrior(prior, sortedItems) : null;
203
+ const headerSize = 4 + 1 + 4 + 2 + attrPalette.length * 4 + 4 + 4;
204
+ const totalSize = headerSize +
205
+ prefixLens.byteLength +
206
+ suffixLens.byteLength +
207
+ rootStreamBytes.byteLength +
208
+ posSec.byteLength +
209
+ attrCol.byteLength +
210
+ flagsCol.byteLength +
211
+ extrasBytes.byteLength +
212
+ (priorBytes ? priorBytes.byteLength : 0);
213
+ const out = new Uint8Array(totalSize);
214
+ const view = new DataView(out.buffer);
215
+ let offset = 0;
216
+ out[offset++] = MAGIC_0;
217
+ out[offset++] = MAGIC_1;
218
+ out[offset++] = MAGIC_2;
219
+ out[offset++] = MAGIC_3;
220
+ out[offset++] = priorBytes ? FORMAT_VERSION_V2 : FORMAT_VERSION_V1;
221
+ view.setUint32(offset, itemCount, true);
222
+ offset += 4;
223
+ view.setUint16(offset, attrPalette.length, true);
224
+ offset += 2;
225
+ for (let i = 0; i < attrPalette.length; i++) {
226
+ view.setUint32(offset, attrPalette[i], true);
227
+ offset += 4;
228
+ }
229
+ view.setUint32(offset, rootStreamBytes.byteLength, true);
230
+ offset += 4;
231
+ view.setUint32(offset, extrasBytes.byteLength, true);
232
+ offset += 4;
233
+ const parts = priorBytes
234
+ ? [prefixLens, suffixLens, rootStreamBytes, posSec, attrCol, flagsCol, extrasBytes, priorBytes]
235
+ : [prefixLens, suffixLens, rootStreamBytes, posSec, attrCol, flagsCol, extrasBytes];
236
+ for (const part of parts) {
237
+ out.set(part, offset);
238
+ offset += part.byteLength;
239
+ }
240
+ return out;
241
+ }
242
+ /**
243
+ * Decodes an `AMR1` or `AMR2` binary morphology payload into exact `DictionaryItem[]` plus optional `MorphPrior`.
244
+ */
245
+ export function parseBinaryDictionaryWithPrior(bytes) {
246
+ if (bytes.byteLength < 19 ||
247
+ bytes[0] !== MAGIC_0 ||
248
+ bytes[1] !== MAGIC_1 ||
249
+ bytes[2] !== MAGIC_2 ||
250
+ bytes[3] !== MAGIC_3) {
251
+ throw new Error("autr: Geçersiz biçimbilim ikili başlığı (AMR1 bekleniyordu).");
252
+ }
253
+ const version = bytes[4];
254
+ if (version !== FORMAT_VERSION_V1 && version !== FORMAT_VERSION_V2) {
255
+ throw new Error(`autr: Desteklenmeyen biçimbilim biçim sürümü: ${version}.`);
256
+ }
257
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
258
+ let offset = 5;
259
+ const itemCount = view.getUint32(offset, true);
260
+ offset += 4;
261
+ const attrPaletteLen = view.getUint16(offset, true);
262
+ offset += 2;
263
+ const attrPalette = new Int32Array(attrPaletteLen);
264
+ for (let i = 0; i < attrPaletteLen; i++) {
265
+ attrPalette[i] = view.getUint32(offset, true) | 0;
266
+ offset += 4;
267
+ }
268
+ const rootsByteLen = view.getUint32(offset, true);
269
+ offset += 4;
270
+ const extrasByteLen = view.getUint32(offset, true);
271
+ offset += 4;
272
+ const wideAttr = attrPaletteLen > 255;
273
+ const attrColLen = wideAttr ? itemCount * 2 : itemCount;
274
+ const prefixLens = bytes.subarray(offset, offset + itemCount);
275
+ offset += itemCount;
276
+ const suffixLens = bytes.subarray(offset, offset + itemCount);
277
+ offset += itemCount;
278
+ const rootStream = bytes.subarray(offset, offset + rootsByteLen);
279
+ offset += rootsByteLen;
280
+ const posSec = bytes.subarray(offset, offset + itemCount);
281
+ offset += itemCount;
282
+ const attrCol = bytes.subarray(offset, offset + attrColLen);
283
+ offset += attrColLen;
284
+ const flagsCol = bytes.subarray(offset, offset + itemCount);
285
+ offset += itemCount;
286
+ const extras = bytes.subarray(offset, offset + extrasByteLen);
287
+ const priorOffset = offset + extrasByteLen;
288
+ const decoder = new TextDecoder("utf-8");
289
+ const curRootBytes = new Uint8Array(256);
290
+ const items = new Array(itemCount);
291
+ const pendingRefs = [];
292
+ const secCount = SECONDARY_POS_LIST.length;
293
+ let rootOff = 0;
294
+ let extOff = 0;
295
+ const readVarint = () => {
296
+ let result = 0;
297
+ let shift = 0;
298
+ for (;;) {
299
+ const b = extras[extOff++];
300
+ result |= (b & 0x7f) << shift;
301
+ if ((b & 0x80) === 0)
302
+ return result >>> 0;
303
+ shift += 7;
304
+ }
305
+ };
306
+ const readString = () => {
307
+ const len = readVarint();
308
+ const s = decoder.decode(extras.subarray(extOff, extOff + len));
309
+ extOff += len;
310
+ return s;
311
+ };
312
+ for (let i = 0; i < itemCount; i++) {
313
+ const p = prefixLens[i];
314
+ const s = suffixLens[i];
315
+ curRootBytes.set(rootStream.subarray(rootOff, rootOff + s), p);
316
+ rootOff += s;
317
+ const root = decoder.decode(curRootBytes.subarray(0, p + s));
318
+ const ps = posSec[i];
319
+ const primaryPos = PRIMARY_POS_LIST[Math.floor(ps / secCount)];
320
+ const secondaryPos = SECONDARY_POS_LIST[ps % secCount];
321
+ const aIdx = wideAttr ? attrCol[i * 2] | (attrCol[i * 2 + 1] << 8) : attrCol[i];
322
+ const attributes = attrPalette[aIdx];
323
+ const flags = flagsCol[i];
324
+ const lemmaMode = flags & LEMMA_MODE_MASK;
325
+ let lemma;
326
+ if (lemmaMode === LEMMA_MODE_ROOT)
327
+ lemma = root;
328
+ else if (lemmaMode === LEMMA_MODE_VERB_MAK)
329
+ lemma = `${root}mak`;
330
+ else if (lemmaMode === LEMMA_MODE_VERB_MEK)
331
+ lemma = `${root}mek`;
332
+ else
333
+ lemma = readString();
334
+ const pronunciation = (flags & FLAG_HAS_PRON) !== 0 ? readString() : root;
335
+ if ((flags & FLAG_HAS_REF) !== 0) {
336
+ pendingRefs.push([i, readVarint()]);
337
+ }
338
+ const index = (flags & FLAG_HAS_INDEX) !== 0 ? readVarint() : 0;
339
+ const key = (flags & FLAG_HAS_KEY) !== 0
340
+ ? readString()
341
+ : defaultItemKey(root, lemma, primaryPos, secondaryPos, attributes);
342
+ items[i] = {
343
+ key,
344
+ lemma,
345
+ root,
346
+ pronunciation,
347
+ primaryPos,
348
+ secondaryPos,
349
+ attributes,
350
+ index,
351
+ referenceItem: null,
352
+ };
353
+ }
354
+ for (let i = 0; i < pendingRefs.length; i++) {
355
+ const [itemIdx, refIdx] = pendingRefs[i];
356
+ const item = items[itemIdx];
357
+ const ref = items[refIdx];
358
+ if (item !== undefined && ref !== undefined) {
359
+ item.referenceItem = ref;
360
+ }
361
+ }
362
+ const prior = version === FORMAT_VERSION_V2 && priorOffset < bytes.byteLength
363
+ ? decodeMorphPrior(bytes, priorOffset, items)
364
+ : undefined;
365
+ return { items, prior };
366
+ }
367
+ /**
368
+ * Decodes an `AMR1`/`AMR2` binary morphology payload back into exact `DictionaryItem[]`.
369
+ */
370
+ export function parseBinaryDictionary(bytes) {
371
+ return parseBinaryDictionaryWithPrior(bytes).items;
372
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Zemberek .dict parser, Turkish root attribute inference, and stem transition generator.
3
+ * Adapted from Zemberek's TextDictionaryLoader and StemTransitionsBase (Apache 2.0).
4
+ */
5
+ import type { MorphotacticsGraph } from "./morphotactics.ts";
6
+ import { type DictionaryItem, type StemTransition } from "./types.ts";
7
+ /**
8
+ * Parses one or more Zemberek `.dict` text payloads into DictionaryItems.
9
+ * Entries marked `A:Informal` or `A:Ext` (non-TDK noisy/colloquial spellings) and
10
+ * `P:Punc` entries are skipped to preserve high spellchecking precision.
11
+ */
12
+ export declare function parseDictionaries(texts: readonly string[]): DictionaryItem[];
13
+ export interface StemIndex {
14
+ readonly stems: Map<string, StemTransition | StemTransition[]>;
15
+ readonly maxStemLength: number;
16
+ }
17
+ /**
18
+ * Generates all surface stem transitions for the parsed dictionary items and indexes
19
+ * them in a compact Map (single transitions stored directly without wrapper arrays).
20
+ */
21
+ export declare function buildStemIndex(items: readonly DictionaryItem[], graph: MorphotacticsGraph): StemIndex;