tinyfix-tr 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +139 -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 +53 -0
- package/dist/pkg/src/data.js +498 -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,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turkish phonological helpers, phonetic attribute bitmasks, and suffix template surface generation.
|
|
3
|
+
* Adapted from Zemberek's TurkishAlphabet, AttributesHelper, and SurfaceTransition (Apache 2.0).
|
|
4
|
+
*/
|
|
5
|
+
import type { SuffixTemplateToken } from "./types.ts";
|
|
6
|
+
export declare const PA_LAST_VOWEL: number;
|
|
7
|
+
export declare const PA_LAST_CONSONANT: number;
|
|
8
|
+
export declare const PA_VOWEL_FRONT: number;
|
|
9
|
+
export declare const PA_VOWEL_BACK: number;
|
|
10
|
+
export declare const PA_VOWEL_ROUND: number;
|
|
11
|
+
export declare const PA_VOWEL_UNROUND: number;
|
|
12
|
+
export declare const PA_LAST_VOICED: number;
|
|
13
|
+
export declare const PA_LAST_VOICELESS: number;
|
|
14
|
+
export declare const PA_LAST_VOICELESS_STOP: number;
|
|
15
|
+
export declare const PA_FIRST_VOWEL: number;
|
|
16
|
+
export declare const PA_FIRST_CONSONANT: number;
|
|
17
|
+
export declare const PA_HAS_NO_VOWEL: number;
|
|
18
|
+
export declare const PA_EXPECTS_VOWEL: number;
|
|
19
|
+
export declare const PA_EXPECTS_CONSONANT: number;
|
|
20
|
+
export declare const PA_CANNOT_TERMINATE: number;
|
|
21
|
+
export declare const PA_LAST_LETTER_DROPPED: number;
|
|
22
|
+
export declare const PA_MODIFIED_PRONOUN: number;
|
|
23
|
+
export declare const PA_UNMODIFIED_PRONOUN: number;
|
|
24
|
+
export declare const SURFACE_CACHE_SIZE = 64;
|
|
25
|
+
export declare const TOKEN_LETTER: 0;
|
|
26
|
+
export declare const TOKEN_A_VOWEL: 1;
|
|
27
|
+
export declare const TOKEN_I_VOWEL: 2;
|
|
28
|
+
export declare const TOKEN_APPEND: 3;
|
|
29
|
+
export declare const TOKEN_DEVOICE_FIRST: 4;
|
|
30
|
+
export declare const TOKEN_LAST_VOICED: 5;
|
|
31
|
+
export declare const TOKEN_LAST_NOT_VOICED: 6;
|
|
32
|
+
export declare function isVowel(ch: string): boolean;
|
|
33
|
+
export declare function isFrontVowel(ch: string): boolean;
|
|
34
|
+
export declare function isRoundedVowel(ch: string): boolean;
|
|
35
|
+
export declare function isVoiceless(ch: string): boolean;
|
|
36
|
+
export declare function isVoicelessStop(ch: string): boolean;
|
|
37
|
+
export declare function voiceChar(ch: string): string;
|
|
38
|
+
export declare function devoiceChar(ch: string): string;
|
|
39
|
+
export declare function countVowels(word: string): number;
|
|
40
|
+
export declare function containsVowel(word: string): boolean;
|
|
41
|
+
export declare function hasCircumflex(word: string): boolean;
|
|
42
|
+
export declare function normalizeCircumflex(word: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Packs the 6 phonetic bits that surface generation depends on into a 0..63 index.
|
|
45
|
+
*/
|
|
46
|
+
export declare function surfaceCacheKey(attrs: number): number;
|
|
47
|
+
/**
|
|
48
|
+
* Computes phonetic attributes for `seq`, transferring vowel harmony from `predecessorAttrs`
|
|
49
|
+
* if `seq` contains no vowels (e.g. consonant-only suffixes like -m, -n, -k, -t, -z).
|
|
50
|
+
*/
|
|
51
|
+
export declare function computePhoneticAttributes(seq: string, predecessorAttrs?: number): number;
|
|
52
|
+
/**
|
|
53
|
+
* Tokenizes a Zemberek-style suffix template:
|
|
54
|
+
* - `A`: low vowel harmony (a/e), elided at index 0 after a vowel
|
|
55
|
+
* - `I`: high vowel harmony (ı/i/u/ü), elided at index 0 after a vowel
|
|
56
|
+
* - `+x`: buffer letter `x`, emitted only when previous sound is a vowel
|
|
57
|
+
* - `>x`: consonant assimilation (devoiced when previous sound is voiceless)
|
|
58
|
+
* - `~x`: final unvoiced stop expecting a consonant next (e.g. -AcA~k, -dI~k, -lI~k)
|
|
59
|
+
* - `!x`: final voiced stop expecting a vowel next and cannot terminate (e.g. -AcA!ğ, -dI!ğ)
|
|
60
|
+
*/
|
|
61
|
+
export declare function tokenizeTemplate(template: string): SuffixTemplateToken[];
|
|
62
|
+
/**
|
|
63
|
+
* Generates the concrete Turkish surface string for a tokenized suffix template given
|
|
64
|
+
* the predecessor's phonetic attributes bitmask. Returns `null` if vowel harmony cannot
|
|
65
|
+
* be resolved (e.g. a vowelless root without pronunciation).
|
|
66
|
+
*/
|
|
67
|
+
export declare function generateSurface(tokens: readonly SuffixTemplateToken[], phoneticAttrs: number): string | null;
|
|
68
|
+
/**
|
|
69
|
+
* Computes the updated phonetic attributes bitmask after consuming `surface` for a suffix transition.
|
|
70
|
+
*/
|
|
71
|
+
export declare function computeNextPhoneticAttrs(surface: string, prevAttrs: number, lastToken: SuffixTemplateToken | undefined): number;
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turkish phonological helpers, phonetic attribute bitmasks, and suffix template surface generation.
|
|
3
|
+
* Adapted from Zemberek's TurkishAlphabet, AttributesHelper, and SurfaceTransition (Apache 2.0).
|
|
4
|
+
*/
|
|
5
|
+
// Phonetic attribute bitmask flags.
|
|
6
|
+
export const PA_LAST_VOWEL = 1 << 0;
|
|
7
|
+
export const PA_LAST_CONSONANT = 1 << 1;
|
|
8
|
+
export const PA_VOWEL_FRONT = 1 << 2;
|
|
9
|
+
export const PA_VOWEL_BACK = 1 << 3;
|
|
10
|
+
export const PA_VOWEL_ROUND = 1 << 4;
|
|
11
|
+
export const PA_VOWEL_UNROUND = 1 << 5;
|
|
12
|
+
export const PA_LAST_VOICED = 1 << 6;
|
|
13
|
+
export const PA_LAST_VOICELESS = 1 << 7;
|
|
14
|
+
export const PA_LAST_VOICELESS_STOP = 1 << 8;
|
|
15
|
+
export const PA_FIRST_VOWEL = 1 << 9;
|
|
16
|
+
export const PA_FIRST_CONSONANT = 1 << 10;
|
|
17
|
+
export const PA_HAS_NO_VOWEL = 1 << 11;
|
|
18
|
+
export const PA_EXPECTS_VOWEL = 1 << 12;
|
|
19
|
+
export const PA_EXPECTS_CONSONANT = 1 << 13;
|
|
20
|
+
export const PA_CANNOT_TERMINATE = 1 << 14;
|
|
21
|
+
export const PA_LAST_LETTER_DROPPED = 1 << 15;
|
|
22
|
+
export const PA_MODIFIED_PRONOUN = 1 << 16;
|
|
23
|
+
export const PA_UNMODIFIED_PRONOUN = 1 << 17;
|
|
24
|
+
export const SURFACE_CACHE_SIZE = 64;
|
|
25
|
+
// Suffix template token kinds.
|
|
26
|
+
export const TOKEN_LETTER = 0;
|
|
27
|
+
export const TOKEN_A_VOWEL = 1;
|
|
28
|
+
export const TOKEN_I_VOWEL = 2;
|
|
29
|
+
export const TOKEN_APPEND = 3;
|
|
30
|
+
export const TOKEN_DEVOICE_FIRST = 4;
|
|
31
|
+
export const TOKEN_LAST_VOICED = 5;
|
|
32
|
+
export const TOKEN_LAST_NOT_VOICED = 6;
|
|
33
|
+
const VOWELS = new Set(["a", "e", "ı", "i", "o", "ö", "u", "ü", "â", "î", "û"]);
|
|
34
|
+
const FRONT_VOWELS = new Set(["e", "i", "ö", "ü", "î"]);
|
|
35
|
+
const ROUNDED_VOWELS = new Set(["o", "ö", "u", "ü", "û"]);
|
|
36
|
+
const VOICELESS = new Set(["ç", "f", "h", "k", "p", "s", "ş", "t"]);
|
|
37
|
+
const VOICELESS_STOPS = new Set(["ç", "k", "p", "t"]);
|
|
38
|
+
const VOICE_MAP = {
|
|
39
|
+
p: "b",
|
|
40
|
+
ç: "c",
|
|
41
|
+
t: "d",
|
|
42
|
+
k: "ğ",
|
|
43
|
+
g: "ğ",
|
|
44
|
+
};
|
|
45
|
+
const DEVOICE_MAP = {
|
|
46
|
+
b: "p",
|
|
47
|
+
c: "ç",
|
|
48
|
+
d: "t",
|
|
49
|
+
g: "k",
|
|
50
|
+
};
|
|
51
|
+
export function isVowel(ch) {
|
|
52
|
+
return VOWELS.has(ch);
|
|
53
|
+
}
|
|
54
|
+
export function isFrontVowel(ch) {
|
|
55
|
+
return FRONT_VOWELS.has(ch);
|
|
56
|
+
}
|
|
57
|
+
export function isRoundedVowel(ch) {
|
|
58
|
+
return ROUNDED_VOWELS.has(ch);
|
|
59
|
+
}
|
|
60
|
+
export function isVoiceless(ch) {
|
|
61
|
+
return VOICELESS.has(ch);
|
|
62
|
+
}
|
|
63
|
+
export function isVoicelessStop(ch) {
|
|
64
|
+
return VOICELESS_STOPS.has(ch);
|
|
65
|
+
}
|
|
66
|
+
export function voiceChar(ch) {
|
|
67
|
+
return VOICE_MAP[ch] ?? ch;
|
|
68
|
+
}
|
|
69
|
+
export function devoiceChar(ch) {
|
|
70
|
+
return DEVOICE_MAP[ch] ?? ch;
|
|
71
|
+
}
|
|
72
|
+
export function countVowels(word) {
|
|
73
|
+
let count = 0;
|
|
74
|
+
for (let i = 0; i < word.length; i++) {
|
|
75
|
+
const ch = word[i];
|
|
76
|
+
if (ch !== undefined && VOWELS.has(ch)) {
|
|
77
|
+
count++;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return count;
|
|
81
|
+
}
|
|
82
|
+
export function containsVowel(word) {
|
|
83
|
+
for (let i = 0; i < word.length; i++) {
|
|
84
|
+
const ch = word[i];
|
|
85
|
+
if (ch !== undefined && VOWELS.has(ch)) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
export function hasCircumflex(word) {
|
|
92
|
+
for (let i = 0; i < word.length; i++) {
|
|
93
|
+
const code = word.charCodeAt(i);
|
|
94
|
+
// â (226), î (238), û (251)
|
|
95
|
+
if (code === 226 || code === 238 || code === 251) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
export function normalizeCircumflex(word) {
|
|
102
|
+
if (!hasCircumflex(word))
|
|
103
|
+
return word;
|
|
104
|
+
let out = "";
|
|
105
|
+
for (let i = 0; i < word.length; i++) {
|
|
106
|
+
const ch = word[i] ?? "";
|
|
107
|
+
if (ch === "â")
|
|
108
|
+
out += "a";
|
|
109
|
+
else if (ch === "î")
|
|
110
|
+
out += "i";
|
|
111
|
+
else if (ch === "û")
|
|
112
|
+
out += "u";
|
|
113
|
+
else
|
|
114
|
+
out += ch;
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Packs the 6 phonetic bits that surface generation depends on into a 0..63 index.
|
|
120
|
+
*/
|
|
121
|
+
export function surfaceCacheKey(attrs) {
|
|
122
|
+
return (((attrs & PA_LAST_VOWEL) !== 0 ? 1 : 0) |
|
|
123
|
+
((attrs & PA_VOWEL_BACK) !== 0 ? 2 : 0) |
|
|
124
|
+
((attrs & PA_VOWEL_FRONT) !== 0 ? 4 : 0) |
|
|
125
|
+
((attrs & PA_VOWEL_ROUND) !== 0 ? 8 : 0) |
|
|
126
|
+
((attrs & PA_VOWEL_UNROUND) !== 0 ? 16 : 0) |
|
|
127
|
+
((attrs & PA_LAST_VOICELESS) !== 0 ? 32 : 0));
|
|
128
|
+
}
|
|
129
|
+
const VOWEL_HARMONY_MASK = PA_VOWEL_FRONT | PA_VOWEL_BACK | PA_VOWEL_ROUND | PA_VOWEL_UNROUND;
|
|
130
|
+
/**
|
|
131
|
+
* Computes phonetic attributes for `seq`, transferring vowel harmony from `predecessorAttrs`
|
|
132
|
+
* if `seq` contains no vowels (e.g. consonant-only suffixes like -m, -n, -k, -t, -z).
|
|
133
|
+
*/
|
|
134
|
+
export function computePhoneticAttributes(seq, predecessorAttrs = 0) {
|
|
135
|
+
if (seq.length === 0) {
|
|
136
|
+
return predecessorAttrs;
|
|
137
|
+
}
|
|
138
|
+
let lastVowel = "";
|
|
139
|
+
for (let i = seq.length - 1; i >= 0; i--) {
|
|
140
|
+
const ch = seq[i];
|
|
141
|
+
if (ch !== undefined && VOWELS.has(ch)) {
|
|
142
|
+
lastVowel = ch;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const first = seq[0] ?? "";
|
|
147
|
+
const last = seq[seq.length - 1] ?? "";
|
|
148
|
+
let attrs = 0;
|
|
149
|
+
if (lastVowel.length > 0) {
|
|
150
|
+
attrs |= VOWELS.has(last) ? PA_LAST_VOWEL : PA_LAST_CONSONANT;
|
|
151
|
+
attrs |= FRONT_VOWELS.has(lastVowel) ? PA_VOWEL_FRONT : PA_VOWEL_BACK;
|
|
152
|
+
attrs |= ROUNDED_VOWELS.has(lastVowel) ? PA_VOWEL_ROUND : PA_VOWEL_UNROUND;
|
|
153
|
+
attrs |= VOWELS.has(first) ? PA_FIRST_VOWEL : PA_FIRST_CONSONANT;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
// Transfer only vowel harmony attributes from predecessor; recompute consonant/voicing bits cleanly.
|
|
157
|
+
attrs = (predecessorAttrs & VOWEL_HARMONY_MASK) | PA_LAST_CONSONANT | PA_FIRST_CONSONANT | PA_HAS_NO_VOWEL;
|
|
158
|
+
}
|
|
159
|
+
if (VOICELESS.has(last)) {
|
|
160
|
+
attrs |= PA_LAST_VOICELESS;
|
|
161
|
+
if (VOICELESS_STOPS.has(last)) {
|
|
162
|
+
attrs |= PA_LAST_VOICELESS_STOP;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
attrs |= PA_LAST_VOICED;
|
|
167
|
+
}
|
|
168
|
+
return attrs;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Tokenizes a Zemberek-style suffix template:
|
|
172
|
+
* - `A`: low vowel harmony (a/e), elided at index 0 after a vowel
|
|
173
|
+
* - `I`: high vowel harmony (ı/i/u/ü), elided at index 0 after a vowel
|
|
174
|
+
* - `+x`: buffer letter `x`, emitted only when previous sound is a vowel
|
|
175
|
+
* - `>x`: consonant assimilation (devoiced when previous sound is voiceless)
|
|
176
|
+
* - `~x`: final unvoiced stop expecting a consonant next (e.g. -AcA~k, -dI~k, -lI~k)
|
|
177
|
+
* - `!x`: final voiced stop expecting a vowel next and cannot terminate (e.g. -AcA!ğ, -dI!ğ)
|
|
178
|
+
*/
|
|
179
|
+
export function tokenizeTemplate(template) {
|
|
180
|
+
const tokens = [];
|
|
181
|
+
let i = 0;
|
|
182
|
+
while (i < template.length) {
|
|
183
|
+
const ch = template[i] ?? "";
|
|
184
|
+
i++;
|
|
185
|
+
if (ch === "+") {
|
|
186
|
+
const next = template[i] ?? "";
|
|
187
|
+
i++;
|
|
188
|
+
if (next === "I") {
|
|
189
|
+
tokens.push({ type: TOKEN_I_VOWEL, letter: "" });
|
|
190
|
+
}
|
|
191
|
+
else if (next === "A") {
|
|
192
|
+
tokens.push({ type: TOKEN_A_VOWEL, letter: "" });
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
tokens.push({ type: TOKEN_APPEND, letter: next });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else if (ch === ">") {
|
|
199
|
+
const next = template[i] ?? "";
|
|
200
|
+
i++;
|
|
201
|
+
tokens.push({ type: TOKEN_DEVOICE_FIRST, letter: next });
|
|
202
|
+
}
|
|
203
|
+
else if (ch === "~") {
|
|
204
|
+
const next = template[i] ?? "";
|
|
205
|
+
i++;
|
|
206
|
+
tokens.push({ type: TOKEN_LAST_VOICED, letter: next });
|
|
207
|
+
}
|
|
208
|
+
else if (ch === "!") {
|
|
209
|
+
const next = template[i] ?? "";
|
|
210
|
+
i++;
|
|
211
|
+
tokens.push({ type: TOKEN_LAST_NOT_VOICED, letter: next });
|
|
212
|
+
}
|
|
213
|
+
else if (ch === "I") {
|
|
214
|
+
tokens.push({ type: TOKEN_I_VOWEL, letter: "" });
|
|
215
|
+
}
|
|
216
|
+
else if (ch === "A") {
|
|
217
|
+
tokens.push({ type: TOKEN_A_VOWEL, letter: "" });
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
tokens.push({ type: TOKEN_LETTER, letter: ch });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return tokens;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Generates the concrete Turkish surface string for a tokenized suffix template given
|
|
227
|
+
* the predecessor's phonetic attributes bitmask. Returns `null` if vowel harmony cannot
|
|
228
|
+
* be resolved (e.g. a vowelless root without pronunciation).
|
|
229
|
+
*/
|
|
230
|
+
export function generateSurface(tokens, phoneticAttrs) {
|
|
231
|
+
let sb = "";
|
|
232
|
+
let attrs = phoneticAttrs;
|
|
233
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
234
|
+
const token = tokens[index];
|
|
235
|
+
if (token === undefined)
|
|
236
|
+
break;
|
|
237
|
+
if (sb.length > 0) {
|
|
238
|
+
attrs = computePhoneticAttributes(sb, phoneticAttrs);
|
|
239
|
+
}
|
|
240
|
+
switch (token.type) {
|
|
241
|
+
case TOKEN_LETTER:
|
|
242
|
+
case TOKEN_LAST_VOICED:
|
|
243
|
+
case TOKEN_LAST_NOT_VOICED:
|
|
244
|
+
sb += token.letter;
|
|
245
|
+
break;
|
|
246
|
+
case TOKEN_A_VOWEL:
|
|
247
|
+
if (index === 0 && (phoneticAttrs & PA_LAST_VOWEL) !== 0) {
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
if ((attrs & PA_VOWEL_BACK) !== 0) {
|
|
251
|
+
sb += "a";
|
|
252
|
+
}
|
|
253
|
+
else if ((attrs & PA_VOWEL_FRONT) !== 0) {
|
|
254
|
+
sb += "e";
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
break;
|
|
260
|
+
case TOKEN_I_VOWEL:
|
|
261
|
+
if (index === 0 && (phoneticAttrs & PA_LAST_VOWEL) !== 0) {
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
if ((attrs & PA_VOWEL_FRONT) !== 0 && (attrs & PA_VOWEL_UNROUND) !== 0) {
|
|
265
|
+
sb += "i";
|
|
266
|
+
}
|
|
267
|
+
else if ((attrs & PA_VOWEL_BACK) !== 0 && (attrs & PA_VOWEL_UNROUND) !== 0) {
|
|
268
|
+
sb += "ı";
|
|
269
|
+
}
|
|
270
|
+
else if ((attrs & PA_VOWEL_BACK) !== 0 && (attrs & PA_VOWEL_ROUND) !== 0) {
|
|
271
|
+
sb += "u";
|
|
272
|
+
}
|
|
273
|
+
else if ((attrs & PA_VOWEL_FRONT) !== 0 && (attrs & PA_VOWEL_ROUND) !== 0) {
|
|
274
|
+
sb += "ü";
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
case TOKEN_APPEND:
|
|
281
|
+
if ((attrs & PA_LAST_VOWEL) !== 0) {
|
|
282
|
+
sb += token.letter;
|
|
283
|
+
}
|
|
284
|
+
break;
|
|
285
|
+
case TOKEN_DEVOICE_FIRST: {
|
|
286
|
+
const ch = (attrs & PA_LAST_VOICELESS) !== 0 ? devoiceChar(token.letter) : token.letter;
|
|
287
|
+
sb += ch;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return sb;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Computes the updated phonetic attributes bitmask after consuming `surface` for a suffix transition.
|
|
296
|
+
*/
|
|
297
|
+
export function computeNextPhoneticAttrs(surface, prevAttrs, lastToken) {
|
|
298
|
+
let attrs = computePhoneticAttributes(surface, prevAttrs);
|
|
299
|
+
attrs &= ~PA_CANNOT_TERMINATE;
|
|
300
|
+
if (lastToken !== undefined) {
|
|
301
|
+
if (lastToken.type === TOKEN_LAST_VOICED) {
|
|
302
|
+
attrs |= PA_EXPECTS_CONSONANT;
|
|
303
|
+
}
|
|
304
|
+
else if (lastToken.type === TOKEN_LAST_NOT_VOICED) {
|
|
305
|
+
attrs |= PA_EXPECTS_VOWEL | PA_CANNOT_TERMINATE;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return attrs;
|
|
309
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
import type { DictionaryItem, MorphAnalysis } from "./types.ts";
|
|
9
|
+
export interface MorphPriorData {
|
|
10
|
+
readonly lemmaCounts: ReadonlyMap<string, number>;
|
|
11
|
+
readonly chainProbs: ReadonlyMap<string, number>;
|
|
12
|
+
readonly bigramProbs: ReadonlyMap<string, number>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Runtime morphological generative prior: estimates the expected corpus frequency of any
|
|
16
|
+
* morphologically valid Turkish word from its lemma frequency and suffix-chain probability.
|
|
17
|
+
*/
|
|
18
|
+
export declare class MorphPrior {
|
|
19
|
+
private readonly lemmaCounts;
|
|
20
|
+
private readonly stemTotals;
|
|
21
|
+
private readonly chainProbs;
|
|
22
|
+
private readonly bigramProbs;
|
|
23
|
+
constructor(data: MorphPriorData);
|
|
24
|
+
/** Total corpus frequency of `(stem, pos)` with a conservative cross-POS fallback for homographs. */
|
|
25
|
+
lemmaCount(stem: string, pos: string): number;
|
|
26
|
+
/** Corpus frequency of `stem` across all parts of speech (for prioritizing fuzzy stem search). */
|
|
27
|
+
stemTotal(stem: string): number;
|
|
28
|
+
/** Probability `P(morphemes | pos)` using exact stored chain probability or morpheme-bigram backoff. */
|
|
29
|
+
chainProb(pos: string, morphemes: readonly string[]): number;
|
|
30
|
+
/**
|
|
31
|
+
* Expected corpus count of a valid word form across its morphological analyses.
|
|
32
|
+
* Returns 0 if `analyses` is empty.
|
|
33
|
+
*/
|
|
34
|
+
expectedCount(analyses: readonly MorphAnalysis[]): number;
|
|
35
|
+
get chainSize(): number;
|
|
36
|
+
get bigramSize(): number;
|
|
37
|
+
get lemmaSize(): number;
|
|
38
|
+
/** Raw maps for binary serialization. */
|
|
39
|
+
toData(): MorphPriorData;
|
|
40
|
+
}
|
|
41
|
+
interface AnalyzerLike {
|
|
42
|
+
analyze(form: string): readonly MorphAnalysis[];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Extracts lemma frequencies and register-balanced suffix-chain + morpheme-bigram probabilities
|
|
46
|
+
* from `unigrams_conv.tsv` and (optionally) `subtitle_counts.tsv`.
|
|
47
|
+
*/
|
|
48
|
+
export declare function buildMorphPriorFromCorpora(analyzer: AnalyzerLike, unigramsConvTsv: string, subtitleTsv?: string, options?: {
|
|
49
|
+
minChainTypes?: number;
|
|
50
|
+
minChainCount?: number;
|
|
51
|
+
subtitleWeight?: number;
|
|
52
|
+
}): MorphPrior;
|
|
53
|
+
/**
|
|
54
|
+
* Serializes a `MorphPrior` into a compact `MST1` binary block aligned with sorted `DictionaryItem[]`.
|
|
55
|
+
*/
|
|
56
|
+
export declare function encodeMorphPrior(prior: MorphPrior, sortedItems: readonly DictionaryItem[]): Uint8Array;
|
|
57
|
+
/**
|
|
58
|
+
* Decodes an `MST1` binary block starting at `startOffset` in `bytes`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function decodeMorphPrior(bytes: Uint8Array, startOffset: number, sortedItems: readonly DictionaryItem[]): MorphPrior;
|
|
61
|
+
export {};
|