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,88 @@
|
|
|
1
|
+
import type { Lexicon } from "./lexicon.ts";
|
|
2
|
+
import { type PhraseContext } from "./rules/proper-names.ts";
|
|
3
|
+
export interface KnownFix {
|
|
4
|
+
text: string;
|
|
5
|
+
/** How often the fix is seen, for comparing with what was typed. */
|
|
6
|
+
count: number;
|
|
7
|
+
/** True when the rule covers the next word too ("hiç bir" -> "hiçbir"). */
|
|
8
|
+
joinsNext: boolean;
|
|
9
|
+
/** True for spoken-style contractions ("noldu" -> "ne oldu"): see Rule.contraction. */
|
|
10
|
+
contraction: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface NameEntry {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly count: number;
|
|
15
|
+
/** True when suffixes on this proper name take NO apostrophe per TDK (languages, derivations, religions like "Türkçe", "Hinduizm"). */
|
|
16
|
+
readonly noApos: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface PhraseEntry {
|
|
19
|
+
readonly words: readonly string[];
|
|
20
|
+
readonly displayWords: readonly string[];
|
|
21
|
+
/** True when inflectional suffixes on the final word take an apostrophe (places, events, works); false for institutions. */
|
|
22
|
+
readonly takesApos: boolean;
|
|
23
|
+
/** Optional contextual gate when the phrase overlaps with ordinary Turkish prose ("era", "day", "work"). */
|
|
24
|
+
readonly context?: PhraseContext;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Misspellings people make often enough to list: "herkez" -> "herkes", "birşey" -> "bir şey", "hiç bir" -> "hiçbir".
|
|
28
|
+
* A rule also covers the wrong form with a suffix ("herkezin" -> "herkesin") when the fixed form with that suffix is
|
|
29
|
+
* a word the lexicon has seen, so "yalnış" never reaches into "yalnızlık".
|
|
30
|
+
*
|
|
31
|
+
* Also stores proper-name tables appended into `known.tsv` for the browser bundle:
|
|
32
|
+
* NAME, ACRO, GIVEN, SURNAME, AMBIG, PHRASE_APOS, PHRASE_NOAPOS.
|
|
33
|
+
*/
|
|
34
|
+
export declare class KnownMisspellings {
|
|
35
|
+
private readonly single;
|
|
36
|
+
private readonly byFirstWord;
|
|
37
|
+
/** Two-word rules with Turkish letters, keyed by the first word without them ("hic" for "hiç bir"). */
|
|
38
|
+
private readonly byPlainFirstWord;
|
|
39
|
+
private readonly stems;
|
|
40
|
+
/** Unconditional proper names always written with a capital ("ahmet" -> "Ahmet"). */
|
|
41
|
+
private readonly names;
|
|
42
|
+
/** Unique ASCII-folded lookup for proper names with Turkish diacritics ("canakkale" -> "Çanakkale"). */
|
|
43
|
+
private readonly asciiNames;
|
|
44
|
+
/** Unconditional all-caps acronyms ("tbmm" -> "TBMM", "nato" -> "NATO"). */
|
|
45
|
+
private readonly acronyms;
|
|
46
|
+
/** Unique ASCII-folded lookup for acronyms with diacritics ("itu" -> "İTÜ", "odtu" -> "ODTÜ"). */
|
|
47
|
+
private readonly asciiAcronyms;
|
|
48
|
+
/** Turkish given names (including ambiguous names like "deniz", "umut", "defne") for person-frame rules. */
|
|
49
|
+
private readonly givenNames;
|
|
50
|
+
/** Turkish surnames (including ambiguous surnames like "çelik", "şahin", "kaya") for person-frame rules. */
|
|
51
|
+
private readonly surnames;
|
|
52
|
+
/** Ambiguous names (both proper name and ordinary Turkish word) for contextual disambiguation. */
|
|
53
|
+
private readonly ambigNames;
|
|
54
|
+
/** Multi-word proper phrases indexed by lowercased first word (longest first). */
|
|
55
|
+
private readonly phrasesByFirst;
|
|
56
|
+
/** Multi-word proper phrases indexed by ASCII-folded first word (for ASCII-typed sentences). */
|
|
57
|
+
private readonly phrasesByPlainFirst;
|
|
58
|
+
private hasCanonicalPhrases;
|
|
59
|
+
/** Lines of data/rules/known.tsv: wrong, right, wrong count/flags, right count, section. */
|
|
60
|
+
static fromTsv(text: string): KnownMisspellings;
|
|
61
|
+
private addPhrase;
|
|
62
|
+
private ensureCanonicalPhrases;
|
|
63
|
+
/** The capitalized spelling of a proper name, from its lowercase form ("istanbul" -> "İstanbul"). */
|
|
64
|
+
nameFor(lower: string, minCount?: number): string | undefined;
|
|
65
|
+
/** Full metadata for a proper name, including whether TDK forbids apostrophes on it. */
|
|
66
|
+
nameEntry(lower: string, minCount?: number): NameEntry | undefined;
|
|
67
|
+
/** Unique ASCII-folded proper-name lookup ("canakkale" -> "Çanakkale"), excluding exact hits. */
|
|
68
|
+
asciiNameEntry(foldedLower: string, minCount?: number): NameEntry | undefined;
|
|
69
|
+
/** All-caps acronym lookup ("tbmm" -> "TBMM"). */
|
|
70
|
+
acronymFor(lower: string): string | undefined;
|
|
71
|
+
/** Unique ASCII-folded acronym lookup ("itu" -> "İTÜ"). */
|
|
72
|
+
asciiAcronymFor(foldedLower: string): string | undefined;
|
|
73
|
+
/** Known Turkish given name (including ambiguous ones like "deniz", "umut", "defne"). */
|
|
74
|
+
givenNameFor(lower: string): string | undefined;
|
|
75
|
+
/** Known Turkish surname (including ambiguous ones like "çelik", "şahin", "kaya"). */
|
|
76
|
+
surnameFor(lower: string): string | undefined;
|
|
77
|
+
/** Known ambiguous name (person/place/work word that is also a common Turkish noun). */
|
|
78
|
+
ambigNameFor(lower: string): string | undefined;
|
|
79
|
+
/** Multi-word proper phrases starting with `firstLower` (or ASCII-folded when `allowPlain` is true). */
|
|
80
|
+
phrasesStartingWith(firstLower: string, allowPlain?: boolean): readonly PhraseEntry[];
|
|
81
|
+
get hasProperData(): boolean;
|
|
82
|
+
get size(): number;
|
|
83
|
+
/**
|
|
84
|
+
* Fixes for a lowercased word, and for it with the next word when a two-word rule matches. With `typedPlain` (the
|
|
85
|
+
* sentence is typed without Turkish letters) a two-word rule also matches its plain form: "hic bir" -> "hiçbir".
|
|
86
|
+
*/
|
|
87
|
+
find(word: string, next: string | undefined, lexicon: Lexicon, typedPlain?: boolean): KnownFix[];
|
|
88
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { stripMarksIn } from "./turkish.js";
|
|
2
|
+
import { isValidKnownParticleSuffix } from "./rules/turkish-rules.js";
|
|
3
|
+
import { CANONICAL_PHRASES, foldTurkish } from "./rules/proper-names.js";
|
|
4
|
+
const MAX_SUFFIX = 6;
|
|
5
|
+
const SUFFIX = /^[a-zçğıöşü]+$/;
|
|
6
|
+
/**
|
|
7
|
+
* Misspellings people make often enough to list: "herkez" -> "herkes", "birşey" -> "bir şey", "hiç bir" -> "hiçbir".
|
|
8
|
+
* A rule also covers the wrong form with a suffix ("herkezin" -> "herkesin") when the fixed form with that suffix is
|
|
9
|
+
* a word the lexicon has seen, so "yalnış" never reaches into "yalnızlık".
|
|
10
|
+
*
|
|
11
|
+
* Also stores proper-name tables appended into `known.tsv` for the browser bundle:
|
|
12
|
+
* NAME, ACRO, GIVEN, SURNAME, AMBIG, PHRASE_APOS, PHRASE_NOAPOS.
|
|
13
|
+
*/
|
|
14
|
+
export class KnownMisspellings {
|
|
15
|
+
single = new Map();
|
|
16
|
+
byFirstWord = new Map();
|
|
17
|
+
/** Two-word rules with Turkish letters, keyed by the first word without them ("hic" for "hiç bir"). */
|
|
18
|
+
byPlainFirstWord = new Map();
|
|
19
|
+
stems = [];
|
|
20
|
+
/** Unconditional proper names always written with a capital ("ahmet" -> "Ahmet"). */
|
|
21
|
+
names = new Map();
|
|
22
|
+
/** Unique ASCII-folded lookup for proper names with Turkish diacritics ("canakkale" -> "Çanakkale"). */
|
|
23
|
+
asciiNames = new Map();
|
|
24
|
+
/** Unconditional all-caps acronyms ("tbmm" -> "TBMM", "nato" -> "NATO"). */
|
|
25
|
+
acronyms = new Map();
|
|
26
|
+
/** Unique ASCII-folded lookup for acronyms with diacritics ("itu" -> "İTÜ", "odtu" -> "ODTÜ"). */
|
|
27
|
+
asciiAcronyms = new Map();
|
|
28
|
+
/** Turkish given names (including ambiguous names like "deniz", "umut", "defne") for person-frame rules. */
|
|
29
|
+
givenNames = new Map();
|
|
30
|
+
/** Turkish surnames (including ambiguous surnames like "çelik", "şahin", "kaya") for person-frame rules. */
|
|
31
|
+
surnames = new Map();
|
|
32
|
+
/** Ambiguous names (both proper name and ordinary Turkish word) for contextual disambiguation. */
|
|
33
|
+
ambigNames = new Map();
|
|
34
|
+
/** Multi-word proper phrases indexed by lowercased first word (longest first). */
|
|
35
|
+
phrasesByFirst = new Map();
|
|
36
|
+
/** Multi-word proper phrases indexed by ASCII-folded first word (for ASCII-typed sentences). */
|
|
37
|
+
phrasesByPlainFirst = new Map();
|
|
38
|
+
hasCanonicalPhrases = false;
|
|
39
|
+
/** Lines of data/rules/known.tsv: wrong, right, wrong count/flags, right count, section. */
|
|
40
|
+
static fromTsv(text) {
|
|
41
|
+
const known = new KnownMisspellings();
|
|
42
|
+
let hasProperSections = false;
|
|
43
|
+
for (const line of text.split("\n")) {
|
|
44
|
+
if (!line || line.startsWith("#"))
|
|
45
|
+
continue;
|
|
46
|
+
const [wrong, right, col3, rightCount, sectionRaw] = line.split("\t");
|
|
47
|
+
if (!wrong || !right)
|
|
48
|
+
continue;
|
|
49
|
+
const section = sectionRaw?.trim();
|
|
50
|
+
if (section === "NAME") {
|
|
51
|
+
hasProperSections = true;
|
|
52
|
+
const noApos = col3?.includes("noapos") ?? false;
|
|
53
|
+
const entry = { name: right, count: Number(rightCount) || 0, noApos };
|
|
54
|
+
known.names.set(wrong, entry);
|
|
55
|
+
const folded = foldTurkish(wrong);
|
|
56
|
+
if (folded !== wrong) {
|
|
57
|
+
const prev = known.asciiNames.get(folded);
|
|
58
|
+
if (prev === undefined)
|
|
59
|
+
known.asciiNames.set(folded, entry);
|
|
60
|
+
else if (prev !== null && prev.name !== entry.name)
|
|
61
|
+
known.asciiNames.set(folded, null);
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (section === "ACRO") {
|
|
66
|
+
hasProperSections = true;
|
|
67
|
+
const entry = { upper: right, count: Number(rightCount) || 0 };
|
|
68
|
+
known.acronyms.set(wrong, entry);
|
|
69
|
+
const folded = foldTurkish(wrong);
|
|
70
|
+
if (folded !== wrong) {
|
|
71
|
+
const prev = known.asciiAcronyms.get(folded);
|
|
72
|
+
if (prev === undefined)
|
|
73
|
+
known.asciiAcronyms.set(folded, entry);
|
|
74
|
+
else if (prev !== null && prev.upper !== entry.upper)
|
|
75
|
+
known.asciiAcronyms.set(folded, null);
|
|
76
|
+
}
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (section === "GIVEN") {
|
|
80
|
+
hasProperSections = true;
|
|
81
|
+
known.givenNames.set(wrong, right);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (section === "SURNAME") {
|
|
85
|
+
hasProperSections = true;
|
|
86
|
+
known.surnames.set(wrong, right);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (section === "AMBIG") {
|
|
90
|
+
hasProperSections = true;
|
|
91
|
+
known.ambigNames.set(wrong, right);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (section === "PHRASE_APOS" || section === "PHRASE_NOAPOS") {
|
|
95
|
+
hasProperSections = true;
|
|
96
|
+
const ctx = col3 === "era" || col3 === "day" || col3 === "work" ? col3 : undefined;
|
|
97
|
+
known.addPhrase(wrong, right, section === "PHRASE_APOS", ctx);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const rule = { wrong, right, rightCount: Number(rightCount), contraction: section === "CONTRACTION" };
|
|
101
|
+
const words = wrong.split(" ");
|
|
102
|
+
if (words.length === 1) {
|
|
103
|
+
known.single.set(wrong, rule);
|
|
104
|
+
if (wrong.length >= 4)
|
|
105
|
+
known.stems.push(rule);
|
|
106
|
+
}
|
|
107
|
+
else if (words.length === 2) {
|
|
108
|
+
const list = known.byFirstWord.get(words[0]) ?? [];
|
|
109
|
+
list.push(rule);
|
|
110
|
+
known.byFirstWord.set(words[0], list);
|
|
111
|
+
const plainFirst = plain(words[0]);
|
|
112
|
+
if (plain(wrong) !== wrong) {
|
|
113
|
+
const plainList = known.byPlainFirstWord.get(plainFirst) ?? [];
|
|
114
|
+
plainList.push(rule);
|
|
115
|
+
known.byPlainFirstWord.set(plainFirst, plainList);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// Always register canonical TDK multi-word phrases when proper-name sections are present (or lazily via ensureCanonicalPhrases).
|
|
120
|
+
if (hasProperSections) {
|
|
121
|
+
known.ensureCanonicalPhrases();
|
|
122
|
+
}
|
|
123
|
+
known.stems.sort((first, second) => second.wrong.length - first.wrong.length);
|
|
124
|
+
for (const list of known.phrasesByFirst.values()) {
|
|
125
|
+
list.sort((a, b) => b.words.length - a.words.length);
|
|
126
|
+
}
|
|
127
|
+
for (const list of known.phrasesByPlainFirst.values()) {
|
|
128
|
+
list.sort((a, b) => b.words.length - a.words.length);
|
|
129
|
+
}
|
|
130
|
+
return known;
|
|
131
|
+
}
|
|
132
|
+
addPhrase(lowerPhrase, displayPhrase, takesApos, context) {
|
|
133
|
+
const words = lowerPhrase.split(" ");
|
|
134
|
+
const displayWords = displayPhrase.split(" ");
|
|
135
|
+
if (words.length < 2 || words.length !== displayWords.length)
|
|
136
|
+
return;
|
|
137
|
+
const first = words[0];
|
|
138
|
+
const existing = this.phrasesByFirst.get(first);
|
|
139
|
+
const dupIdx = existing?.findIndex((p) => p.words.length === words.length && p.words.every((w, i) => w === words[i])) ?? -1;
|
|
140
|
+
if (dupIdx !== -1 && existing) {
|
|
141
|
+
// Allow canonical contextual metadata to refine an untagged TSV entry.
|
|
142
|
+
if (context && !existing[dupIdx].context) {
|
|
143
|
+
const updated = { ...existing[dupIdx], context };
|
|
144
|
+
existing[dupIdx] = updated;
|
|
145
|
+
const pList = this.phrasesByPlainFirst.get(foldTurkish(first));
|
|
146
|
+
if (pList) {
|
|
147
|
+
const pIdx = pList.findIndex((p) => p.words.length === words.length && p.words.every((w, i) => w === words[i]));
|
|
148
|
+
if (pIdx !== -1)
|
|
149
|
+
pList[pIdx] = updated;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const entry = context ? { words, displayWords, takesApos, context } : { words, displayWords, takesApos };
|
|
155
|
+
if (existing)
|
|
156
|
+
existing.push(entry);
|
|
157
|
+
else
|
|
158
|
+
this.phrasesByFirst.set(first, [entry]);
|
|
159
|
+
const plainFirst = foldTurkish(first);
|
|
160
|
+
if (foldTurkish(lowerPhrase) !== lowerPhrase) {
|
|
161
|
+
const pList = this.phrasesByPlainFirst.get(plainFirst);
|
|
162
|
+
if (pList)
|
|
163
|
+
pList.push(entry);
|
|
164
|
+
else
|
|
165
|
+
this.phrasesByPlainFirst.set(plainFirst, [entry]);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
ensureCanonicalPhrases() {
|
|
169
|
+
if (this.hasCanonicalPhrases)
|
|
170
|
+
return;
|
|
171
|
+
this.hasCanonicalPhrases = true;
|
|
172
|
+
for (const [display, takesApos, context] of CANONICAL_PHRASES) {
|
|
173
|
+
this.addPhrase(display.replaceAll("I", "ı").replaceAll("İ", "i").toLowerCase(), display, takesApos, context);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** The capitalized spelling of a proper name, from its lowercase form ("istanbul" -> "İstanbul"). */
|
|
177
|
+
nameFor(lower, minCount = 0) {
|
|
178
|
+
const entry = this.names.get(lower);
|
|
179
|
+
return entry && entry.count >= minCount ? entry.name : undefined;
|
|
180
|
+
}
|
|
181
|
+
/** Full metadata for a proper name, including whether TDK forbids apostrophes on it. */
|
|
182
|
+
nameEntry(lower, minCount = 0) {
|
|
183
|
+
const entry = this.names.get(lower);
|
|
184
|
+
return entry && entry.count >= minCount ? entry : undefined;
|
|
185
|
+
}
|
|
186
|
+
/** Unique ASCII-folded proper-name lookup ("canakkale" -> "Çanakkale"), excluding exact hits. */
|
|
187
|
+
asciiNameEntry(foldedLower, minCount = 0) {
|
|
188
|
+
const entry = this.asciiNames.get(foldedLower);
|
|
189
|
+
return entry && entry.count >= minCount ? entry : undefined;
|
|
190
|
+
}
|
|
191
|
+
/** All-caps acronym lookup ("tbmm" -> "TBMM"). */
|
|
192
|
+
acronymFor(lower) {
|
|
193
|
+
return this.acronyms.get(lower)?.upper;
|
|
194
|
+
}
|
|
195
|
+
/** Unique ASCII-folded acronym lookup ("itu" -> "İTÜ"). */
|
|
196
|
+
asciiAcronymFor(foldedLower) {
|
|
197
|
+
const entry = this.asciiAcronyms.get(foldedLower);
|
|
198
|
+
return entry ? entry.upper : undefined;
|
|
199
|
+
}
|
|
200
|
+
/** Known Turkish given name (including ambiguous ones like "deniz", "umut", "defne"). */
|
|
201
|
+
givenNameFor(lower) {
|
|
202
|
+
return this.givenNames.get(lower);
|
|
203
|
+
}
|
|
204
|
+
/** Known Turkish surname (including ambiguous ones like "çelik", "şahin", "kaya"). */
|
|
205
|
+
surnameFor(lower) {
|
|
206
|
+
return this.surnames.get(lower);
|
|
207
|
+
}
|
|
208
|
+
/** Known ambiguous name (person/place/work word that is also a common Turkish noun). */
|
|
209
|
+
ambigNameFor(lower) {
|
|
210
|
+
return this.ambigNames.get(lower);
|
|
211
|
+
}
|
|
212
|
+
/** Multi-word proper phrases starting with `firstLower` (or ASCII-folded when `allowPlain` is true). */
|
|
213
|
+
phrasesStartingWith(firstLower, allowPlain = false) {
|
|
214
|
+
const exact = this.phrasesByFirst.get(firstLower);
|
|
215
|
+
if (!allowPlain)
|
|
216
|
+
return exact ?? [];
|
|
217
|
+
const plainList = this.phrasesByPlainFirst.get(foldTurkish(firstLower));
|
|
218
|
+
if (!plainList)
|
|
219
|
+
return exact ?? [];
|
|
220
|
+
if (!exact)
|
|
221
|
+
return plainList;
|
|
222
|
+
return [...exact, ...plainList];
|
|
223
|
+
}
|
|
224
|
+
get hasProperData() {
|
|
225
|
+
return this.names.size > 0;
|
|
226
|
+
}
|
|
227
|
+
get size() {
|
|
228
|
+
return this.single.size + [...this.byFirstWord.values()].reduce((sum, list) => sum + list.length, 0);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Fixes for a lowercased word, and for it with the next word when a two-word rule matches. With `typedPlain` (the
|
|
232
|
+
* sentence is typed without Turkish letters) a two-word rule also matches its plain form: "hic bir" -> "hiçbir".
|
|
233
|
+
*/
|
|
234
|
+
find(word, next, lexicon, typedPlain = false) {
|
|
235
|
+
const fixes = [];
|
|
236
|
+
const exact = this.single.get(word);
|
|
237
|
+
if (exact)
|
|
238
|
+
fixes.push({ text: exact.right, count: exact.rightCount, joinsNext: false, contraction: exact.contraction });
|
|
239
|
+
else {
|
|
240
|
+
for (const rule of this.stems) {
|
|
241
|
+
if (!word.startsWith(rule.wrong))
|
|
242
|
+
continue;
|
|
243
|
+
const suffix = word.slice(rule.wrong.length);
|
|
244
|
+
if (suffix.length > MAX_SUFFIX || !SUFFIX.test(suffix))
|
|
245
|
+
continue;
|
|
246
|
+
if (!isValidKnownParticleSuffix(rule.right, suffix))
|
|
247
|
+
continue;
|
|
248
|
+
const fixed = rule.right + suffix;
|
|
249
|
+
const lastWord = fixed.split(" ").at(-1);
|
|
250
|
+
const count = lexicon.count(lastWord);
|
|
251
|
+
if (count > 0) {
|
|
252
|
+
fixes.push({ text: fixed, count: Math.min(count, rule.rightCount), joinsNext: false, contraction: rule.contraction });
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (next) {
|
|
258
|
+
for (const rule of this.byFirstWord.get(word) ?? []) {
|
|
259
|
+
const second = rule.wrong.split(" ")[1];
|
|
260
|
+
if (next === second)
|
|
261
|
+
fixes.push({ text: rule.right, count: rule.rightCount, joinsNext: true, contraction: rule.contraction });
|
|
262
|
+
else if (next.startsWith(second) && SUFFIX.test(next.slice(second.length)) && next.length - second.length <= MAX_SUFFIX) {
|
|
263
|
+
const suffix = next.slice(second.length);
|
|
264
|
+
if (!isValidKnownParticleSuffix(rule.right, suffix))
|
|
265
|
+
continue;
|
|
266
|
+
const fixed = rule.right + suffix;
|
|
267
|
+
const count = lexicon.count(fixed.split(" ").at(-1));
|
|
268
|
+
if (count > 0)
|
|
269
|
+
fixes.push({ text: fixed, count: Math.min(count, rule.rightCount), joinsNext: true, contraction: rule.contraction });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (typedPlain && fixes.every((fix) => !fix.joinsNext)) {
|
|
273
|
+
for (const rule of this.byPlainFirstWord.get(word) ?? []) {
|
|
274
|
+
if (plain(rule.wrong) === `${word} ${next}`) {
|
|
275
|
+
fixes.push({ text: rule.right, count: rule.rightCount, joinsNext: true, contraction: rule.contraction });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return fixes;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** The word without Turkish letters. */
|
|
284
|
+
function plain(text) {
|
|
285
|
+
return stripMarksIn(text, [[0, text.length]]);
|
|
286
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compact binary format (`ALX1`) for Turkish unigram + bigram lexicons.
|
|
3
|
+
*
|
|
4
|
+
* Design goals:
|
|
5
|
+
* - Zero external dependencies, browser-safe (TypedArrays + DataView + TextEncoder/TextDecoder).
|
|
6
|
+
* - Columnar 1-byte alphabet front-coding for fast direct binary search (~1 ms index init, ~27 µs/word, ~6 MB RAM).
|
|
7
|
+
* - Exact small counts + 1-byte logarithmic quantization for larger counts (zero accuracy loss).
|
|
8
|
+
* - Disjoint Split-CSR bigram tables keyed by unigram IDs (bySecond for frequent trailing particles, byFirst for rest).
|
|
9
|
+
* - Optional Bloom filter tiers for rare protection-only forms (`count = 2, 3, ...`) so clean rare words stay protected
|
|
10
|
+
* without paying full string storage.
|
|
11
|
+
*/
|
|
12
|
+
export interface BinaryUnigramInput {
|
|
13
|
+
readonly form: string;
|
|
14
|
+
readonly count: number;
|
|
15
|
+
readonly capitalized: number;
|
|
16
|
+
}
|
|
17
|
+
export interface BinaryBigramInput {
|
|
18
|
+
readonly first: string;
|
|
19
|
+
readonly second: string;
|
|
20
|
+
readonly count: number;
|
|
21
|
+
/** True to store under the `bySecond` CSR partition (optimal for frequent trailing particles like de/da/ki/mi). */
|
|
22
|
+
readonly groupBySecond?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface BinaryBloomTierInput {
|
|
25
|
+
readonly returnCount: number;
|
|
26
|
+
readonly bitsPerItem: number;
|
|
27
|
+
readonly words: readonly string[];
|
|
28
|
+
}
|
|
29
|
+
export interface EncodeBinaryLexiconOptions {
|
|
30
|
+
readonly unigrams: readonly BinaryUnigramInput[];
|
|
31
|
+
readonly bigrams: readonly BinaryBigramInput[];
|
|
32
|
+
readonly bloomTiers?: readonly BinaryBloomTierInput[];
|
|
33
|
+
readonly blockSize?: number;
|
|
34
|
+
readonly exactCountMax?: number;
|
|
35
|
+
readonly countBuckets?: number;
|
|
36
|
+
readonly countMaxVal?: number;
|
|
37
|
+
readonly capMax?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Encodes unigrams, bigrams, and optional Bloom tail filters into an `ALX1` binary payload.
|
|
41
|
+
*/
|
|
42
|
+
export declare function encodeBinaryLexicon(options: EncodeBinaryLexiconOptions): Uint8Array;
|
|
43
|
+
/**
|
|
44
|
+
* Memory-compact, zero-string-allocation runtime lexicon backed directly by `ALX1` typed arrays.
|
|
45
|
+
*/
|
|
46
|
+
export declare class BinaryLexicon {
|
|
47
|
+
private readonly blockSize;
|
|
48
|
+
private readonly wordCount;
|
|
49
|
+
private readonly totalFormCount;
|
|
50
|
+
private readonly totalPairCount;
|
|
51
|
+
private readonly capMax;
|
|
52
|
+
private readonly quantTable;
|
|
53
|
+
private readonly alphabetSize;
|
|
54
|
+
private readonly charToByte;
|
|
55
|
+
private readonly byteToChar;
|
|
56
|
+
private readonly prefixLens;
|
|
57
|
+
private readonly suffixLens;
|
|
58
|
+
private readonly chars;
|
|
59
|
+
private readonly counts;
|
|
60
|
+
private readonly caps;
|
|
61
|
+
private readonly blkCharOff;
|
|
62
|
+
private readonly bktLo;
|
|
63
|
+
private readonly bktHi;
|
|
64
|
+
private readonly byFirst;
|
|
65
|
+
private readonly bySecond;
|
|
66
|
+
private readonly blooms;
|
|
67
|
+
private readonly queryBuf;
|
|
68
|
+
private lastForm;
|
|
69
|
+
private lastId;
|
|
70
|
+
constructor(bytes: Uint8Array);
|
|
71
|
+
get size(): number;
|
|
72
|
+
get exactSize(): number;
|
|
73
|
+
get pairCount(): number;
|
|
74
|
+
private findExactId;
|
|
75
|
+
count(form: string): number;
|
|
76
|
+
/** Returns the exact-table count only (skipping protection-only Bloom filters). */
|
|
77
|
+
exactCount(form: string): number;
|
|
78
|
+
capitalShare(form: string): number;
|
|
79
|
+
pair(first: string, second: string): number;
|
|
80
|
+
/** Reconstructs the exact-table entries as TSV (for debugging/diagnostics). */
|
|
81
|
+
toTsv(): {
|
|
82
|
+
unigrams: string;
|
|
83
|
+
bigrams: string;
|
|
84
|
+
};
|
|
85
|
+
}
|