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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Russian Base Phonetic Rules
// ===========================
// Phonetic rules for Standard Russian (Русский язык).
// Maps Cyrillic script to Latin phonetic representations.
//
// Key features:
//   - Cyrillic to Latin transliteration
//   - Vowel reduction (о in unstressed positions)
//   - Final devoicing
//   - Soft consonants (before ь and soft vowels)
//   - Iotated vowels (е, ё, ю, я → ye, yo, yu, ya)
//
// Rule IDs: 2000-2099

@name "Russian Base Phonetic Rules"
@version "1.0.0"
@author "liblevenshtein"
@description "Phonetic normalization rules for Standard Russian"

// ============================================================
// MULTI-CHARACTER PATTERNS (highest priority)
// ============================================================

// ЩЩ double consonant → ɕː (long palatal fricative)
[id: 2000, name: "щщ to ɕː", weight: 0.05, group: russian_doubles, ipa: "/ɕː/"]
щщ -> ɕː;

// ЖЖ double consonant → ʒ
[id: 2001, name: "жж to ʒ", weight: 0.05, group: russian_doubles, ipa: "/ʒ/"]
жж -> ʒ;

// ШШ double consonant → ʃ
[id: 2002, name: "шш to ʃ", weight: 0.05, group: russian_doubles, ipa: "/ʃ/"]
шш -> ʃ;

// ЦЦ double consonant → t͡s
[id: 2003, name: "цц to t͡s", weight: 0.05, group: russian_doubles, ipa: "/t͡s/"]
цц -> t͡s;

// ЧЧ double consonant → t͡ʃ
[id: 2004, name: "чч to t͡ʃ", weight: 0.05, group: russian_doubles, ipa: "/t͡ʃ/"]
чч -> t͡ʃ;

// ============================================================
// COMPLEX CONSONANTS (before simple consonants)
// ============================================================

// Щ → ɕː (voiceless alveolo-palatal fricative, long)
[id: 2010, name: "щ to ɕː", weight: 0.1, group: russian_complex, ipa: "/ɕː/"]
щ -> ɕː;

[id: 2011, name: "Щ to ɕː", weight: 0.1, group: russian_complex, ipa: "/ɕː/"]
Щ -> ɕː;

// Ж → ʒ (voiced postalveolar fricative)
[id: 2012, name: "ж to ʒ", weight: 0.1, group: russian_complex, ipa: "/ʒ/"]
ж -> ʒ;

[id: 2013, name: "Ж to ʒ", weight: 0.1, group: russian_complex, ipa: "/ʒ/"]
Ж -> ʒ;

// Ш → ʃ (voiceless postalveolar fricative)
[id: 2014, name: "ш to ʃ", weight: 0.1, group: russian_complex, ipa: "/ʃ/"]
ш -> ʃ;

[id: 2015, name: "Ш to ʃ", weight: 0.1, group: russian_complex, ipa: "/ʃ/"]
Ш -> ʃ;

// Х → x (voiceless velar fricative)
[id: 2016, name: "х to x", weight: 0.1, group: russian_complex, ipa: "/x/"]
х -> x;

[id: 2017, name: "Х to x", weight: 0.1, group: russian_complex, ipa: "/x/"]
Х -> x;

// Ц → t͡s (voiceless alveolar affricate)
[id: 2018, name: "ц to t͡s", weight: 0.1, group: russian_complex, ipa: "/t͡s/"]
ц -> t͡s;

[id: 2019, name: "Ц to t͡s", weight: 0.1, group: russian_complex, ipa: "/t͡s/"]
Ц -> t͡s;

// Ч → t͡ʃ (voiceless postalveolar affricate)
[id: 2020, name: "ч to t͡ʃ", weight: 0.1, group: russian_complex, ipa: "/t͡ʃ/"]
ч -> t͡ʃ;

[id: 2021, name: "Ч to t͡ʃ", weight: 0.1, group: russian_complex, ipa: "/t͡ʃ/"]
Ч -> t͡ʃ;

// ============================================================
// IOTATED VOWELS (vowels with initial /j/)
// ============================================================
// These vowels contain a "y" sound at the start

// Е → je (or e after consonants - simplified to je for matching)
[id: 2025, name: "е to je", weight: 0.15, group: russian_iotated, ipa: "/je/"]
е -> je;

[id: 2026, name: "Е to je", weight: 0.15, group: russian_iotated, ipa: "/je/"]
Е -> je;

// Ё → jo (always stressed, always iotated)
[id: 2027, name: "ё to jo", weight: 0.15, group: russian_iotated, ipa: "/jo/"]
ё -> jo;

