liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Korean Base Phonetic Rules
// ==========================
// Phonetic rules for Korean (한국어) using Hangul script.
// Maps Hangul jamo to Latin phonetic representations using
// Revised Romanization of Korean (RR).
//
// Note: Korean syllable blocks are pre-composed characters.
// This file handles individual jamo (letters) which appear
// in decomposed form or as compatibility characters.
//
// Key features:
//   - Hangul jamo (consonants and vowels) to Latin
//   - Double consonants (ㄲ, ㄸ, ㅃ, ㅆ, ㅉ) → tensed consonants
//   - Vowel combinations and diphthongs
//   - Initial vs final consonant pronunciation differences
//
// Rule IDs: 2050-2149

@name "Korean Base Phonetic Rules"
@version "1.0.0"
@author "liblevenshtein"
@description "Phonetic normalization rules for Korean Hangul"

// ============================================================
// TENSE (DOUBLE) CONSONANTS ([:tense:] class) - Higher priority
// ============================================================
// Korean tense consonants (ssang) with IPA tenseness marker ͈ (U+0348)
// These need to match before single consonants

// ㄲ (ssang-giyeok) → k͈ (tense velar stop)
[id: 2050, name: "ssang giyeok tense", weight: 0.05, group: korean_tense_consonants, ipa: "/k͈/"]
ㄲ -> k͈;

// ㄸ (ssang-digeut) → t͈ (tense alveolar stop)
[id: 2051, name: "ssang digeut tense", weight: 0.05, group: korean_tense_consonants, ipa: "/t͈/"]
ㄸ -> t͈;

// ㅃ (ssang-bieup) → p͈ (tense bilabial stop)
[id: 2052, name: "ssang bieup tense", weight: 0.05, group: korean_tense_consonants, ipa: "/p͈/"]
ㅃ -> p͈;

// ㅆ (ssang-siot) → s͈ (tense alveolar fricative)
[id: 2053, name: "ssang siot tense", weight: 0.05, group: korean_tense_consonants, ipa: "/s͈/"]
ㅆ -> s͈;

// ㅉ (ssang-jieut) → t͡ɕ͈ (tense alveolo-palatal affricate)
[id: 2054, name: "ssang jieut tense", weight: 0.05, group: korean_tense_consonants, ipa: "/t͡ɕ͈/"]
ㅉ -> t͡ɕ͈;

// ============================================================
// ASPIRATED CONSONANTS ([:aspirated:] class)
// ============================================================
// These use IPA aspiration marker ʰ (U+02B0)

// ㅋ (kieuk) → kʰ (aspirated velar stop)
[id: 2060, name: "kieuk aspirated", weight: 0.1, group: korean_aspirated, ipa: "/kʰ/"]
ㅋ -> kʰ;

// ㅌ (tieut) → tʰ (aspirated alveolar stop)
[id: 2061, name: "tieut aspirated", weight: 0.1, group: korean_aspirated, ipa: "/tʰ/"]
ㅌ -> tʰ;

// ㅍ (pieup) → pʰ (aspirated bilabial stop)
[id: 2062, name: "pieup aspirated", weight: 0.1, group: korean_aspirated, ipa: "/pʰ/"]
ㅍ -> pʰ;

// ㅊ (chieut) → t͡ɕʰ (aspirated alveolo-palatal affricate)
[id: 2063, name: "chieut aspirated", weight: 0.1, group: korean_aspirated, ipa: "/t͡ɕʰ/"]
ㅊ -> t͡ɕʰ;

// ============================================================
// BASIC CONSONANTS
// ============================================================

// ㄱ (giyeok) → ɡ/k (varies by position)
[id: 2070, name: "giyeok", weight: 0.15, group: korean_consonants, ipa: "/ɡ/"]
ㄱ -> ɡ;

// ㄴ (nieun) → n
[id: 2071, name: "nieun", weight: 0.15, group: korean_consonants]
ㄴ -> n;

// ㄷ (digeut) → d/t (varies by position)
[id: 2072, name: "digeut", weight: 0.15, group: korean_consonants]
ㄷ -> d;

