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.
- package/LICENSE +21 -0
- package/README.md +138 -2
- package/dist/pkg/src/context.d.ts +13 -0
- package/dist/pkg/src/context.js +80 -0
- package/dist/pkg/src/data.d.ts +50 -0
- package/dist/pkg/src/data.js +495 -0
- package/dist/pkg/src/engine.d.ts +39 -0
- package/dist/pkg/src/engine.js +357 -0
- package/dist/pkg/src/index.d.ts +30 -0
- package/dist/pkg/src/index.js +85 -0
- package/dist/pkg/src/protocol.d.ts +64 -0
- package/dist/pkg/src/protocol.js +1 -0
- package/dist/pkg/src/types.d.ts +184 -0
- package/dist/pkg/src/types.js +16 -0
- package/dist/pkg/src/worker.d.ts +11 -0
- package/dist/pkg/src/worker.js +111 -0
- package/dist/pkg/src/workerClient.d.ts +3 -0
- package/dist/pkg/src/workerClient.js +202 -0
- package/dist/src/context-vocab.d.ts +1 -0
- package/dist/src/context-vocab.js +2 -0
- package/dist/src/context.d.ts +92 -0
- package/dist/src/context.js +390 -0
- package/dist/src/corrector.d.ts +338 -0
- package/dist/src/corrector.js +2684 -0
- package/dist/src/known.d.ts +88 -0
- package/dist/src/known.js +286 -0
- package/dist/src/lexicon-binary.d.ts +85 -0
- package/dist/src/lexicon-binary.js +805 -0
- package/dist/src/lexicon.d.ts +24 -0
- package/dist/src/lexicon.js +79 -0
- package/dist/src/morphology/analyzer.d.ts +30 -0
- package/dist/src/morphology/analyzer.js +311 -0
- package/dist/src/morphology/binary.d.ts +22 -0
- package/dist/src/morphology/binary.js +372 -0
- package/dist/src/morphology/dictionary.d.ts +21 -0
- package/dist/src/morphology/dictionary.js +559 -0
- package/dist/src/morphology/fuzzy.d.ts +44 -0
- package/dist/src/morphology/fuzzy.js +599 -0
- package/dist/src/morphology/index.d.ts +40 -0
- package/dist/src/morphology/index.js +81 -0
- package/dist/src/morphology/morphotactics.d.ts +148 -0
- package/dist/src/morphology/morphotactics.js +1871 -0
- package/dist/src/morphology/phonetics.d.ts +71 -0
- package/dist/src/morphology/phonetics.js +309 -0
- package/dist/src/morphology/prior.d.ts +61 -0
- package/dist/src/morphology/prior.js +457 -0
- package/dist/src/morphology/types.d.ts +110 -0
- package/dist/src/morphology/types.js +32 -0
- package/dist/src/rules/proper-names.d.ts +145 -0
- package/dist/src/rules/proper-names.js +1217 -0
- package/dist/src/rules/turkish-rules.d.ts +126 -0
- package/dist/src/rules/turkish-rules.js +365 -0
- package/dist/src/sentence.d.ts +83 -0
- package/dist/src/sentence.js +2408 -0
- package/dist/src/turkish.d.ts +20 -0
- package/dist/src/turkish.js +94 -0
- package/package.json +60 -4
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Principled morphological generative prior for unseen or rare valid Turkish word forms:
|
|
3
|
+
* P_hat(form) = max_{a in analyze(form)} C_lemma(a.stem, a.pos) * P(a.morphemes | a.pos)
|
|
4
|
+
*
|
|
5
|
+
* Uses exact corpus-derived suffix chain probabilities with a smoothed morpheme-bigram backoff
|
|
6
|
+
* for unseen/long suffix chains, and quantized per-lemma corpus counts embedded in AMR2 `morph.bin`.
|
|
7
|
+
*/
|
|
8
|
+
const PRIOR_MAGIC_0 = 0x4d; // 'M'
|
|
9
|
+
const PRIOR_MAGIC_1 = 0x53; // 'S'
|
|
10
|
+
const PRIOR_MAGIC_2 = 0x54; // 'T'
|
|
11
|
+
const PRIOR_MAGIC_3 = 0x31; // '1'
|
|
12
|
+
const PRIMARY_POS_LIST = [
|
|
13
|
+
"Noun",
|
|
14
|
+
"Adj",
|
|
15
|
+
"Verb",
|
|
16
|
+
"Pron",
|
|
17
|
+
"Adv",
|
|
18
|
+
"Conj",
|
|
19
|
+
"Ques",
|
|
20
|
+
"Postp",
|
|
21
|
+
"Det",
|
|
22
|
+
"Num",
|
|
23
|
+
"Dup",
|
|
24
|
+
"Interj",
|
|
25
|
+
"Punc",
|
|
26
|
+
];
|
|
27
|
+
const PRIMARY_POS_INDEX = new Map(PRIMARY_POS_LIST.map((p, i) => [p, i]));
|
|
28
|
+
const LEMMA_LOG_MAX = 17.0; // exp(17) ~ 24M
|
|
29
|
+
const PROB_NEG_LOG_MAX = 20.0; // exp(-20) ~ 2e-9
|
|
30
|
+
const UNSEEN_BIGRAM_PROB = 1e-4;
|
|
31
|
+
/** POS tags inside a morpheme sequence that mark a derivational POS boundary. */
|
|
32
|
+
const POS_BOUNDARY_MORPHEMES = new Set(["Noun", "Adj", "Verb", "Adv", "Pron"]);
|
|
33
|
+
function quantizeLemmaCount(count) {
|
|
34
|
+
if (count <= 0)
|
|
35
|
+
return 0;
|
|
36
|
+
const lg = Math.log(count + 1);
|
|
37
|
+
return Math.max(1, Math.min(255, Math.round((lg / LEMMA_LOG_MAX) * 255)));
|
|
38
|
+
}
|
|
39
|
+
function dequantizeLemmaCount(q) {
|
|
40
|
+
if (q === 0)
|
|
41
|
+
return 0;
|
|
42
|
+
return Math.exp((q * LEMMA_LOG_MAX) / 255) - 1;
|
|
43
|
+
}
|
|
44
|
+
function quantizeProb(prob) {
|
|
45
|
+
if (prob <= 0)
|
|
46
|
+
return 255;
|
|
47
|
+
const nlp = -Math.log(prob);
|
|
48
|
+
if (nlp <= 0)
|
|
49
|
+
return 0;
|
|
50
|
+
return Math.max(0, Math.min(255, Math.round((nlp / PROB_NEG_LOG_MAX) * 255)));
|
|
51
|
+
}
|
|
52
|
+
function dequantizeProb(q) {
|
|
53
|
+
return Math.exp((-q * PROB_NEG_LOG_MAX) / 255);
|
|
54
|
+
}
|
|
55
|
+
function derivationalDepth(morphemes) {
|
|
56
|
+
let depth = 0;
|
|
57
|
+
for (let i = 0; i < morphemes.length; i++) {
|
|
58
|
+
if (POS_BOUNDARY_MORPHEMES.has(morphemes[i]))
|
|
59
|
+
depth++;
|
|
60
|
+
}
|
|
61
|
+
return depth;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Runtime morphological generative prior: estimates the expected corpus frequency of any
|
|
65
|
+
* morphologically valid Turkish word from its lemma frequency and suffix-chain probability.
|
|
66
|
+
*/
|
|
67
|
+
export class MorphPrior {
|
|
68
|
+
lemmaCounts;
|
|
69
|
+
stemTotals;
|
|
70
|
+
chainProbs;
|
|
71
|
+
bigramProbs;
|
|
72
|
+
constructor(data) {
|
|
73
|
+
this.lemmaCounts = new Map(data.lemmaCounts);
|
|
74
|
+
this.chainProbs = new Map(data.chainProbs);
|
|
75
|
+
this.bigramProbs = new Map(data.bigramProbs);
|
|
76
|
+
this.stemTotals = new Map();
|
|
77
|
+
for (const [key, val] of this.lemmaCounts) {
|
|
78
|
+
const bar = key.indexOf("|");
|
|
79
|
+
if (bar === -1)
|
|
80
|
+
continue;
|
|
81
|
+
const stem = key.slice(0, bar);
|
|
82
|
+
this.stemTotals.set(stem, (this.stemTotals.get(stem) ?? 0) + val);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Total corpus frequency of `(stem, pos)` with a conservative cross-POS fallback for homographs. */
|
|
86
|
+
lemmaCount(stem, pos) {
|
|
87
|
+
const exact = this.lemmaCounts.get(`${stem}|${pos}`);
|
|
88
|
+
if (exact !== undefined && exact > 0)
|
|
89
|
+
return exact;
|
|
90
|
+
const total = this.stemTotals.get(stem);
|
|
91
|
+
return total !== undefined ? total * 0.2 : 0;
|
|
92
|
+
}
|
|
93
|
+
/** Corpus frequency of `stem` across all parts of speech (for prioritizing fuzzy stem search). */
|
|
94
|
+
stemTotal(stem) {
|
|
95
|
+
return this.stemTotals.get(stem) ?? 0;
|
|
96
|
+
}
|
|
97
|
+
/** Probability `P(morphemes | pos)` using exact stored chain probability or morpheme-bigram backoff. */
|
|
98
|
+
chainProb(pos, morphemes) {
|
|
99
|
+
const key = `${pos}|${morphemes.join("+")}`;
|
|
100
|
+
const exact = this.chainProbs.get(key);
|
|
101
|
+
if (exact !== undefined)
|
|
102
|
+
return exact;
|
|
103
|
+
// Smoothed morpheme-bigram backoff for unseen/long suffix chains.
|
|
104
|
+
let prob = 1.0;
|
|
105
|
+
let prev = `^${pos}`;
|
|
106
|
+
for (let i = 0; i < morphemes.length; i++) {
|
|
107
|
+
const cur = morphemes[i];
|
|
108
|
+
prob *= this.bigramProbs.get(`${prev}>${cur}`) ?? UNSEEN_BIGRAM_PROB;
|
|
109
|
+
prev = cur;
|
|
110
|
+
}
|
|
111
|
+
prob *= this.bigramProbs.get(`${prev}>$`) ?? UNSEEN_BIGRAM_PROB;
|
|
112
|
+
// Slight damping on backed-off chains so attested chains are preferred when tied.
|
|
113
|
+
return prob * 0.5;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Expected corpus count of a valid word form across its morphological analyses.
|
|
117
|
+
* Returns 0 if `analyses` is empty.
|
|
118
|
+
*/
|
|
119
|
+
expectedCount(analyses) {
|
|
120
|
+
if (analyses.length === 0)
|
|
121
|
+
return 0;
|
|
122
|
+
let best = 0;
|
|
123
|
+
for (let i = 0; i < analyses.length; i++) {
|
|
124
|
+
const a = analyses[i];
|
|
125
|
+
const lc = this.lemmaCount(a.stem, a.pos);
|
|
126
|
+
if (lc <= 0)
|
|
127
|
+
continue;
|
|
128
|
+
const est = lc * this.chainProb(a.pos, a.morphemes);
|
|
129
|
+
if (est > best)
|
|
130
|
+
best = est;
|
|
131
|
+
}
|
|
132
|
+
return best;
|
|
133
|
+
}
|
|
134
|
+
get chainSize() {
|
|
135
|
+
return this.chainProbs.size;
|
|
136
|
+
}
|
|
137
|
+
get bigramSize() {
|
|
138
|
+
return this.bigramProbs.size;
|
|
139
|
+
}
|
|
140
|
+
get lemmaSize() {
|
|
141
|
+
return this.lemmaCounts.size;
|
|
142
|
+
}
|
|
143
|
+
/** Raw maps for binary serialization. */
|
|
144
|
+
toData() {
|
|
145
|
+
return {
|
|
146
|
+
lemmaCounts: this.lemmaCounts,
|
|
147
|
+
chainProbs: this.chainProbs,
|
|
148
|
+
bigramProbs: this.bigramProbs,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function collectCorpusStats(analyzer, tsv, hasCapColumn, minCount) {
|
|
153
|
+
const posTotal = new Map();
|
|
154
|
+
const lemmaCount = new Map();
|
|
155
|
+
const chainCount = new Map();
|
|
156
|
+
const chainTypes = new Map();
|
|
157
|
+
const bigramCount = new Map();
|
|
158
|
+
const bigramCtx = new Map();
|
|
159
|
+
for (const line of tsv.split("\n")) {
|
|
160
|
+
if (!line)
|
|
161
|
+
continue;
|
|
162
|
+
const parts = line.split("\t");
|
|
163
|
+
const form = parts[0];
|
|
164
|
+
if (!form || form.includes("'"))
|
|
165
|
+
continue;
|
|
166
|
+
const count = Number(parts[1]);
|
|
167
|
+
if (!Number.isFinite(count) || count < minCount)
|
|
168
|
+
continue;
|
|
169
|
+
if (hasCapColumn) {
|
|
170
|
+
const caps = Number(parts[2] ?? "0");
|
|
171
|
+
if (caps / count >= 0.7)
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const analyses = analyzer.analyze(form);
|
|
175
|
+
if (analyses.length === 0)
|
|
176
|
+
continue;
|
|
177
|
+
// Prefer analyses with minimum derivational depth so bare Adj ("eksik" []) and bare Noun ("eksik" ["A3sg"])
|
|
178
|
+
// share weight fairly while deep spurious derivations are discounted.
|
|
179
|
+
let minDeriv = Infinity;
|
|
180
|
+
for (let i = 0; i < analyses.length; i++) {
|
|
181
|
+
const d = derivationalDepth(analyses[i].morphemes);
|
|
182
|
+
if (d < minDeriv)
|
|
183
|
+
minDeriv = d;
|
|
184
|
+
}
|
|
185
|
+
const best = analyses.filter((a) => derivationalDepth(a.morphemes) === minDeriv);
|
|
186
|
+
const w = count / best.length;
|
|
187
|
+
for (let i = 0; i < best.length; i++) {
|
|
188
|
+
const a = best[i];
|
|
189
|
+
posTotal.set(a.pos, (posTotal.get(a.pos) ?? 0) + w);
|
|
190
|
+
const lk = `${a.stem}|${a.pos}`;
|
|
191
|
+
lemmaCount.set(lk, (lemmaCount.get(lk) ?? 0) + w);
|
|
192
|
+
const ck = `${a.pos}|${a.morphemes.join("+")}`;
|
|
193
|
+
chainCount.set(ck, (chainCount.get(ck) ?? 0) + w);
|
|
194
|
+
chainTypes.set(ck, (chainTypes.get(ck) ?? 0) + 1);
|
|
195
|
+
let prev = `^${a.pos}`;
|
|
196
|
+
for (let m = 0; m < a.morphemes.length; m++) {
|
|
197
|
+
const cur = a.morphemes[m];
|
|
198
|
+
const bk = `${prev}>${cur}`;
|
|
199
|
+
bigramCount.set(bk, (bigramCount.get(bk) ?? 0) + w);
|
|
200
|
+
bigramCtx.set(prev, (bigramCtx.get(prev) ?? 0) + w);
|
|
201
|
+
prev = cur;
|
|
202
|
+
}
|
|
203
|
+
const endKey = `${prev}>$`;
|
|
204
|
+
bigramCount.set(endKey, (bigramCount.get(endKey) ?? 0) + w);
|
|
205
|
+
bigramCtx.set(prev, (bigramCtx.get(prev) ?? 0) + w);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return { posTotal, lemmaCount, chainCount, chainTypes, bigramCount, bigramCtx };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Extracts lemma frequencies and register-balanced suffix-chain + morpheme-bigram probabilities
|
|
212
|
+
* from `unigrams_conv.tsv` and (optionally) `subtitle_counts.tsv`.
|
|
213
|
+
*/
|
|
214
|
+
export function buildMorphPriorFromCorpora(analyzer, unigramsConvTsv, subtitleTsv, options = {}) {
|
|
215
|
+
const minChainTypes = options.minChainTypes ?? 3;
|
|
216
|
+
const minChainCount = options.minChainCount ?? 40;
|
|
217
|
+
const subWeight = subtitleTsv ? (options.subtitleWeight ?? 0.45) : 0;
|
|
218
|
+
const convWeight = 1 - subWeight;
|
|
219
|
+
const conv = collectCorpusStats(analyzer, unigramsConvTsv, true, 2);
|
|
220
|
+
const sub = subtitleTsv ? collectCorpusStats(analyzer, subtitleTsv, false, 2) : null;
|
|
221
|
+
// Lemma counts come from the combined lexicon corpus (unigrams_conv.tsv), matching exactCount() scale.
|
|
222
|
+
const lemmaCounts = new Map(conv.lemmaCount);
|
|
223
|
+
if (sub) {
|
|
224
|
+
// Include spoken lemmas missing or very rare in Wikipedia at a conservative scale.
|
|
225
|
+
for (const [lk, sc] of sub.lemmaCount) {
|
|
226
|
+
const existing = lemmaCounts.get(lk) ?? 0;
|
|
227
|
+
if (sc > existing) {
|
|
228
|
+
lemmaCounts.set(lk, Math.max(existing, sc * 0.5));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// Register-balanced suffix-chain probabilities P(chain | pos).
|
|
233
|
+
const allChains = new Set(conv.chainCount.keys());
|
|
234
|
+
if (sub) {
|
|
235
|
+
for (const k of sub.chainCount.keys())
|
|
236
|
+
allChains.add(k);
|
|
237
|
+
}
|
|
238
|
+
const chainProbs = new Map();
|
|
239
|
+
for (const ck of allChains) {
|
|
240
|
+
const convTypes = conv.chainTypes.get(ck) ?? 0;
|
|
241
|
+
const subTypes = sub?.chainTypes.get(ck) ?? 0;
|
|
242
|
+
const convC = conv.chainCount.get(ck) ?? 0;
|
|
243
|
+
const subC = sub?.chainCount.get(ck) ?? 0;
|
|
244
|
+
if (convTypes + subTypes < minChainTypes || convC + subC < minChainCount)
|
|
245
|
+
continue;
|
|
246
|
+
const pos = ck.slice(0, ck.indexOf("|"));
|
|
247
|
+
const pConv = convC / (conv.posTotal.get(pos) ?? 1);
|
|
248
|
+
const pSub = sub ? subC / (sub.posTotal.get(pos) ?? 1) : pConv;
|
|
249
|
+
const p = convWeight * pConv + subWeight * pSub;
|
|
250
|
+
if (p > 0)
|
|
251
|
+
chainProbs.set(ck, p);
|
|
252
|
+
}
|
|
253
|
+
// Register-balanced morpheme-bigram backoff probabilities P(next | prev).
|
|
254
|
+
const allBigrams = new Set(conv.bigramCount.keys());
|
|
255
|
+
if (sub) {
|
|
256
|
+
for (const k of sub.bigramCount.keys())
|
|
257
|
+
allBigrams.add(k);
|
|
258
|
+
}
|
|
259
|
+
const bigramProbs = new Map();
|
|
260
|
+
for (const bk of allBigrams) {
|
|
261
|
+
const gt = bk.indexOf(">");
|
|
262
|
+
const prev = bk.slice(0, gt);
|
|
263
|
+
const cConv = conv.bigramCount.get(bk) ?? 0;
|
|
264
|
+
const cSub = sub?.bigramCount.get(bk) ?? 0;
|
|
265
|
+
if (cConv + cSub < 5)
|
|
266
|
+
continue;
|
|
267
|
+
const pConv = (cConv + 0.5) / ((conv.bigramCtx.get(prev) ?? 0) + 50);
|
|
268
|
+
const pSub = sub ? (cSub + 0.5) / ((sub.bigramCtx.get(prev) ?? 0) + 50) : pConv;
|
|
269
|
+
const p = convWeight * pConv + subWeight * pSub;
|
|
270
|
+
if (p > 0)
|
|
271
|
+
bigramProbs.set(bk, p);
|
|
272
|
+
}
|
|
273
|
+
return new MorphPrior({ lemmaCounts, chainProbs, bigramProbs });
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Serializes a `MorphPrior` into a compact `MST1` binary block aligned with sorted `DictionaryItem[]`.
|
|
277
|
+
*/
|
|
278
|
+
export function encodeMorphPrior(prior, sortedItems) {
|
|
279
|
+
const data = prior.toData();
|
|
280
|
+
const encoder = new TextEncoder();
|
|
281
|
+
// 1. Per-item quantized lemma count (aligned to sortedItems index).
|
|
282
|
+
const itemCount = sortedItems.length;
|
|
283
|
+
const lemmaCol = new Uint8Array(itemCount);
|
|
284
|
+
for (let i = 0; i < itemCount; i++) {
|
|
285
|
+
const item = sortedItems[i];
|
|
286
|
+
const count = data.lemmaCounts.get(`${item.root}|${item.primaryPos}`) ?? 0;
|
|
287
|
+
lemmaCol[i] = quantizeLemmaCount(count);
|
|
288
|
+
}
|
|
289
|
+
// 2. Build shared symbol table for morpheme IDs, ^POS start tokens, and $ end token.
|
|
290
|
+
const symToId = new Map();
|
|
291
|
+
const symList = [];
|
|
292
|
+
const intern = (sym) => {
|
|
293
|
+
let id = symToId.get(sym);
|
|
294
|
+
if (id === undefined) {
|
|
295
|
+
id = symList.length;
|
|
296
|
+
if (id > 255)
|
|
297
|
+
throw new Error(`morph prior: symbol table overflow (${id} > 255)`);
|
|
298
|
+
symToId.set(sym, id);
|
|
299
|
+
symList.push(sym);
|
|
300
|
+
}
|
|
301
|
+
return id;
|
|
302
|
+
};
|
|
303
|
+
// Pre-sort chains deterministically for better compression.
|
|
304
|
+
const chainEntries = [];
|
|
305
|
+
for (const [key, prob] of [...data.chainProbs.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
306
|
+
const bar = key.indexOf("|");
|
|
307
|
+
const pos = key.slice(0, bar);
|
|
308
|
+
const posIdx = PRIMARY_POS_INDEX.get(pos);
|
|
309
|
+
if (posIdx === undefined)
|
|
310
|
+
continue;
|
|
311
|
+
const rest = key.slice(bar + 1);
|
|
312
|
+
const morphs = rest.length === 0 ? [] : rest.split("+");
|
|
313
|
+
if (morphs.length > 255)
|
|
314
|
+
continue;
|
|
315
|
+
const mIds = morphs.map(intern);
|
|
316
|
+
chainEntries.push({ posIdx, mIds, qProb: quantizeProb(prob) });
|
|
317
|
+
}
|
|
318
|
+
const bigramEntries = [];
|
|
319
|
+
for (const [key, prob] of [...data.bigramProbs.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
320
|
+
const gt = key.indexOf(">");
|
|
321
|
+
const prev = key.slice(0, gt);
|
|
322
|
+
const next = key.slice(gt + 1);
|
|
323
|
+
bigramEntries.push({ prevId: intern(prev), nextId: intern(next), qProb: quantizeProb(prob) });
|
|
324
|
+
}
|
|
325
|
+
// Encode symbol table.
|
|
326
|
+
const symBytesArr = [];
|
|
327
|
+
for (const sym of symList) {
|
|
328
|
+
const enc = encoder.encode(sym);
|
|
329
|
+
symBytesArr.push(enc.byteLength);
|
|
330
|
+
for (let j = 0; j < enc.byteLength; j++)
|
|
331
|
+
symBytesArr.push(enc[j]);
|
|
332
|
+
}
|
|
333
|
+
const symBytes = new Uint8Array(symBytesArr);
|
|
334
|
+
// Encode chains.
|
|
335
|
+
const chainBytesArr = [];
|
|
336
|
+
for (const ch of chainEntries) {
|
|
337
|
+
chainBytesArr.push(ch.posIdx, ch.mIds.length);
|
|
338
|
+
for (let j = 0; j < ch.mIds.length; j++)
|
|
339
|
+
chainBytesArr.push(ch.mIds[j]);
|
|
340
|
+
chainBytesArr.push(ch.qProb);
|
|
341
|
+
}
|
|
342
|
+
const chainBytes = new Uint8Array(chainBytesArr);
|
|
343
|
+
// Encode bigrams.
|
|
344
|
+
const bigramBytes = new Uint8Array(bigramEntries.length * 3);
|
|
345
|
+
for (let i = 0; i < bigramEntries.length; i++) {
|
|
346
|
+
const bg = bigramEntries[i];
|
|
347
|
+
bigramBytes[i * 3] = bg.prevId;
|
|
348
|
+
bigramBytes[i * 3 + 1] = bg.nextId;
|
|
349
|
+
bigramBytes[i * 3 + 2] = bg.qProb;
|
|
350
|
+
}
|
|
351
|
+
// Layout: 4 magic ('MST1') + 2 symCount + 4 symByteLen + 4 chainCount + 4 chainByteLen + 4 bigramCount
|
|
352
|
+
// + lemmaCol (itemCount) + symBytes + chainBytes + bigramBytes
|
|
353
|
+
const headerLen = 22;
|
|
354
|
+
const totalLen = headerLen + lemmaCol.byteLength + symBytes.byteLength + chainBytes.byteLength + bigramBytes.byteLength;
|
|
355
|
+
const out = new Uint8Array(totalLen);
|
|
356
|
+
const view = new DataView(out.buffer);
|
|
357
|
+
let off = 0;
|
|
358
|
+
out[off++] = PRIOR_MAGIC_0;
|
|
359
|
+
out[off++] = PRIOR_MAGIC_1;
|
|
360
|
+
out[off++] = PRIOR_MAGIC_2;
|
|
361
|
+
out[off++] = PRIOR_MAGIC_3;
|
|
362
|
+
view.setUint16(off, symList.length, true);
|
|
363
|
+
off += 2;
|
|
364
|
+
view.setUint32(off, symBytes.byteLength, true);
|
|
365
|
+
off += 4;
|
|
366
|
+
view.setUint32(off, chainEntries.length, true);
|
|
367
|
+
off += 4;
|
|
368
|
+
view.setUint32(off, chainBytes.byteLength, true);
|
|
369
|
+
off += 4;
|
|
370
|
+
view.setUint32(off, bigramEntries.length, true);
|
|
371
|
+
off += 4;
|
|
372
|
+
out.set(lemmaCol, off);
|
|
373
|
+
off += lemmaCol.byteLength;
|
|
374
|
+
out.set(symBytes, off);
|
|
375
|
+
off += symBytes.byteLength;
|
|
376
|
+
out.set(chainBytes, off);
|
|
377
|
+
off += chainBytes.byteLength;
|
|
378
|
+
out.set(bigramBytes, off);
|
|
379
|
+
return out;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Decodes an `MST1` binary block starting at `startOffset` in `bytes`.
|
|
383
|
+
*/
|
|
384
|
+
export function decodeMorphPrior(bytes, startOffset, sortedItems) {
|
|
385
|
+
if (bytes.byteLength - startOffset < 22 ||
|
|
386
|
+
bytes[startOffset] !== PRIOR_MAGIC_0 ||
|
|
387
|
+
bytes[startOffset + 1] !== PRIOR_MAGIC_1 ||
|
|
388
|
+
bytes[startOffset + 2] !== PRIOR_MAGIC_2 ||
|
|
389
|
+
bytes[startOffset + 3] !== PRIOR_MAGIC_3) {
|
|
390
|
+
throw new Error("autr: Geçersiz biçimbilim öncül bloğu (MST1 bekleniyordu).");
|
|
391
|
+
}
|
|
392
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
393
|
+
let off = startOffset + 4;
|
|
394
|
+
const symCount = view.getUint16(off, true);
|
|
395
|
+
off += 2;
|
|
396
|
+
const symByteLen = view.getUint32(off, true);
|
|
397
|
+
off += 4;
|
|
398
|
+
const chainCount = view.getUint32(off, true);
|
|
399
|
+
off += 4;
|
|
400
|
+
const chainByteLen = view.getUint32(off, true);
|
|
401
|
+
off += 4;
|
|
402
|
+
const bigramCount = view.getUint32(off, true);
|
|
403
|
+
off += 4;
|
|
404
|
+
const itemCount = sortedItems.length;
|
|
405
|
+
const lemmaCol = bytes.subarray(off, off + itemCount);
|
|
406
|
+
off += itemCount;
|
|
407
|
+
const decoder = new TextDecoder("utf-8");
|
|
408
|
+
const symList = new Array(symCount);
|
|
409
|
+
let symPos = off;
|
|
410
|
+
for (let i = 0; i < symCount; i++) {
|
|
411
|
+
const len = bytes[symPos++];
|
|
412
|
+
symList[i] = decoder.decode(bytes.subarray(symPos, symPos + len));
|
|
413
|
+
symPos += len;
|
|
414
|
+
}
|
|
415
|
+
off += symByteLen;
|
|
416
|
+
const chainEnd = off + chainByteLen;
|
|
417
|
+
const chainProbs = new Map();
|
|
418
|
+
let cPos = off;
|
|
419
|
+
for (let i = 0; i < chainCount && cPos < chainEnd; i++) {
|
|
420
|
+
const posIdx = bytes[cPos++];
|
|
421
|
+
const len = bytes[cPos++];
|
|
422
|
+
const pos = PRIMARY_POS_LIST[posIdx];
|
|
423
|
+
if (len === 0) {
|
|
424
|
+
const qProb = bytes[cPos++];
|
|
425
|
+
chainProbs.set(`${pos}|`, dequantizeProb(qProb));
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
const parts = new Array(len);
|
|
429
|
+
for (let j = 0; j < len; j++)
|
|
430
|
+
parts[j] = symList[bytes[cPos++]];
|
|
431
|
+
const qProb = bytes[cPos++];
|
|
432
|
+
chainProbs.set(`${pos}|${parts.join("+")}`, dequantizeProb(qProb));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
off = chainEnd;
|
|
436
|
+
const bigramProbs = new Map();
|
|
437
|
+
for (let i = 0; i < bigramCount; i++) {
|
|
438
|
+
const prev = symList[bytes[off++]];
|
|
439
|
+
const next = symList[bytes[off++]];
|
|
440
|
+
const qProb = bytes[off++];
|
|
441
|
+
bigramProbs.set(`${prev}>${next}`, dequantizeProb(qProb));
|
|
442
|
+
}
|
|
443
|
+
const lemmaCounts = new Map();
|
|
444
|
+
for (let i = 0; i < itemCount; i++) {
|
|
445
|
+
const q = lemmaCol[i];
|
|
446
|
+
if (q === 0)
|
|
447
|
+
continue;
|
|
448
|
+
const item = sortedItems[i];
|
|
449
|
+
const key = `${item.root}|${item.primaryPos}`;
|
|
450
|
+
const val = dequantizeLemmaCount(q);
|
|
451
|
+
const existing = lemmaCounts.get(key);
|
|
452
|
+
if (existing === undefined || val > existing) {
|
|
453
|
+
lemmaCounts.set(key, val);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return new MorphPrior({ lemmaCounts, chainProbs, bigramProbs });
|
|
457
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types and bitmask constants for the Turkish morphological analyzer.
|
|
3
|
+
* Adapted from Zemberek's core/morphotactics data model (Apache 2.0).
|
|
4
|
+
*/
|
|
5
|
+
export type PrimaryPos = "Noun" | "Adj" | "Verb" | "Pron" | "Adv" | "Conj" | "Ques" | "Postp" | "Det" | "Num" | "Dup" | "Interj" | "Punc";
|
|
6
|
+
export type SecondaryPos = "None" | "ProperNoun" | "Abbreviation" | "Time" | "PersonalPron" | "DemonstrativePron" | "QuantitivePron" | "QuestionPron" | "ReflexivePron" | "Card" | "Ord" | "Dist" | "PCDat" | "PCAbl" | "PCNom" | "PCGen" | "PCIns" | "PCAcc";
|
|
7
|
+
export declare const RA_NONE = 0;
|
|
8
|
+
export declare const RA_VOICING: number;
|
|
9
|
+
export declare const RA_NO_VOICING: number;
|
|
10
|
+
export declare const RA_VOICING_OPT: number;
|
|
11
|
+
export declare const RA_INVERSE_HARMONY: number;
|
|
12
|
+
export declare const RA_DOUBLING: number;
|
|
13
|
+
export declare const RA_LAST_VOWEL_DROP: number;
|
|
14
|
+
export declare const RA_COMPOUND_P3SG: number;
|
|
15
|
+
export declare const RA_COMPOUND_P3SG_ROOT: number;
|
|
16
|
+
export declare const RA_NO_SUFFIX: number;
|
|
17
|
+
export declare const RA_PROGRESSIVE_VOWEL_DROP: number;
|
|
18
|
+
export declare const RA_AORIST_I: number;
|
|
19
|
+
export declare const RA_AORIST_A: number;
|
|
20
|
+
export declare const RA_PASSIVE_IN: number;
|
|
21
|
+
export declare const RA_CAUSATIVE_T: number;
|
|
22
|
+
export declare const RA_IMPLICIT_PLURAL: number;
|
|
23
|
+
export declare const RA_IMPLICIT_DATIVE: number;
|
|
24
|
+
export declare const RA_IMPLICIT_P1SG: number;
|
|
25
|
+
export declare const RA_IMPLICIT_P2SG: number;
|
|
26
|
+
export declare const RA_FAMILY_MEMBER: number;
|
|
27
|
+
export declare const RA_RECIPROCAL: number;
|
|
28
|
+
export declare const RA_NON_RECIPROCAL: number;
|
|
29
|
+
export declare const RA_REFLEXIVE: number;
|
|
30
|
+
export declare const RA_DUMMY: number;
|
|
31
|
+
export declare const RA_NOUN_CONS_INSERT_N: number;
|
|
32
|
+
export declare const RA_EXT: number;
|
|
33
|
+
export declare const RA_INFORMAL: number;
|
|
34
|
+
export interface DictionaryItem {
|
|
35
|
+
/** Lookup key without numeric homonym index, e.g. "ben_Pron_Pers", "dün_Noun_Time", "değil_Verb". */
|
|
36
|
+
readonly key: string;
|
|
37
|
+
/** Lowercased lemma preserving circumflexes (for circumflex verification). */
|
|
38
|
+
readonly lemma: string;
|
|
39
|
+
/** Normalized root surface without circumflexes and without -mak/-mek for verbs. */
|
|
40
|
+
readonly root: string;
|
|
41
|
+
/** Normalized pronunciation string (defaults to root). */
|
|
42
|
+
readonly pronunciation: string;
|
|
43
|
+
readonly primaryPos: PrimaryPos;
|
|
44
|
+
readonly secondaryPos: SecondaryPos;
|
|
45
|
+
readonly attributes: number;
|
|
46
|
+
readonly index: number;
|
|
47
|
+
/** Reference item for Dummy compound roots (e.g. "zeytinyağ" -> "zeytinyağı") or variants. */
|
|
48
|
+
referenceItem: DictionaryItem | null;
|
|
49
|
+
}
|
|
50
|
+
export interface Morpheme {
|
|
51
|
+
readonly id: string;
|
|
52
|
+
readonly derivational: boolean;
|
|
53
|
+
readonly pos: PrimaryPos | null;
|
|
54
|
+
}
|
|
55
|
+
export interface SuffixTemplateToken {
|
|
56
|
+
readonly type: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
57
|
+
readonly letter: string;
|
|
58
|
+
}
|
|
59
|
+
export interface PathNode {
|
|
60
|
+
readonly prev: PathNode | null;
|
|
61
|
+
readonly stemTransition: StemTransition;
|
|
62
|
+
readonly state: MorphemeState;
|
|
63
|
+
readonly surface: string;
|
|
64
|
+
/** Current offset in the normalized search input. */
|
|
65
|
+
readonly pos: number;
|
|
66
|
+
/** Total length of the search input (for O(1) HAS_TAIL / HAS_NO_TAIL checks). */
|
|
67
|
+
readonly inputLength: number;
|
|
68
|
+
/** Current PhoneticAttribute bitmask. */
|
|
69
|
+
readonly phonetic: number;
|
|
70
|
+
readonly depth: number;
|
|
71
|
+
readonly hasSurface: boolean;
|
|
72
|
+
readonly hasDerivation: boolean;
|
|
73
|
+
readonly currentGroupHasSurface: boolean;
|
|
74
|
+
readonly lastDerivation: MorphemeState | null;
|
|
75
|
+
}
|
|
76
|
+
export type Condition = (path: PathNode) => boolean;
|
|
77
|
+
export interface SuffixTransition {
|
|
78
|
+
readonly from: MorphemeState;
|
|
79
|
+
readonly to: MorphemeState;
|
|
80
|
+
readonly template: string;
|
|
81
|
+
readonly tokens: readonly SuffixTemplateToken[];
|
|
82
|
+
readonly lastToken: SuffixTemplateToken | undefined;
|
|
83
|
+
readonly hasSurface: boolean;
|
|
84
|
+
readonly condition: Condition | null;
|
|
85
|
+
/** Pre-computed expectation mask: PA_EXPECTS_VOWEL or PA_EXPECTS_CONSONANT that must NOT be present. */
|
|
86
|
+
readonly forbiddenPhonetic: number;
|
|
87
|
+
/** 64-entry surface cache indexed by 6-bit phonetic harmony/voicing key (null = impossible). */
|
|
88
|
+
readonly surfaceCache: (string | null | undefined)[];
|
|
89
|
+
/** 64-entry next-phonetic-bitmask cache aligned with surfaceCache. */
|
|
90
|
+
readonly nextPhoneticCache: Int32Array;
|
|
91
|
+
}
|
|
92
|
+
export interface MorphemeState {
|
|
93
|
+
readonly id: string;
|
|
94
|
+
readonly morpheme: Morpheme;
|
|
95
|
+
readonly terminal: boolean;
|
|
96
|
+
readonly derivative: boolean;
|
|
97
|
+
readonly posRoot: boolean;
|
|
98
|
+
readonly outgoing: SuffixTransition[];
|
|
99
|
+
}
|
|
100
|
+
export interface StemTransition {
|
|
101
|
+
readonly surface: string;
|
|
102
|
+
readonly item: DictionaryItem;
|
|
103
|
+
readonly phonetic: number;
|
|
104
|
+
readonly to: MorphemeState;
|
|
105
|
+
}
|
|
106
|
+
export interface MorphAnalysis {
|
|
107
|
+
stem: string;
|
|
108
|
+
pos: string;
|
|
109
|
+
morphemes: string[];
|
|
110
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types and bitmask constants for the Turkish morphological analyzer.
|
|
3
|
+
* Adapted from Zemberek's core/morphotactics data model (Apache 2.0).
|
|
4
|
+
*/
|
|
5
|
+
// Root attribute bitmask flags (max 31 bits in signed 32-bit bitwise ops).
|
|
6
|
+
export const RA_NONE = 0;
|
|
7
|
+
export const RA_VOICING = 1 << 0;
|
|
8
|
+
export const RA_NO_VOICING = 1 << 1;
|
|
9
|
+
export const RA_VOICING_OPT = 1 << 2;
|
|
10
|
+
export const RA_INVERSE_HARMONY = 1 << 3;
|
|
11
|
+
export const RA_DOUBLING = 1 << 4;
|
|
12
|
+
export const RA_LAST_VOWEL_DROP = 1 << 5;
|
|
13
|
+
export const RA_COMPOUND_P3SG = 1 << 6;
|
|
14
|
+
export const RA_COMPOUND_P3SG_ROOT = 1 << 7;
|
|
15
|
+
export const RA_NO_SUFFIX = 1 << 8;
|
|
16
|
+
export const RA_PROGRESSIVE_VOWEL_DROP = 1 << 9;
|
|
17
|
+
export const RA_AORIST_I = 1 << 10;
|
|
18
|
+
export const RA_AORIST_A = 1 << 11;
|
|
19
|
+
export const RA_PASSIVE_IN = 1 << 12;
|
|
20
|
+
export const RA_CAUSATIVE_T = 1 << 13;
|
|
21
|
+
export const RA_IMPLICIT_PLURAL = 1 << 14;
|
|
22
|
+
export const RA_IMPLICIT_DATIVE = 1 << 15;
|
|
23
|
+
export const RA_IMPLICIT_P1SG = 1 << 16;
|
|
24
|
+
export const RA_IMPLICIT_P2SG = 1 << 17;
|
|
25
|
+
export const RA_FAMILY_MEMBER = 1 << 18;
|
|
26
|
+
export const RA_RECIPROCAL = 1 << 19;
|
|
27
|
+
export const RA_NON_RECIPROCAL = 1 << 20;
|
|
28
|
+
export const RA_REFLEXIVE = 1 << 21;
|
|
29
|
+
export const RA_DUMMY = 1 << 22;
|
|
30
|
+
export const RA_NOUN_CONS_INSERT_N = 1 << 23;
|
|
31
|
+
export const RA_EXT = 1 << 24;
|
|
32
|
+
export const RA_INFORMAL = 1 << 25;
|