[id: 2028, name: "Ё to jo", weight: 0.15, group: russian_iotated, ipa: "/jo/"]
Ё -> jo;

// Ю → ju
[id: 2029, name: "ю to ju", weight: 0.15, group: russian_iotated, ipa: "/ju/"]
ю -> ju;

[id: 2030, name: "Ю to ju", weight: 0.15, group: russian_iotated, ipa: "/ju/"]
Ю -> ju;

// Я → ja
[id: 2031, name: "я to ja", weight: 0.15, group: russian_iotated, ipa: "/ja/"]
я -> ja;

[id: 2032, name: "Я to ja", weight: 0.15, group: russian_iotated, ipa: "/ja/"]
Я -> ja;

// ============================================================
// SIMPLE VOWELS
// ============================================================

// А → a
[id: 2035, name: "а to a", weight: 0.2, group: russian_vowels]
а -> a;

[id: 2036, name: "А to a", weight: 0.2, group: russian_vowels]
А -> a;

// Э → e (open e, like "bed")
[id: 2037, name: "э to e", weight: 0.2, group: russian_vowels]
э -> e;

[id: 2038, name: "Э to e", weight: 0.2, group: russian_vowels]
Э -> e;

// И → i
[id: 2039, name: "и to i", weight: 0.2, group: russian_vowels]
и -> i;

[id: 2040, name: "И to i", weight: 0.2, group: russian_vowels]
И -> i;

// О → o
[id: 2041, name: "о to o", weight: 0.2, group: russian_vowels]
о -> o;

[id: 2042, name: "О to o", weight: 0.2, group: russian_vowels]
О -> o;

// У → u
[id: 2043, name: "у to u", weight: 0.2, group: russian_vowels]
у -> u;

[id: 2044, name: "У to u", weight: 0.2, group: russian_vowels]
У -> u;

// Ы → ɨ (unique Russian vowel, close central unrounded)
[id: 2045, name: "ы to ɨ", weight: 0.2, group: russian_vowels, ipa: "/ɨ/"]
ы -> ɨ;

[id: 2046, name: "Ы to ɨ", weight: 0.2, group: russian_vowels, ipa: "/ɨ/"]
Ы -> ɨ;

// ============================================================
// SIMPLE CONSONANTS
// ============================================================

// Б → b
[id: 2050, name: "б to b", weight: 0.25, group: russian_consonants]
б -> b;

[id: 2051, name: "Б to b", weight: 0.25, group: russian_consonants]
Б -> b;

// В → v
[id: 2052, name: "в to v", weight: 0.25, group: russian_consonants]
в -> v;

[id: 2053, name: "В to v", weight: 0.25, group: russian_consonants]
В -> v;

// Г → ɡ
[id: 2054, name: "г to ɡ", weight: 0.25, group: russian_consonants, ipa: "/ɡ/"]
г -> ɡ;

[id: 2055, name: "Г to ɡ", weight: 0.25, group: russian_consonants, ipa: "/ɡ/"]
Г -> ɡ;

// Д → d
[id: 2056, name: "д to d", weight: 0.25, group: russian_consonants]
д -> d;

[id: 2057, name: "Д to d", weight: 0.25, group: russian_consonants]
Д -> d;

// З → z
[id: 2058, name: "з to z", weight: 0.25, group: russian_consonants]
з -> z;

[id: 2059, name: "З to z", weight: 0.25, group: russian_consonants]
З -> z;

// Й → j (palatal approximant)
[id: 2060, name: "й to j", weight: 0.25, group: russian_consonants, ipa: "/j/"]
й -> j;

[id: 2061, name: "Й to j", weight: 0.25, group: russian_consonants, ipa: "/j/"]
Й -> j;

// К → k
[id: 2062, name: "к to k", weight: 0.25, group: russian_consonants]
к -> k;

[id: 2063, name: "К to k", weight: 0.25, group: russian_consonants]
К -> k;

// Л → l
[id: 2064, name: "л to l", weight: 0.25, group: russian_consonants]
л -> l;

[id: 2065, name: "Л to l", weight: 0.25, group: russian_consonants]
Л -> l;

// М → m
[id: 2066, name: "м to m", weight: 0.25, group: russian_consonants]
м -> m;

[id: 2067, name: "М to m", weight: 0.25, group: russian_consonants]
М -> m;

// Н → n
[id: 2068, name: "н to n", weight: 0.25, group: russian_consonants]
н -> n;

