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,24 @@
|
|
|
1
|
+
/** Word-form and word-pair counts from edited Turkish text. Forms are lowercased the Turkish way; "ankara'ya" keeps its apostrophe. */
|
|
2
|
+
export declare class Lexicon {
|
|
3
|
+
private readonly counts;
|
|
4
|
+
private readonly capitalized;
|
|
5
|
+
private readonly pairs;
|
|
6
|
+
private binary;
|
|
7
|
+
/** unigrams: "form\tcount\tcapitalizedMidSentence" lines. bigrams: "first second\tcount" lines. */
|
|
8
|
+
static fromTsv(unigrams: string, bigrams: string, minCount?: number, minPair?: number): Lexicon;
|
|
9
|
+
/** Loads a compact `ALX1` binary lexicon (`lexicon.bin`) directly into memory without parsing TSV strings. */
|
|
10
|
+
static fromBinary(bytes: Uint8Array): Lexicon;
|
|
11
|
+
count(form: string): number;
|
|
12
|
+
/** Exact-table count only (skips protection-only Bloom filters in binary lexicons for target candidate lookups). */
|
|
13
|
+
exactCount(form: string): number;
|
|
14
|
+
pair(first: string, second: string): number;
|
|
15
|
+
/** Share of occurrences written with a capital letter mid-sentence: near 1 for names like "ankara". */
|
|
16
|
+
capitalShare(form: string): number;
|
|
17
|
+
get size(): number;
|
|
18
|
+
get pairCount(): number;
|
|
19
|
+
/** The lexicon as the two TSV texts it was loaded from, for measuring what a pruned copy would weigh. */
|
|
20
|
+
toTsv(): {
|
|
21
|
+
unigrams: string;
|
|
22
|
+
bigrams: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { BinaryLexicon } from "./lexicon-binary.js";
|
|
2
|
+
/** Word-form and word-pair counts from edited Turkish text. Forms are lowercased the Turkish way; "ankara'ya" keeps its apostrophe. */
|
|
3
|
+
export class Lexicon {
|
|
4
|
+
counts = new Map();
|
|
5
|
+
capitalized = new Map();
|
|
6
|
+
pairs = new Map();
|
|
7
|
+
binary = null;
|
|
8
|
+
/** unigrams: "form\tcount\tcapitalizedMidSentence" lines. bigrams: "first second\tcount" lines. */
|
|
9
|
+
static fromTsv(unigrams, bigrams, minCount = 1, minPair = 1) {
|
|
10
|
+
const lexicon = new Lexicon();
|
|
11
|
+
for (const line of unigrams.split("\n")) {
|
|
12
|
+
if (!line)
|
|
13
|
+
continue;
|
|
14
|
+
const [form, count, caps] = line.split("\t");
|
|
15
|
+
const value = Number(count);
|
|
16
|
+
if (value < minCount)
|
|
17
|
+
continue;
|
|
18
|
+
lexicon.counts.set(form, value);
|
|
19
|
+
if (caps && caps !== "0")
|
|
20
|
+
lexicon.capitalized.set(form, Number(caps));
|
|
21
|
+
}
|
|
22
|
+
for (const line of bigrams.split("\n")) {
|
|
23
|
+
if (!line)
|
|
24
|
+
continue;
|
|
25
|
+
const tab = line.lastIndexOf("\t");
|
|
26
|
+
const value = Number(line.slice(tab + 1));
|
|
27
|
+
if (value >= minPair)
|
|
28
|
+
lexicon.pairs.set(line.slice(0, tab), value);
|
|
29
|
+
}
|
|
30
|
+
return lexicon;
|
|
31
|
+
}
|
|
32
|
+
/** Loads a compact `ALX1` binary lexicon (`lexicon.bin`) directly into memory without parsing TSV strings. */
|
|
33
|
+
static fromBinary(bytes) {
|
|
34
|
+
const lexicon = new Lexicon();
|
|
35
|
+
lexicon.binary = new BinaryLexicon(bytes);
|
|
36
|
+
return lexicon;
|
|
37
|
+
}
|
|
38
|
+
count(form) {
|
|
39
|
+
if (this.binary !== null)
|
|
40
|
+
return this.binary.count(form);
|
|
41
|
+
return this.counts.get(form) ?? 0;
|
|
42
|
+
}
|
|
43
|
+
/** Exact-table count only (skips protection-only Bloom filters in binary lexicons for target candidate lookups). */
|
|
44
|
+
exactCount(form) {
|
|
45
|
+
if (this.binary !== null)
|
|
46
|
+
return this.binary.exactCount(form);
|
|
47
|
+
return this.counts.get(form) ?? 0;
|
|
48
|
+
}
|
|
49
|
+
pair(first, second) {
|
|
50
|
+
if (this.binary !== null)
|
|
51
|
+
return this.binary.pair(first, second);
|
|
52
|
+
return this.pairs.get(first + " " + second) ?? 0;
|
|
53
|
+
}
|
|
54
|
+
/** Share of occurrences written with a capital letter mid-sentence: near 1 for names like "ankara". */
|
|
55
|
+
capitalShare(form) {
|
|
56
|
+
if (this.binary !== null)
|
|
57
|
+
return this.binary.capitalShare(form);
|
|
58
|
+
const count = this.count(form);
|
|
59
|
+
return count === 0 ? 0 : (this.capitalized.get(form) ?? 0) / count;
|
|
60
|
+
}
|
|
61
|
+
get size() {
|
|
62
|
+
if (this.binary !== null)
|
|
63
|
+
return this.binary.size;
|
|
64
|
+
return this.counts.size;
|
|
65
|
+
}
|
|
66
|
+
get pairCount() {
|
|
67
|
+
if (this.binary !== null)
|
|
68
|
+
return this.binary.pairCount;
|
|
69
|
+
return this.pairs.size;
|
|
70
|
+
}
|
|
71
|
+
/** The lexicon as the two TSV texts it was loaded from, for measuring what a pruned copy would weigh. */
|
|
72
|
+
toTsv() {
|
|
73
|
+
if (this.binary !== null)
|
|
74
|
+
return this.binary.toTsv();
|
|
75
|
+
const unigrams = [...this.counts].map(([form, count]) => `${form}\t${count}\t${this.capitalized.get(form) ?? 0}`).join("\n");
|
|
76
|
+
const bigrams = [...this.pairs].map(([pair, count]) => `${pair}\t${count}`).join("\n");
|
|
77
|
+
return { unigrams, bigrams };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fast forward FST search engine for Turkish morphological validation and analysis.
|
|
3
|
+
* Adapted from Zemberek's RuleBasedAnalyzer.java (Apache 2.0), redesigned around:
|
|
4
|
+
* - Zero-substring offset tracking (`pos`) into the normalized input string.
|
|
5
|
+
* - Immutable singly-linked PathNodes (no per-step array copying).
|
|
6
|
+
* - 64-entry per-transition phonetic surface/attribute caches.
|
|
7
|
+
* - Longest-stem-first DFS with immediate short-circuit for `isWord()`.
|
|
8
|
+
*/
|
|
9
|
+
import type { StemIndex } from "./dictionary.ts";
|
|
10
|
+
import { type MorphAnalysis } from "./types.ts";
|
|
11
|
+
export declare class MorphologicalAnalyzer {
|
|
12
|
+
private readonly stemIndex;
|
|
13
|
+
private readonly wordCache;
|
|
14
|
+
constructor(stemIndex: StemIndex);
|
|
15
|
+
/** Exposes the underlying StemIndex for lazy fuzzy morphological search. */
|
|
16
|
+
getStemIndex(): StemIndex;
|
|
17
|
+
/**
|
|
18
|
+
* Returns whether `form` is a valid Turkish word according to the loaded lexicon and morphotactics.
|
|
19
|
+
* Uses a bounded cache for fast repeated lookups and longest-stem-first early-exit DFS when uncached.
|
|
20
|
+
*/
|
|
21
|
+
isWord(form: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Returns all valid deduplicated morphological analyses for `form`.
|
|
24
|
+
*/
|
|
25
|
+
analyze(form: string): MorphAnalysis[];
|
|
26
|
+
private remember;
|
|
27
|
+
private checkWordUncached;
|
|
28
|
+
private dfsAccepts;
|
|
29
|
+
private expandNode;
|
|
30
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fast forward FST search engine for Turkish morphological validation and analysis.
|
|
3
|
+
* Adapted from Zemberek's RuleBasedAnalyzer.java (Apache 2.0), redesigned around:
|
|
4
|
+
* - Zero-substring offset tracking (`pos`) into the normalized input string.
|
|
5
|
+
* - Immutable singly-linked PathNodes (no per-step array copying).
|
|
6
|
+
* - 64-entry per-transition phonetic surface/attribute caches.
|
|
7
|
+
* - Longest-stem-first DFS with immediate short-circuit for `isWord()`.
|
|
8
|
+
*/
|
|
9
|
+
import { lowerTr } from "../turkish.js";
|
|
10
|
+
import { PA_CANNOT_TERMINATE, computeNextPhoneticAttrs, generateSurface, hasCircumflex, normalizeCircumflex, surfaceCacheKey, } from "./phonetics.js";
|
|
11
|
+
import { MORPHEMES } from "./morphotactics.js";
|
|
12
|
+
import { RA_DUMMY, } from "./types.js";
|
|
13
|
+
const MAX_REPEATING_STATE_COUNT = 3;
|
|
14
|
+
const MAX_SEARCH_DEPTH = 40;
|
|
15
|
+
const MAX_CACHE_ENTRIES = 65_536;
|
|
16
|
+
function exceedsStateRepeatLimit(node, nextStateId) {
|
|
17
|
+
let count = 0;
|
|
18
|
+
let cur = node;
|
|
19
|
+
while (cur !== null) {
|
|
20
|
+
if (cur.state.id === nextStateId) {
|
|
21
|
+
count++;
|
|
22
|
+
if (count >= MAX_REPEATING_STATE_COUNT)
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
cur = cur.prev;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
function resolveSurfaceAndNextPhonetic(transition, phonetic) {
|
|
30
|
+
const key = surfaceCacheKey(phonetic);
|
|
31
|
+
let surface = transition.surfaceCache[key];
|
|
32
|
+
if (surface === undefined) {
|
|
33
|
+
const generated = generateSurface(transition.tokens, phonetic);
|
|
34
|
+
transition.surfaceCache[key] = generated;
|
|
35
|
+
if (generated !== null) {
|
|
36
|
+
transition.nextPhoneticCache[key] = computeNextPhoneticAttrs(generated, phonetic, transition.lastToken);
|
|
37
|
+
}
|
|
38
|
+
surface = generated;
|
|
39
|
+
}
|
|
40
|
+
if (surface === null)
|
|
41
|
+
return null;
|
|
42
|
+
return { surface, nextPhonetic: transition.nextPhoneticCache[key] ?? 0 };
|
|
43
|
+
}
|
|
44
|
+
function matchesCircumflex(stemTransition, inputWithCircumflex) {
|
|
45
|
+
const stemLen = stemTransition.surface.length;
|
|
46
|
+
let lemmaStem = null;
|
|
47
|
+
for (let i = 0; i < inputWithCircumflex.length; i++) {
|
|
48
|
+
const ch = inputWithCircumflex[i] ?? "";
|
|
49
|
+
if (ch !== "â" && ch !== "î" && ch !== "û")
|
|
50
|
+
continue;
|
|
51
|
+
// Suffixes never carry circumflexes.
|
|
52
|
+
if (i >= stemLen)
|
|
53
|
+
return false;
|
|
54
|
+
if (lemmaStem === null) {
|
|
55
|
+
let item = stemTransition.item;
|
|
56
|
+
if ((item.attributes & RA_DUMMY) !== 0 && item.referenceItem !== null) {
|
|
57
|
+
item = item.referenceItem;
|
|
58
|
+
}
|
|
59
|
+
lemmaStem = item.primaryPos === "Verb" && (item.lemma.endsWith("mak") || item.lemma.endsWith("mek"))
|
|
60
|
+
? item.lemma.slice(0, -3)
|
|
61
|
+
: item.lemma;
|
|
62
|
+
}
|
|
63
|
+
if (i >= lemmaStem.length || lemmaStem[i] !== ch) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
function makeInitialNode(stem, inputLength) {
|
|
70
|
+
return {
|
|
71
|
+
prev: null,
|
|
72
|
+
stemTransition: stem,
|
|
73
|
+
state: stem.to,
|
|
74
|
+
surface: stem.surface,
|
|
75
|
+
pos: stem.surface.length,
|
|
76
|
+
inputLength,
|
|
77
|
+
phonetic: stem.phonetic,
|
|
78
|
+
depth: 0,
|
|
79
|
+
hasSurface: false,
|
|
80
|
+
hasDerivation: false,
|
|
81
|
+
currentGroupHasSurface: false,
|
|
82
|
+
lastDerivation: null,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function stepNode(prev, transition, surface, nextPhonetic) {
|
|
86
|
+
const isDeriv = transition.to.derivative;
|
|
87
|
+
const hasSurf = surface.length > 0;
|
|
88
|
+
return {
|
|
89
|
+
prev,
|
|
90
|
+
stemTransition: prev.stemTransition,
|
|
91
|
+
state: transition.to,
|
|
92
|
+
surface,
|
|
93
|
+
pos: prev.pos + surface.length,
|
|
94
|
+
inputLength: prev.inputLength,
|
|
95
|
+
phonetic: nextPhonetic,
|
|
96
|
+
depth: prev.depth + 1,
|
|
97
|
+
hasSurface: prev.hasSurface || hasSurf,
|
|
98
|
+
hasDerivation: prev.hasDerivation || isDeriv,
|
|
99
|
+
currentGroupHasSurface: isDeriv ? false : prev.currentGroupHasSurface || hasSurf,
|
|
100
|
+
lastDerivation: isDeriv ? transition.to : prev.lastDerivation,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function pathToAnalysis(endNode) {
|
|
104
|
+
const chain = [];
|
|
105
|
+
let cur = endNode;
|
|
106
|
+
while (cur !== null) {
|
|
107
|
+
chain.push(cur);
|
|
108
|
+
cur = cur.prev;
|
|
109
|
+
}
|
|
110
|
+
let item = endNode.stemTransition.item;
|
|
111
|
+
if ((item.attributes & RA_DUMMY) !== 0 && item.referenceItem !== null) {
|
|
112
|
+
item = item.referenceItem;
|
|
113
|
+
}
|
|
114
|
+
const morphemes = [];
|
|
115
|
+
// Skip index chain.length - 1 (the root state itself) and skip empty Pnon/Nom noise.
|
|
116
|
+
for (let i = chain.length - 2; i >= 0; i--) {
|
|
117
|
+
const node = chain[i];
|
|
118
|
+
if (node === undefined)
|
|
119
|
+
continue;
|
|
120
|
+
const m = node.state.morpheme;
|
|
121
|
+
if (m === MORPHEMES.nom || m === MORPHEMES.pnon) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
morphemes.push(m.id);
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
stem: item.root,
|
|
128
|
+
pos: item.primaryPos,
|
|
129
|
+
morphemes,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export class MorphologicalAnalyzer {
|
|
133
|
+
stemIndex;
|
|
134
|
+
wordCache;
|
|
135
|
+
constructor(stemIndex) {
|
|
136
|
+
this.stemIndex = stemIndex;
|
|
137
|
+
this.wordCache = new Map();
|
|
138
|
+
}
|
|
139
|
+
/** Exposes the underlying StemIndex for lazy fuzzy morphological search. */
|
|
140
|
+
getStemIndex() {
|
|
141
|
+
return this.stemIndex;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Returns whether `form` is a valid Turkish word according to the loaded lexicon and morphotactics.
|
|
145
|
+
* Uses a bounded cache for fast repeated lookups and longest-stem-first early-exit DFS when uncached.
|
|
146
|
+
*/
|
|
147
|
+
isWord(form) {
|
|
148
|
+
if (form.length === 0)
|
|
149
|
+
return false;
|
|
150
|
+
const cachedRaw = this.wordCache.get(form);
|
|
151
|
+
if (cachedRaw !== undefined)
|
|
152
|
+
return cachedRaw;
|
|
153
|
+
const lower = lowerTr(form);
|
|
154
|
+
if (lower !== form) {
|
|
155
|
+
const cachedLower = this.wordCache.get(lower);
|
|
156
|
+
if (cachedLower !== undefined) {
|
|
157
|
+
this.remember(form, cachedLower);
|
|
158
|
+
return cachedLower;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const valid = this.checkWordUncached(lower);
|
|
162
|
+
this.remember(lower, valid);
|
|
163
|
+
if (lower !== form) {
|
|
164
|
+
this.remember(form, valid);
|
|
165
|
+
}
|
|
166
|
+
return valid;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Returns all valid deduplicated morphological analyses for `form`.
|
|
170
|
+
*/
|
|
171
|
+
analyze(form) {
|
|
172
|
+
if (form.length === 0)
|
|
173
|
+
return [];
|
|
174
|
+
const lower = lowerTr(form);
|
|
175
|
+
if (lower.length === 0)
|
|
176
|
+
return [];
|
|
177
|
+
const inputHasCircumflex = hasCircumflex(lower);
|
|
178
|
+
const searchInput = inputHasCircumflex ? normalizeCircumflex(lower) : lower;
|
|
179
|
+
const inputLen = searchInput.length;
|
|
180
|
+
const maxLen = Math.min(inputLen, this.stemIndex.maxStemLength);
|
|
181
|
+
const results = [];
|
|
182
|
+
const seenKeys = new Set();
|
|
183
|
+
const stack = [];
|
|
184
|
+
for (let len = 1; len <= maxLen; len++) {
|
|
185
|
+
const entry = this.stemIndex.stems.get(searchInput.slice(0, len));
|
|
186
|
+
if (entry === undefined)
|
|
187
|
+
continue;
|
|
188
|
+
if (Array.isArray(entry)) {
|
|
189
|
+
for (let i = 0; i < entry.length; i++) {
|
|
190
|
+
const st = entry[i];
|
|
191
|
+
if (st !== undefined && (!inputHasCircumflex || matchesCircumflex(st, lower))) {
|
|
192
|
+
stack.push(makeInitialNode(st, inputLen));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if (!inputHasCircumflex || matchesCircumflex(entry, lower)) {
|
|
197
|
+
stack.push(makeInitialNode(entry, inputLen));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
while (stack.length > 0) {
|
|
201
|
+
const node = stack.pop();
|
|
202
|
+
if (node === undefined)
|
|
203
|
+
break;
|
|
204
|
+
if (node.pos === inputLen && node.state.terminal && (node.phonetic & PA_CANNOT_TERMINATE) === 0) {
|
|
205
|
+
const analysis = pathToAnalysis(node);
|
|
206
|
+
const dedupKey = `${analysis.stem}|${analysis.pos}|${analysis.morphemes.join("+")}`;
|
|
207
|
+
if (!seenKeys.has(dedupKey)) {
|
|
208
|
+
seenKeys.add(dedupKey);
|
|
209
|
+
results.push(analysis);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (node.depth >= MAX_SEARCH_DEPTH)
|
|
213
|
+
continue;
|
|
214
|
+
this.expandNode(node, searchInput, inputLen, stack);
|
|
215
|
+
}
|
|
216
|
+
return results;
|
|
217
|
+
}
|
|
218
|
+
remember(key, value) {
|
|
219
|
+
if (this.wordCache.size >= MAX_CACHE_ENTRIES) {
|
|
220
|
+
this.wordCache.clear();
|
|
221
|
+
}
|
|
222
|
+
this.wordCache.set(key, value);
|
|
223
|
+
}
|
|
224
|
+
checkWordUncached(lower) {
|
|
225
|
+
if (lower.length === 0)
|
|
226
|
+
return false;
|
|
227
|
+
const inputHasCircumflex = hasCircumflex(lower);
|
|
228
|
+
const searchInput = inputHasCircumflex ? normalizeCircumflex(lower) : lower;
|
|
229
|
+
const inputLen = searchInput.length;
|
|
230
|
+
const maxLen = Math.min(inputLen, this.stemIndex.maxStemLength);
|
|
231
|
+
const stack = [];
|
|
232
|
+
// Try longest stem prefixes first so uninflected / lightly-inflected words match immediately.
|
|
233
|
+
for (let len = maxLen; len >= 1; len--) {
|
|
234
|
+
const entry = this.stemIndex.stems.get(searchInput.slice(0, len));
|
|
235
|
+
if (entry === undefined)
|
|
236
|
+
continue;
|
|
237
|
+
if (Array.isArray(entry)) {
|
|
238
|
+
for (let i = 0; i < entry.length; i++) {
|
|
239
|
+
const st = entry[i];
|
|
240
|
+
if (st !== undefined && (!inputHasCircumflex || matchesCircumflex(st, lower))) {
|
|
241
|
+
stack.push(makeInitialNode(st, inputLen));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else if (!inputHasCircumflex || matchesCircumflex(entry, lower)) {
|
|
246
|
+
stack.push(makeInitialNode(entry, inputLen));
|
|
247
|
+
}
|
|
248
|
+
if (stack.length > 0 && this.dfsAccepts(stack, searchInput, inputLen)) {
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
dfsAccepts(stack, searchInput, inputLen) {
|
|
255
|
+
while (stack.length > 0) {
|
|
256
|
+
const node = stack.pop();
|
|
257
|
+
if (node === undefined)
|
|
258
|
+
break;
|
|
259
|
+
if (node.pos === inputLen && node.state.terminal && (node.phonetic & PA_CANNOT_TERMINATE) === 0) {
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
if (node.depth >= MAX_SEARCH_DEPTH)
|
|
263
|
+
continue;
|
|
264
|
+
this.expandNode(node, searchInput, inputLen, stack);
|
|
265
|
+
}
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
expandNode(node, searchInput, inputLen, stack) {
|
|
269
|
+
const outgoing = node.state.outgoing;
|
|
270
|
+
const remaining = inputLen - node.pos;
|
|
271
|
+
const phonetic = node.phonetic;
|
|
272
|
+
for (let i = 0; i < outgoing.length; i++) {
|
|
273
|
+
const tr = outgoing[i];
|
|
274
|
+
if (tr === undefined)
|
|
275
|
+
continue;
|
|
276
|
+
// Fast-path empty (epsilon) transitions.
|
|
277
|
+
if (!tr.hasSurface) {
|
|
278
|
+
if (tr.condition !== null && !tr.condition(node))
|
|
279
|
+
continue;
|
|
280
|
+
if (node.depth > 8 && exceedsStateRepeatLimit(node, tr.to.id))
|
|
281
|
+
continue;
|
|
282
|
+
stack.push(stepNode(node, tr, "", phonetic));
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (remaining === 0)
|
|
286
|
+
continue;
|
|
287
|
+
// Fast bitmask check for vowel/consonant expectation before surface lookup.
|
|
288
|
+
if ((phonetic & tr.forbiddenPhonetic) !== 0)
|
|
289
|
+
continue;
|
|
290
|
+
const resolved = resolveSurfaceAndNextPhonetic(tr, phonetic);
|
|
291
|
+
if (resolved === null)
|
|
292
|
+
continue;
|
|
293
|
+
const { surface, nextPhonetic } = resolved;
|
|
294
|
+
const surfLen = surface.length;
|
|
295
|
+
if (surfLen > remaining)
|
|
296
|
+
continue;
|
|
297
|
+
if (surfLen > 0) {
|
|
298
|
+
// Check first character before startsWith to reject mismatches in O(1).
|
|
299
|
+
if (searchInput.charCodeAt(node.pos) !== surface.charCodeAt(0))
|
|
300
|
+
continue;
|
|
301
|
+
if (surfLen > 1 && !searchInput.startsWith(surface, node.pos))
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (tr.condition !== null && !tr.condition(node))
|
|
305
|
+
continue;
|
|
306
|
+
if (node.depth > 8 && exceedsStateRepeatLimit(node, tr.to.id))
|
|
307
|
+
continue;
|
|
308
|
+
stack.push(stepNode(node, tr, surface, nextPhonetic));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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 { type MorphPrior } from "./prior.ts";
|
|
7
|
+
import { type DictionaryItem } from "./types.ts";
|
|
8
|
+
/**
|
|
9
|
+
* Encodes parsed `DictionaryItem[]` into a compact, columnar, front-coded `AMR1` (or `AMR2` with prior) binary payload.
|
|
10
|
+
*/
|
|
11
|
+
export declare function encodeBinaryDictionary(items: readonly DictionaryItem[], prior?: MorphPrior): Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* Decodes an `AMR1` or `AMR2` binary morphology payload into exact `DictionaryItem[]` plus optional `MorphPrior`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseBinaryDictionaryWithPrior(bytes: Uint8Array): {
|
|
16
|
+
items: DictionaryItem[];
|
|
17
|
+
prior?: MorphPrior;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Decodes an `AMR1`/`AMR2` binary morphology payload back into exact `DictionaryItem[]`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseBinaryDictionary(bytes: Uint8Array): DictionaryItem[];
|