// ㄹ (rieul) → r/l (varies by position)
[id: 2073, name: "rieul", weight: 0.15, group: korean_consonants]
ㄹ -> r;

// ㅁ (mieum) → m
[id: 2074, name: "mieum", weight: 0.15, group: korean_consonants]
ㅁ -> m;

// ㅂ (bieup) → b/p (varies by position)
[id: 2075, name: "bieup", weight: 0.15, group: korean_consonants]
ㅂ -> b;

// ㅅ (siot) → s
[id: 2076, name: "siot", weight: 0.15, group: korean_consonants]
ㅅ -> s;

// ㅇ (ieung) → silent initially, ŋ finally
// When initial, it's a placeholder (silent)
// When final, it represents velar nasal
[id: 2077, name: "ieung", weight: 0.15, group: korean_consonants, ipa: "/ŋ/"]
ㅇ -> ŋ;

// ㅈ (jieut) → t͡ɕ (alveolo-palatal affricate)
[id: 2078, name: "jieut", weight: 0.15, group: korean_consonants, ipa: "/t͡ɕ/"]
ㅈ -> t͡ɕ;

// ㅎ (hieut) → h
[id: 2079, name: "hieut", weight: 0.15, group: korean_consonants]
ㅎ -> h;

// ============================================================
// COMPOUND FINAL CONSONANTS
// ============================================================

// ㄳ (giyeok-siot) → ks
[id: 2080, name: "giyeok siot", weight: 0.1, group: korean_compound_finals]
ㄳ -> ks;

// ㄵ (nieun-jieut) → nj
[id: 2081, name: "nieun jieut", weight: 0.1, group: korean_compound_finals]
ㄵ -> nj;

// ㄶ (nieun-hieut) → nh
[id: 2082, name: "nieun hieut", weight: 0.1, group: korean_compound_finals]
ㄶ -> nh;

// ㄺ (rieul-giyeok) → lg
[id: 2083, name: "rieul giyeok", weight: 0.1, group: korean_compound_finals]
ㄺ -> lg;

// ㄻ (rieul-mieum) → lm
[id: 2084, name: "rieul mieum", weight: 0.1, group: korean_compound_finals]
ㄻ -> lm;

// ㄼ (rieul-bieup) → lb
[id: 2085, name: "rieul bieup", weight: 0.1, group: korean_compound_finals]
ㄼ -> lb;

// ㄽ (rieul-siot) → ls
[id: 2086, name: "rieul siot", weight: 0.1, group: korean_compound_finals]
ㄽ -> ls;

// ㄾ (rieul-tieut) → lt
[id: 2087, name: "rieul tieut", weight: 0.1, group: korean_compound_finals]
ㄾ -> lt;

// ㄿ (rieul-pieup) → lp
[id: 2088, name: "rieul pieup", weight: 0.1, group: korean_compound_finals]
ㄿ -> lp;

// ㅀ (rieul-hieut) → lh
[id: 2089, name: "rieul hieut", weight: 0.1, group: korean_compound_finals]
ㅀ -> lh;

// ㅄ (bieup-siot) → ps
[id: 2090, name: "bieup siot", weight: 0.1, group: korean_compound_finals]
ㅄ -> ps;

// ============================================================
// BASIC VOWELS
// ============================================================

// ㅏ (a) → a
[id: 2100, name: "vowel a", weight: 0.2, group: korean_vowels]
ㅏ -> a;

// ㅓ (eo) → eo
[id: 2101, name: "vowel eo", weight: 0.2, group: korean_vowels]
ㅓ -> eo;

// ㅗ (o) → o
[id: 2102, name: "vowel o", weight: 0.2, group: korean_vowels]
ㅗ -> o;

// ㅜ (u) → u
[id: 2103, name: "vowel u", weight: 0.2, group: korean_vowels]
ㅜ -> u;

// ㅡ (eu) → eu
[id: 2104, name: "vowel eu", weight: 0.2, group: korean_vowels]
ㅡ -> eu;

// ㅣ (i) → i
[id: 2105, name: "vowel i", weight: 0.2, group: korean_vowels]
ㅣ -> i;