[id: 2069, name: "Н to n", weight: 0.25, group: russian_consonants]
Н -> n;

// П → p
[id: 2070, name: "п to p", weight: 0.25, group: russian_consonants]
п -> p;

[id: 2071, name: "П to p", weight: 0.25, group: russian_consonants]
П -> p;

// Р → r
[id: 2072, name: "р to r", weight: 0.25, group: russian_consonants]
р -> r;

[id: 2073, name: "Р to r", weight: 0.25, group: russian_consonants]
Р -> r;

// С → s
[id: 2074, name: "с to s", weight: 0.25, group: russian_consonants]
с -> s;

[id: 2075, name: "С to s", weight: 0.25, group: russian_consonants]
С -> s;

// Т → t
[id: 2076, name: "т to t", weight: 0.25, group: russian_consonants]
т -> t;

[id: 2077, name: "Т to t", weight: 0.25, group: russian_consonants]
Т -> t;

// Ф → f
[id: 2078, name: "ф to f", weight: 0.25, group: russian_consonants]
ф -> f;

[id: 2079, name: "Ф to f", weight: 0.25, group: russian_consonants]
Ф -> f;

// ============================================================
// SOFT AND HARD SIGNS
// ============================================================
// These modify the preceding consonant but have no sound of their own

// Ь (soft sign) → remove (marks palatalization)
[id: 2085, name: "soft sign remove", weight: 0.3, group: russian_signs]
ь -> ;

[id: 2086, name: "Soft sign remove", weight: 0.3, group: russian_signs]
Ь -> ;

// Ъ (hard sign) → remove (marks separation, no longer used much)
[id: 2087, name: "hard sign remove", weight: 0.3, group: russian_signs]
ъ -> ;

[id: 2088, name: "Hard sign remove", weight: 0.3, group: russian_signs]
Ъ -> ;

// ============================================================
// FINAL DEVOICING (at word end)
// ============================================================
// Voiced consonants become voiceless at word end
// Must run BEFORE transliteration (lower weight = earlier)

[id: 2090, name: "final б to p", weight: 0.08, group: russian_devoicing]
б -> p / _#;

[id: 2091, name: "final в to f", weight: 0.08, group: russian_devoicing]
в -> f / _#;

[id: 2092, name: "final г to k", weight: 0.08, group: russian_devoicing]
г -> k / _#;

[id: 2093, name: "final д to t", weight: 0.08, group: russian_devoicing]
д -> t / _#;

[id: 2094, name: "final ж to ʃ", weight: 0.08, group: russian_devoicing, ipa: "/ʃ/"]
ж -> ʃ / _#;

[id: 2095, name: "final з to s", weight: 0.08, group: russian_devoicing]
з -> s / _#;

// ============================================================
// DOUBLE CONSONANT SIMPLIFICATION (Latin output)
// ============================================================
// After transliteration, simplify double Latin consonants

[id: 2096, name: "bb to b", weight: 0.4, group: russian_latin_doubles]
bb -> b;

[id: 2097, name: "dd to d", weight: 0.4, group: russian_latin_doubles]
dd -> d;

[id: 2098, name: "ff to f", weight: 0.4, group: russian_latin_doubles]
ff -> f;

[id: 2099, name: "kk to k", weight: 0.4, group: russian_latin_doubles]
kk -> k;

// ============================================================
// VOWEL REDUCTION (Akan'e and Ikan'e)
// ============================================================
// In Standard (Moscow) Russian, vowels reduce in unstressed positions:
// - Akan'e (аканье): /о/ → [ɐ] unstressed (sounds like reduced 'a')
// - Ikan'e (иканье): /е/, /я/ → [ɪ] unstressed (sounds like reduced 'i')
//
// These rules provide alternative phonetic representations for reduced vowels.
// Since stress is not marked in standard Russian text, we provide both
// the full and reduced forms as alternatives.

// о → ɐ (unstressed 'o' reduces to near-open central vowel)
[id: 2800, name: "akan'e o reduction", weight: 0.15, group: russian_reduction, ipa: "/ɐ/"]
o -> ɐ;

// е (after transliteration: je or e) → ɪ (unstressed 'e' reduces)
[id: 2801, name: "ikan'e je reduction", weight: 0.15, group: russian_reduction, ipa: "/ɪ/"]
je -> ɪ;

// е after consonants becomes just e, which also reduces
[id: 2802, name: "ikan'e e reduction", weight: 0.15, group: russian_reduction, ipa: "/ɪ/"]
e -> ɪ;

