tinyfix-tr 0.0.0-stage → 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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +138 -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 +50 -0
  6. package/dist/pkg/src/data.js +495 -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,357 @@
1
+ import { CAREFUL, Corrector } from "../../src/corrector.js";
2
+ import { lowerTr } from "../../src/turkish.js";
3
+ /** Temkinli kip: motorun doğrulanan varsayılan eşikleri. */
4
+ export { CAREFUL };
5
+ /** Dengeli kip: `CAREFUL` ayarından biraz daha cesur eşikler. */
6
+ export const BALANCED = {
7
+ ...CAREFUL,
8
+ threshold: { marks: 1.8, apostrophe: 2.0, split: 2.0, join: 2.0, typo: 2.3, known: 0.7, context: 2.5, contraction: 0.7, punct: 1 },
9
+ cost: { marks: 0.4, apostrophe: 0.5, split: 0.4, join: 0.4, typo: 1.7, known: 0, context: 0, contraction: 0, punct: 0 },
10
+ ambiguity: 0.7,
11
+ knownWord: 5,
12
+ knownMarked: 15,
13
+ markedOverride: 4.5,
14
+ minName: 12,
15
+ };
16
+ /** Kipin eşikleri; kısaltmalar yalnızca "kisaltma" türü açıkça istendiğinde açılır. */
17
+ function settingsFor(mode, kinds) {
18
+ return {
19
+ ...SETTINGS_BY_MODE[mode],
20
+ contractions: kinds?.has("kisaltma") ?? false,
21
+ // Cümle başı büyük harf ve nokta sonrası boşluk ("merhaba.ben" → "Merhaba. Ben").
22
+ sentenceCase: kinds ? kinds.has("noktalama") : true,
23
+ };
24
+ }
25
+ const SETTINGS_BY_MODE = {
26
+ careful: CAREFUL,
27
+ balanced: BALANCED,
28
+ };
29
+ const FENCED_CODE = /```[\s\S]*?(?:```|$)/g;
30
+ const INLINE_CODE = /`[^`\n]*(?:`|$)/gm;
31
+ function findCodeRanges(text) {
32
+ if (!text.includes("`"))
33
+ return [];
34
+ const ranges = [];
35
+ for (const pattern of [FENCED_CODE, INLINE_CODE]) {
36
+ for (const match of text.matchAll(pattern)) {
37
+ ranges.push([match.index, match.index + match[0].length]);
38
+ }
39
+ }
40
+ return ranges;
41
+ }
42
+ /** Motorun iç düzeltme türünü dışarıya açılan anlaşılır Türkçe türe eşler. */
43
+ /**
44
+ * Birleştirme, ayırma ve bağlam modeli kararları komşu kelimelere ya da kelime çiftlerinin sıklığına bakar; iki
45
+ * kelimeyi kapsayan her düzeltme de öyledir ("hiç bir" → "hiçbir"). Geri kalanı yalnızca kelimeye bakar.
46
+ */
47
+ export function correctionScope(correction) {
48
+ if (correction.kind === "join" || correction.kind === "split" || correction.kind === "context" || correction.kind === "punct") {
49
+ return "baglam";
50
+ }
51
+ return correction.original.includes(" ") ? "baglam" : "kelime";
52
+ }
53
+ export function mapCorrectionKind(correction) {
54
+ switch (correction.kind) {
55
+ case "marks":
56
+ return "turkce-karakter";
57
+ case "apostrophe":
58
+ return "kesme";
59
+ case "split":
60
+ case "join":
61
+ return "bitisik-ayri";
62
+ case "typo":
63
+ return "yazim";
64
+ case "contraction":
65
+ return "kisaltma";
66
+ case "punct":
67
+ return "noktalama";
68
+ case "context":
69
+ case "known": {
70
+ const originalHasSpace = correction.original.includes(" ");
71
+ const replacementHasSpace = correction.replacement.includes(" ");
72
+ if (originalHasSpace !== replacementHasSpace)
73
+ return "bitisik-ayri";
74
+ const strip = (value) => lowerTr(value).replace(/\s+/g, "");
75
+ if (strip(correction.original) === strip(correction.replacement))
76
+ return "bitisik-ayri";
77
+ const fold = (value) => lowerTr(value).replace(/[çğıöşüâîû]/g, (char) => "cgiosuaiu"["çğıöşüâîû".indexOf(char)]);
78
+ if (fold(correction.original) === fold(correction.replacement))
79
+ return "turkce-karakter";
80
+ return "yazim";
81
+ }
82
+ }
83
+ }
84
+ /** Motorun doğal logaritmik `margin` değerini 0–1 aralığında güven puanına dönüştürür. */
85
+ export function marginToConfidence(margin) {
86
+ if (!Number.isFinite(margin))
87
+ return margin > 0 ? 1 : 0;
88
+ const raw = 1 / (1 + Math.exp(-margin / 2));
89
+ return Math.min(1, Math.max(0, Math.round(raw * 1000) / 1000));
90
+ }
91
+ function normalizeIgnoredWord(word) {
92
+ return lowerTr(word.trim());
93
+ }
94
+ /** Verilen düzeltmeleri metne uygular. Çakışma veya uyuşmazlık durumunda hata fırlatır. */
95
+ export function applyFixes(text, fixes) {
96
+ if (fixes.length === 0)
97
+ return text;
98
+ const ordered = [...fixes].sort((first, second) => first.from - second.from);
99
+ let output = "";
100
+ let cursor = 0;
101
+ for (const fix of ordered) {
102
+ if (fix.from < 0 || fix.to < fix.from || fix.to > text.length) {
103
+ throw new Error(`autr: Geçersiz düzeltme aralığı [${fix.from}, ${fix.to}).`);
104
+ }
105
+ if (fix.from < cursor) {
106
+ throw new Error(`autr: Çakışan düzeltmeler uygulanamaz (${fix.from} < ${cursor}).`);
107
+ }
108
+ const actual = text.slice(fix.from, fix.to);
109
+ if (actual !== fix.original) {
110
+ throw new Error(`autr: ${fix.from} konumunda "${fix.original}" bekleniyordu, metinde "${actual}" bulundu.`);
111
+ }
112
+ output += text.slice(cursor, fix.from) + fix.replacement;
113
+ cursor = fix.to;
114
+ }
115
+ return output + text.slice(cursor);
116
+ }
117
+ /** Çekirdek motoru (ve isteğe bağlı bağlam modelini) paket API'si ile sarmalayan çalışma motoru. */
118
+ export class TdkaiEngine {
119
+ corrector;
120
+ ignored = new Set();
121
+ contextSession;
122
+ asyncQueue = Promise.resolve();
123
+ mode;
124
+ disposed = false;
125
+ constructor(data, mode = "careful", ignored, contextSession = data.contextSession ?? null) {
126
+ this.mode = mode;
127
+ this.contextSession = contextSession;
128
+ this.corrector = new Corrector(data.lexicon, SETTINGS_BY_MODE[mode], data.known, data.morphology);
129
+ if (ignored) {
130
+ for (const word of ignored)
131
+ this.ignore(word);
132
+ }
133
+ }
134
+ get hasContext() {
135
+ return this.contextSession !== null;
136
+ }
137
+ assertActive() {
138
+ if (this.disposed) {
139
+ throw new Error("autr: Örnek kapatıldı (dispose edildi) ve artık kullanılamaz.");
140
+ }
141
+ }
142
+ setMode(mode) {
143
+ this.assertActive();
144
+ this.mode = mode;
145
+ this.corrector.settings = SETTINGS_BY_MODE[mode];
146
+ }
147
+ ignore(word) {
148
+ this.assertActive();
149
+ const normalized = normalizeIgnoredWord(word);
150
+ if (normalized)
151
+ this.ignored.add(normalized);
152
+ }
153
+ unignore(word) {
154
+ this.assertActive();
155
+ const normalized = normalizeIgnoredWord(word);
156
+ if (normalized)
157
+ this.ignored.delete(normalized);
158
+ }
159
+ filterCorrections(text, corrections, range, kindFilter) {
160
+ if (corrections.length === 0)
161
+ return [];
162
+ const codeRanges = findCodeRanges(text);
163
+ const fixes = [];
164
+ let lastEnd = -1;
165
+ for (const correction of corrections) {
166
+ if (correction.original === correction.replacement)
167
+ continue;
168
+ if (correction.start < lastEnd)
169
+ continue;
170
+ if (range) {
171
+ const [rangeFrom, rangeTo] = range;
172
+ if (!(correction.start < rangeTo && correction.end > rangeFrom))
173
+ continue;
174
+ }
175
+ if (codeRanges.length > 0 &&
176
+ codeRanges.some(([codeStart, codeEnd]) => correction.start < codeEnd && correction.end > codeStart)) {
177
+ continue;
178
+ }
179
+ if (this.ignored.has(normalizeIgnoredWord(correction.original)))
180
+ continue;
181
+ const kind = mapCorrectionKind(correction);
182
+ if (kindFilter && !kindFilter.has(kind))
183
+ continue;
184
+ fixes.push({
185
+ from: correction.start,
186
+ to: correction.end,
187
+ original: correction.original,
188
+ replacement: correction.replacement,
189
+ kind,
190
+ confidence: marginToConfidence(correction.margin),
191
+ scope: correctionScope(correction),
192
+ ...(correction.kind === "context" ? { byContext: true } : {}),
193
+ });
194
+ lastEnd = correction.end;
195
+ }
196
+ return fixes;
197
+ }
198
+ filterFlags(text, rawFlags, rawCorrections, range, kindFilter) {
199
+ if (rawFlags.length === 0)
200
+ return [];
201
+ if (kindFilter && !kindFilter.has("yazim"))
202
+ return [];
203
+ const codeRanges = findCodeRanges(text);
204
+ const flags = [];
205
+ let lastEnd = -1;
206
+ for (const flag of rawFlags) {
207
+ if (flag.start < lastEnd)
208
+ continue;
209
+ if (range) {
210
+ const [rangeFrom, rangeTo] = range;
211
+ if (!(flag.start < rangeTo && flag.end > rangeFrom))
212
+ continue;
213
+ }
214
+ if (codeRanges.length > 0 &&
215
+ codeRanges.some(([codeStart, codeEnd]) => flag.start < codeEnd && flag.end > codeStart)) {
216
+ continue;
217
+ }
218
+ if (this.ignored.has(normalizeIgnoredWord(flag.original)))
219
+ continue;
220
+ if (rawCorrections.some((c) => c.original !== c.replacement && flag.start < c.end && flag.end > c.start)) {
221
+ continue;
222
+ }
223
+ flags.push({
224
+ from: flag.start,
225
+ to: flag.end,
226
+ original: flag.original,
227
+ suggestions: flag.suggestions.slice(0, 5),
228
+ reason: flag.reason,
229
+ });
230
+ lastEnd = flag.end;
231
+ }
232
+ return flags;
233
+ }
234
+ enqueueAsync(task) {
235
+ const next = this.asyncQueue.then(task, task);
236
+ this.asyncQueue = next.catch(() => { });
237
+ return next;
238
+ }
239
+ async check(text, options = {}) {
240
+ this.assertActive();
241
+ if (!text)
242
+ return [];
243
+ const range = options.range;
244
+ if (range) {
245
+ const [rangeFrom, rangeTo] = range;
246
+ if (rangeFrom >= rangeTo)
247
+ return [];
248
+ }
249
+ const kindFilter = options.kinds ? new Set(options.kinds) : null;
250
+ if (kindFilter && kindFilter.size === 0)
251
+ return [];
252
+ const session = this.contextSession;
253
+ const settings = settingsFor(options.mode ?? this.mode, kindFilter);
254
+ if (!session) {
255
+ this.corrector.settings = settings;
256
+ let corrections;
257
+ try {
258
+ corrections = this.corrector.correct(text, { autoMarked: options.autoMarked }).corrections;
259
+ }
260
+ finally {
261
+ this.corrector.settings = SETTINGS_BY_MODE[this.mode];
262
+ }
263
+ return this.filterCorrections(text, corrections, range, kindFilter);
264
+ }
265
+ return this.enqueueAsync(async () => {
266
+ this.assertActive();
267
+ const activeSession = this.contextSession;
268
+ this.corrector.settings = settings;
269
+ let corrections;
270
+ try {
271
+ corrections = activeSession
272
+ ? (await this.corrector.correctAsync(text, activeSession.model, {
273
+ autoMarked: options.autoMarked,
274
+ priorTypoOriginals: options.priorTypoOriginals?.map(({ from, to, original }) => ({
275
+ start: from,
276
+ end: to,
277
+ original,
278
+ })),
279
+ })).corrections
280
+ : this.corrector.correct(text, { autoMarked: options.autoMarked }).corrections;
281
+ }
282
+ finally {
283
+ this.corrector.settings = SETTINGS_BY_MODE[this.mode];
284
+ }
285
+ this.assertActive();
286
+ return this.filterCorrections(text, corrections, range, kindFilter);
287
+ });
288
+ }
289
+ async inspect(text, options = {}) {
290
+ this.assertActive();
291
+ if (!text)
292
+ return { fixes: [], flags: [] };
293
+ const range = options.range;
294
+ if (range) {
295
+ const [rangeFrom, rangeTo] = range;
296
+ if (rangeFrom >= rangeTo)
297
+ return { fixes: [], flags: [] };
298
+ }
299
+ const kindFilter = options.kinds ? new Set(options.kinds) : null;
300
+ if (kindFilter && kindFilter.size === 0)
301
+ return { fixes: [], flags: [] };
302
+ const session = this.contextSession;
303
+ const settings = settingsFor(options.mode ?? this.mode, kindFilter);
304
+ if (!session) {
305
+ this.corrector.settings = settings;
306
+ let result;
307
+ try {
308
+ result = this.corrector.inspect(text, { autoMarked: options.autoMarked });
309
+ }
310
+ finally {
311
+ this.corrector.settings = SETTINGS_BY_MODE[this.mode];
312
+ }
313
+ return {
314
+ fixes: this.filterCorrections(text, result.corrections, range, kindFilter),
315
+ flags: this.filterFlags(text, result.flags, result.corrections, range, kindFilter),
316
+ };
317
+ }
318
+ return this.enqueueAsync(async () => {
319
+ this.assertActive();
320
+ const activeSession = this.contextSession;
321
+ this.corrector.settings = settings;
322
+ let result;
323
+ try {
324
+ result = activeSession
325
+ ? await this.corrector.inspectAsync(text, activeSession.model, {
326
+ autoMarked: options.autoMarked,
327
+ priorTypoOriginals: options.priorTypoOriginals?.map(({ from, to, original }) => ({
328
+ start: from,
329
+ end: to,
330
+ original,
331
+ })),
332
+ })
333
+ : this.corrector.inspect(text, { autoMarked: options.autoMarked });
334
+ }
335
+ finally {
336
+ this.corrector.settings = SETTINGS_BY_MODE[this.mode];
337
+ }
338
+ this.assertActive();
339
+ return {
340
+ fixes: this.filterCorrections(text, result.corrections, range, kindFilter),
341
+ flags: this.filterFlags(text, result.flags, result.corrections, range, kindFilter),
342
+ };
343
+ });
344
+ }
345
+ async dispose() {
346
+ if (this.disposed)
347
+ return;
348
+ this.disposed = true;
349
+ this.ignored.clear();
350
+ const session = this.contextSession;
351
+ this.contextSession = null;
352
+ if (session) {
353
+ await this.asyncQueue.catch(() => { });
354
+ await session.dispose();
355
+ }
356
+ }
357
+ }
@@ -0,0 +1,30 @@
1
+ import { ContextModel, SEQUENCE, pack, type ContextQuery, type RunModel } from "../../src/context.ts";
2
+ import type { WordValidator } from "../../src/corrector.ts";
3
+ import { TurkishMorphology } from "../../src/morphology/index.ts";
4
+ import { createOnnxContextSession, wrapContextModel } from "./context.ts";
5
+ import { CONTEXT_MODEL_FILE, DATA_FILES, DEFAULT_BASE_URL, LEGACY_DATA_FILES, fetchRawData, isBinaryData, loadData, parseBinaryData, parseRawData, resolveBaseUrl } from "./data.ts";
6
+ import { BALANCED, CAREFUL, applyFixes, mapCorrectionKind, marginToConfidence } from "./engine.ts";
7
+ import { ALL_FIX_KINDS, DEFAULT_FIX_KINDS } from "./types.ts";
8
+ import type { Tdkai, BinaryData, CheckOptions, ContextModelFileName, ContextSession, ContextSessionLoader, DataFileName, Fix, FixKind, FixScope, Flag, FlagReason, InspectResult, LoadOptions, LoadProgress, Mode, ProgressCallback, RawData, WarningCallback } from "./types.ts";
9
+ export { ALL_FIX_KINDS, DEFAULT_FIX_KINDS, BALANCED, CAREFUL, CONTEXT_MODEL_FILE, ContextModel, DATA_FILES, DEFAULT_BASE_URL, LEGACY_DATA_FILES, SEQUENCE, TurkishMorphology, applyFixes, createOnnxContextSession, fetchRawData, isBinaryData, loadData, mapCorrectionKind, marginToConfidence, pack, parseBinaryData, parseRawData, resolveBaseUrl, wrapContextModel, };
10
+ export type { Tdkai, BinaryData, CheckOptions, ContextModelFileName, ContextQuery, ContextSession, ContextSessionLoader, DataFileName, Fix, FixKind, FixScope, Flag, FlagReason, InspectResult, LoadOptions, LoadProgress, Mode, ProgressCallback, RawData, RunModel, WarningCallback, WordValidator, };
11
+ export interface CreateOptions {
12
+ mode?: Mode;
13
+ minCount?: number;
14
+ minPair?: number;
15
+ ignored?: Iterable<string>;
16
+ /** İsteğe bağlı biçimbilim doğrulayıcısı (verilmezse `data.morphology` kullanılır). */
17
+ morphology?: WordValidator;
18
+ /** İsteğe bağlı bağlam modeli veya `RunModel` işlevi (örneğin testler veya özel çalışma zamanları için). */
19
+ contextModel?: ContextModel | RunModel;
20
+ }
21
+ /**
22
+ * Bellekteki ikili veya ham TSV sözlük verisinden eşzamanlı olarak bir `Tdkai` örneği oluşturur.
23
+ * Ağ isteği gerektirmeyen testler veya önceden yüklenmiş veriler için kullanışlıdır.
24
+ */
25
+ export declare function createTdkai(data: RawData | BinaryData, options?: CreateOptions): Tdkai;
26
+ /**
27
+ * Sözlük ve biçimbilim dosyalarını (ve `context: true` ise `context.onnx` bağlam modelini) indirip Türkçe otomatik düzelticiyi hazırlar.
28
+ * `worker: true` verildiğinde ve ortam desteklediğinde Web Worker içinde çalışır.
29
+ */
30
+ export declare function loadTdkai(options?: LoadOptions): Promise<Tdkai>;
@@ -0,0 +1,85 @@
1
+ import { ContextModel, SEQUENCE, pack } from "../../src/context.js";
2
+ import { KnownMisspellings } from "../../src/known.js";
3
+ import { Lexicon } from "../../src/lexicon.js";
4
+ import { TurkishMorphology } from "../../src/morphology/index.js";
5
+ import { createOnnxContextSession, wrapContextModel } from "./context.js";
6
+ import { CONTEXT_MODEL_FILE, DATA_FILES, DEFAULT_BASE_URL, LEGACY_DATA_FILES, fetchRawData, isBinaryData, loadData, parseBinaryData, parseRawData, resolveBaseUrl, } from "./data.js";
7
+ import { TdkaiEngine, BALANCED, CAREFUL, applyFixes, mapCorrectionKind, marginToConfidence } from "./engine.js";
8
+ import { ALL_FIX_KINDS, DEFAULT_FIX_KINDS } from "./types.js";
9
+ import { loadTdkaiInWorker } from "./workerClient.js";
10
+ export { ALL_FIX_KINDS, DEFAULT_FIX_KINDS, BALANCED, CAREFUL, CONTEXT_MODEL_FILE, ContextModel, DATA_FILES, DEFAULT_BASE_URL, LEGACY_DATA_FILES, SEQUENCE, TurkishMorphology, applyFixes, createOnnxContextSession, fetchRawData, isBinaryData, loadData, mapCorrectionKind, marginToConfidence, pack, parseBinaryData, parseRawData, resolveBaseUrl, wrapContextModel, };
11
+ function wrapEngine(engine) {
12
+ const autr = {
13
+ get hasContext() {
14
+ return engine.hasContext;
15
+ },
16
+ check: (text, options) => engine.check(text, options),
17
+ inspect: (text, options) => engine.inspect(text, options),
18
+ apply: (text, fixes) => applyFixes(text, fixes),
19
+ fix: async (text, options) => {
20
+ const fixes = await engine.check(text, options);
21
+ return applyFixes(text, fixes);
22
+ },
23
+ ignore: (word) => engine.ignore(word),
24
+ unignore: (word) => engine.unignore(word),
25
+ setMode: (mode) => engine.setMode(mode),
26
+ dispose: () => engine.dispose(),
27
+ };
28
+ return autr;
29
+ }
30
+ /**
31
+ * Bellekteki ikili veya ham TSV sözlük verisinden eşzamanlı olarak bir `Tdkai` örneği oluşturur.
32
+ * Ağ isteği gerektirmeyen testler veya önceden yüklenmiş veriler için kullanışlıdır.
33
+ */
34
+ export function createTdkai(data, options = {}) {
35
+ const lexicon = isBinaryData(data)
36
+ ? Lexicon.fromBinary(data.lexicon)
37
+ : Lexicon.fromTsv(data.unigrams, data.bigrams, options.minCount ?? 1, options.minPair ?? 1);
38
+ const known = KnownMisspellings.fromTsv(data.known);
39
+ let morphology = options.morphology;
40
+ if (!morphology && data.morphology) {
41
+ if (data.morphology instanceof Uint8Array) {
42
+ morphology = TurkishMorphology.fromBinary(data.morphology);
43
+ }
44
+ else if (typeof data.morphology === "string") {
45
+ morphology = TurkishMorphology.fromDictionaries([data.morphology]);
46
+ }
47
+ else {
48
+ morphology = TurkishMorphology.fromDictionaries(data.morphology);
49
+ }
50
+ }
51
+ const contextSession = options.contextModel ? wrapContextModel(options.contextModel) : null;
52
+ const engine = new TdkaiEngine({ lexicon, known, morphology, contextSession }, options.mode ?? "careful", options.ignored, contextSession);
53
+ return wrapEngine(engine);
54
+ }
55
+ /**
56
+ * Sözlük ve biçimbilim dosyalarını (ve `context: true` ise `context.onnx` bağlam modelini) indirip Türkçe otomatik düzelticiyi hazırlar.
57
+ * `worker: true` verildiğinde ve ortam desteklediğinde Web Worker içinde çalışır.
58
+ */
59
+ export async function loadTdkai(options = {}) {
60
+ if (options.worker && (options.createWorker || typeof Worker !== "undefined")) {
61
+ return loadTdkaiInWorker(options);
62
+ }
63
+ const loaded = await loadData({
64
+ baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
65
+ data: options.data,
66
+ context: options.context,
67
+ wasmPaths: options.wasmPaths,
68
+ contextModel: options.contextModel,
69
+ loadContextSession: options.loadContextSession,
70
+ fetch: options.fetch,
71
+ signal: options.signal,
72
+ onProgress: options.onProgress,
73
+ onWarning: (message) => {
74
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
75
+ console.warn(message);
76
+ }
77
+ options.onWarning?.(message);
78
+ },
79
+ minCount: options.minCount,
80
+ minPair: options.minPair,
81
+ yieldToMain: typeof document !== "undefined",
82
+ });
83
+ const engine = new TdkaiEngine(loaded, options.mode ?? "careful", options.ignored, loaded.contextSession);
84
+ return wrapEngine(engine);
85
+ }
@@ -0,0 +1,64 @@
1
+ import type { BinaryData, CheckOptions, Fix, Flag, LoadProgress, Mode, RawData } from "./types.ts";
2
+ export type WorkerRequest = {
3
+ type: "load";
4
+ id: number;
5
+ baseUrl?: string;
6
+ data?: RawData | BinaryData;
7
+ mode: Mode;
8
+ minCount?: number;
9
+ minPair?: number;
10
+ ignored?: string[];
11
+ context?: boolean;
12
+ wasmPaths?: string;
13
+ } | {
14
+ type: "check";
15
+ id: number;
16
+ text: string;
17
+ options?: CheckOptions;
18
+ } | {
19
+ type: "inspect";
20
+ id: number;
21
+ text: string;
22
+ options?: CheckOptions;
23
+ } | {
24
+ type: "ignore";
25
+ word: string;
26
+ } | {
27
+ type: "unignore";
28
+ word: string;
29
+ } | {
30
+ type: "setMode";
31
+ mode: Mode;
32
+ } | {
33
+ type: "dispose";
34
+ id: number;
35
+ };
36
+ export type WorkerResponse = {
37
+ type: "progress";
38
+ id: number;
39
+ progress: LoadProgress;
40
+ } | {
41
+ type: "warning";
42
+ id: number;
43
+ message: string;
44
+ } | {
45
+ type: "loaded";
46
+ id: number;
47
+ hasContext: boolean;
48
+ } | {
49
+ type: "result";
50
+ id: number;
51
+ fixes: Fix[];
52
+ } | {
53
+ type: "inspectResult";
54
+ id: number;
55
+ fixes: Fix[];
56
+ flags: Flag[];
57
+ } | {
58
+ type: "disposed";
59
+ id: number;
60
+ } | {
61
+ type: "error";
62
+ id: number;
63
+ message: string;
64
+ };
@@ -0,0 +1 @@
1
+ export {};