// ㅐ (ae) → ae
[id: 2106, name: "vowel ae", weight: 0.2, group: korean_vowels]
ㅐ -> ae;

// ㅔ (e) → e
[id: 2107, name: "vowel e", weight: 0.2, group: korean_vowels]
ㅔ -> e;

// ============================================================
// Y-COMBINATION VOWELS (Iotized vowels)
// ============================================================

// ㅑ (ya) → ya
[id: 2110, name: "vowel ya", weight: 0.2, group: korean_y_vowels]
ㅑ -> ya;

// ㅕ (yeo) → yeo
[id: 2111, name: "vowel yeo", weight: 0.2, group: korean_y_vowels]
ㅕ -> yeo;

// ㅛ (yo) → yo
[id: 2112, name: "vowel yo", weight: 0.2, group: korean_y_vowels]
ㅛ -> yo;

// ㅠ (yu) → yu
[id: 2113, name: "vowel yu", weight: 0.2, group: korean_y_vowels]
ㅠ -> yu;

// ㅒ (yae) → yae
[id: 2114, name: "vowel yae", weight: 0.2, group: korean_y_vowels]
ㅒ -> yae;

// ㅖ (ye) → ye
[id: 2115, name: "vowel ye", weight: 0.2, group: korean_y_vowels]
ㅖ -> ye;

// ============================================================
// W-COMBINATION VOWELS (Labial glide vowels)
// ============================================================

// ㅘ (wa) → wa
[id: 2120, name: "vowel wa", weight: 0.2, group: korean_w_vowels]
ㅘ -> wa;

// ㅙ (wae) → wae
[id: 2121, name: "vowel wae", weight: 0.2, group: korean_w_vowels]
ㅙ -> wae;

// ㅚ (oe) → oe (or we in modern pronunciation)
[id: 2122, name: "vowel oe", weight: 0.2, group: korean_w_vowels]
ㅚ -> oe;

// ㅝ (wo) → wo
[id: 2123, name: "vowel wo", weight: 0.2, group: korean_w_vowels]
ㅝ -> wo;

// ㅞ (we) → we
[id: 2124, name: "vowel we", weight: 0.2, group: korean_w_vowels]
ㅞ -> we;

// ㅟ (wi) → wi
[id: 2125, name: "vowel wi", weight: 0.2, group: korean_w_vowels]
ㅟ -> wi;

// ㅢ (ui) → ui (or i after consonants)
[id: 2126, name: "vowel ui", weight: 0.2, group: korean_w_vowels]
ㅢ -> ui;

// ============================================================
// COMMON SYLLABLE SIMPLIFICATIONS
// ============================================================
// Simplify double romanizations for easier matching

[id: 2140, name: "eo to o", weight: 0.3, group: korean_simplification]
eo -> o;

[id: 2141, name: "eu to u", weight: 0.3, group: korean_simplification]
eu -> u;

[id: 2142, name: "ae to e", weight: 0.3, group: korean_simplification]
ae -> e;

// ============================================================
// POSITION-DEPENDENT CONSONANT RULES
// ============================================================
// Korean consonants change pronunciation based on position:
// - Initial: before vowel (word-initial or syllable-initial)
// - Final: at end of syllable (batchim / 받침)
// - Intervocalic: between vowels (typically voiced)
//
// These rules run AFTER basic jamo conversion to adjust
// the IPA output based on phonetic environment.

// Define vowel set for context matching
@define KOREAN_VOWEL = [aeiou]

// ------------------------------
// ㅇ (ieung) position rules
// ------------------------------
// Initial ㅇ is silent (placeholder for vowel-initial syllables)
// Final ㅇ is /ŋ/ (already covered in basic rules)

// ieung is silent at word start (before vowel)
[id: 2150, name: "ieung initial silent", weight: 0.01, group: korean_position]
ŋ -> / # _ $KOREAN_VOWEL;

// ieung is silent between vowels (as initial of second syllable)
[id: 2151, name: "ieung intervocalic silent", weight: 0.01, group: korean_position]
ŋ -> / $KOREAN_VOWEL _ $KOREAN_VOWEL;