// я (after transliteration: ja) → ɪ (unstressed 'ya' reduces to 'i'-like)
[id: 2803, name: "ikan'e ja reduction", weight: 0.15, group: russian_reduction, ipa: "/ɪ/"]
ja -> ɪ;

// а → ɐ (unstressed 'a' also reduces to near-open central)
// In pretonic (immediately before stress) position, this is common
[id: 2804, name: "a reduction", weight: 0.18, group: russian_reduction, ipa: "/ɐ/"]
a -> ɐ;

// ============================================================
// BEIDER-MORSE SLAVIC RULES
// ============================================================
// Additional rules derived from the Beider-Morse Phonetic Matching
// algorithm for Russian and other Slavic languages.
// ID Range: 3400-3499

@define VOWEL_RU = [aeiouɐɨɪ]
@define CONSONANT_RU = [bcdfghjklmnpqrstvwxz]

// ------------------------------------------------------------
// Regressive Voicing Assimilation
// ------------------------------------------------------------
// Russian has regressive voicing assimilation like Polish

[id: 3400, name: "vk to fk russian", weight: 0.10, group: bm_russian_assimilation]
vk -> fk;

[id: 3401, name: "vp to fp russian", weight: 0.10, group: bm_russian_assimilation]
vp -> fp;

[id: 3402, name: "vt to ft russian", weight: 0.10, group: bm_russian_assimilation]
vt -> ft;

[id: 3403, name: "vs to fs russian", weight: 0.10, group: bm_russian_assimilation]
vs -> fs;

[id: 3404, name: "zk to sk russian", weight: 0.10, group: bm_russian_assimilation]
zk -> sk;

[id: 3405, name: "zp to sp russian", weight: 0.10, group: bm_russian_assimilation]
zp -> sp;

[id: 3406, name: "zt to st russian", weight: 0.10, group: bm_russian_assimilation]
zt -> st;

[id: 3407, name: "zs to ss russian", weight: 0.10, group: bm_russian_assimilation]
zs -> ss;

[id: 3408, name: "bk to pk russian", weight: 0.10, group: bm_russian_assimilation]
bk -> pk;

[id: 3409, name: "dk to tk russian", weight: 0.10, group: bm_russian_assimilation]
dk -> tk;

[id: 3410, name: "gk to kk russian", weight: 0.10, group: bm_russian_assimilation]
gk -> kk;

// Progressive assimilation (voiceless before voiced)
[id: 3411, name: "kb to gb russian", weight: 0.10, group: bm_russian_assimilation]
kb -> gb;

[id: 3412, name: "sb to zb russian", weight: 0.10, group: bm_russian_assimilation]
sb -> zb;

[id: 3413, name: "sd to zd russian", weight: 0.10, group: bm_russian_assimilation]
sd -> zd;

// ------------------------------------------------------------
// G Weakening Patterns
// ------------------------------------------------------------
// In some positions, г becomes в or х

[id: 3420, name: "ogo to ovo russian", weight: 0.15, group: bm_russian_g]
// -ОГО genitive -> pronounced as -ОВО (его, того, красного)
ogo -> ovo;

[id: 3421, name: "ego to evo russian", weight: 0.15, group: bm_russian_g]
// -ЕГО genitive
ego -> evo;

[id: 3422, name: "ogo to ava russian", weight: 0.20, group: bm_russian_g]
// -АГО old spelling
ago -> avo;

// ------------------------------------------------------------
// Palatalization Patterns
// ------------------------------------------------------------
// Consonants before soft vowels are palatalized

[id: 3425, name: "ti palatalization", weight: 0.10, group: bm_russian_palatal, ipa: "/tʲi/"]
// T before I: palatalized
ti -> tʲi;

[id: 3426, name: "di palatalization", weight: 0.10, group: bm_russian_palatal, ipa: "/dʲi/"]
// D before I: palatalized
di -> dʲi;

[id: 3427, name: "ni palatalization", weight: 0.10, group: bm_russian_palatal, ipa: "/ɲi/"]
// N before I: palatalized
ni -> ɲi;

[id: 3428, name: "li palatalization", weight: 0.10, group: bm_russian_palatal, ipa: "/lʲi/"]
// L before I: palatalized (different from "hard" L)
li -> lʲi;

[id: 3429, name: "si palatalization", weight: 0.10, group: bm_russian_palatal, ipa: "/ɕi/"]
// S before I: approaches ɕ
si -> ɕi;

[id: 3430, name: "zi palatalization", weight: 0.10, group: bm_russian_palatal, ipa: "/ʑi/"]
// Z before I
zi -> ʑi;

// ------------------------------------------------------------
// TS Cluster Patterns
// ------------------------------------------------------------

[id: 3435, name: "dts to ts russian", weight: 0.10, group: bm_russian_cluster, ipa: "/t͡s/"]
// ДТС cluster (as in "двадцать"): pronounced as ts
dts -> t͡s;

[id: 3436, name: "tts to ts russian", weight: 0.10, group: bm_russian_cluster, ipa: "/t͡s/"]
// ТТС cluster: simplified
tts -> t͡s;

[id: 3437, name: "cts to ts russian", weight: 0.10, group: bm_russian_cluster, ipa: "/t͡s/"]
// ЦТС: simplified
cts -> t͡s;

// ------------------------------------------------------------
// STN/ZDN Simplification
// ------------------------------------------------------------
// Some consonant clusters simplify in speech

[id: 3440, name: "stn to sn russian", weight: 0.15, group: bm_russian_simplify]
// СТН -> СН (честный -> чесный in casual speech)
stn -> sn;

[id: 3441, name: "zdn to zn russian", weight: 0.15, group: bm_russian_simplify]
// ЗДН -> ЗН (поздно -> позно in casual speech)
zdn -> zn;

[id: 3442, name: "stl to sl russian", weight: 0.15, group: bm_russian_simplify]
// СТЛ -> СЛ (счастливый)
stl -> sl;

[id: 3443, name: "rdn to rn russian", weight: 0.15, group: bm_russian_simplify]
// РДН -> РН (сердце)
rdn -> rn;

[id: 3444, name: "rdc to rc russian", weight: 0.15, group: bm_russian_simplify]
// РДЦ -> РЦ (сердце)
rdc -> rc;

[id: 3445, name: "lnc to nc russian", weight: 0.15, group: bm_russian_simplify]
// ЛНЦ -> НЦ (солнце -> сонце)
lnc -> nc;

// ------------------------------------------------------------
// Cross-Slavic Patterns
// ------------------------------------------------------------

[id: 3450, name: "w to v russian", weight: 0.20, group: bm_russian_cross]
// W -> V: Polish/German names in Russian
w -> v;

[id: 3451, name: "ow to ov russian", weight: 0.15, group: bm_russian_cross]
// -OW: German/Polish ending
ow -> ov / _#;

[id: 3452, name: "ew to ev russian", weight: 0.15, group: bm_russian_cross]
// -EW: German/Polish ending
ew -> ev / _#;

[id: 3453, name: "cz to ch russian", weight: 0.15, group: bm_russian_cross, ipa: "/t͡ʃ/"]
// CZ: Polish spelling in Russian context
cz -> t͡ʃ;

[id: 3454, name: "sz to sh russian", weight: 0.15, group: bm_russian_cross, ipa: "/ʃ/"]
// SZ: Polish spelling
sz -> ʃ;

// ------------------------------------------------------------
// Name Suffix Patterns
// ------------------------------------------------------------

[id: 3460, name: "ov name suffix", weight: 0.05, group: bm_russian_name_suffix]
// -ОВ: Most common surname ending
ov -> of / _#;

[id: 3461, name: "ev name suffix", weight: 0.05, group: bm_russian_name_suffix]
// -ЕВ: After soft consonants
ev -> ef / _#;

[id: 3462, name: "in name suffix", weight: 0.10, group: bm_russian_name_suffix]
// -ИН: Pushkin, Rasputin
in -> in / _#;

[id: 3463, name: "enko name suffix", weight: 0.10, group: bm_russian_name_suffix]
// -ЕНКО: Ukrainian names
enko -> enko / _#;

[id: 3464, name: "ko name suffix", weight: 0.10, group: bm_russian_name_suffix]
// -КО: Ukrainian names
ko -> ko / _#;

// ------------------------------------------------------------
// TSS/STS Simplification
// ------------------------------------------------------------

[id: 3470, name: "tss to ts russian", weight: 0.10, group: bm_russian_geminate, ipa: "/t͡s/"]
// Double TS simplified
tss -> t͡s;

[id: 3471, name: "sss to ss russian", weight: 0.10, group: bm_russian_geminate]
// Triple S simplified
sss -> ss;

[id: 3472, name: "mmm to mm russian", weight: 0.10, group: bm_russian_geminate]
// Triple M simplified
mmm -> mm;

[id: 3473, name: "nnn to nn russian", weight: 0.10, group: bm_russian_geminate]
// Triple N simplified
nnn -> nn;