// ------------------------------
// ㄹ (rieul) position rules
// ------------------------------
// Initial: /r/ (flap, similar to Spanish r)
// Final: /l/ (lateral)
// Between vowels: /r/ (flap)

// rieul becomes /l/ at word end
[id: 2152, name: "rieul final l", weight: 0.01, group: korean_position]
r -> l / _ #;

// rieul becomes /l/ before consonant
[id: 2153, name: "rieul preconsonantal l", weight: 0.01, group: korean_position]
r -> l / _ [ktnmpsh];

// ------------------------------
// Coda neutralization (final position)
// ------------------------------
// In final position (batchim), many consonants neutralize:
// ㄱ, ㅋ, ㄲ → /k̚/ (unreleased k)
// ㄷ, ㅌ, ㅅ, ㅆ, ㅈ, ㅊ, ㅎ → /t̚/ (unreleased t)
// ㅂ, ㅍ → /p̚/ (unreleased p)

// ɡ/k becomes unreleased k at word end
[id: 2154, name: "velar final unreleased", weight: 0.02, group: korean_position]
ɡ -> k / _ #;

// Aspirated k also unreleases at word end
[id: 2155, name: "aspirated k final", weight: 0.02, group: korean_position]
kk -> k / _ #;

// d/t becomes unreleased t at word end
[id: 2156, name: "dental final unreleased", weight: 0.02, group: korean_position]
d -> t / _ #;

// s becomes t at word end (coda neutralization)
[id: 2157, name: "siot final t", weight: 0.02, group: korean_position]
s -> t / _ #;

// ss (double siot) also becomes t finally
[id: 2158, name: "ssang siot final t", weight: 0.02, group: korean_position]
ss -> t / _ #;

// h becomes silent or very weak at word end
[id: 2159, name: "hieut final weak", weight: 0.02, group: korean_position]
h -> / _ #;

// b/p becomes unreleased p at word end
[id: 2160, name: "bilabial final unreleased", weight: 0.02, group: korean_position]
b -> p / _ #;

// pp becomes p finally
[id: 2161, name: "ssang bieup final p", weight: 0.02, group: korean_position]
pp -> p / _ #;

// ------------------------------
// Intervocalic voicing
// ------------------------------
// Plain consonants between vowels often become voiced

// k between vowels stays as ɡ (already ɡ from basic rules)
// This is already handled since ㄱ → ɡ

// t between vowels can voice to d
[id: 2162, name: "t intervocalic voicing", weight: 0.02, group: korean_position]
t -> d / $KOREAN_VOWEL _ $KOREAN_VOWEL;

// p between vowels can voice to b
[id: 2163, name: "p intervocalic voicing", weight: 0.02, group: korean_position]
p -> b / $KOREAN_VOWEL _ $KOREAN_VOWEL;

// ------------------------------
// Palatalization before /i/, /y/
// ------------------------------
// s + i/y → ɕ (palatalized s)

// s before i becomes palatal
[id: 2164, name: "siot palatalization i", weight: 0.01, group: korean_position]
si -> ɕi;

// ss before i becomes tense palatal
[id: 2165, name: "ssang siot palatalization i", weight: 0.01, group: korean_position]
ssi -> ɕɕi;

// t + i → t͡ɕi (affrication before i)
[id: 2166, name: "digeut affrication i", weight: 0.01, group: korean_position]
ti -> t͡ɕi;

// d + i → d͡ʑi (voiced affrication before i)
[id: 2167, name: "digeut voiced affrication i", weight: 0.01, group: korean_position]
di -> d͡ʑi;

// ------------------------------
// Nasalization
// ------------------------------
// Stops become nasals before nasals

// k/ɡ before n/m becomes ŋ
[id: 2168, name: "velar nasalization", weight: 0.02, group: korean_position]
ɡ -> ŋ / _ [nm];

// t/d before n/m becomes n
[id: 2169, name: "dental nasalization", weight: 0.02, group: korean_position]
d -> n / _ [nm];

// p/b before n/m becomes m
[id: 2170, name: "bilabial nasalization", weight: 0.02, group: korean_position]
b -> m / _ [nm];