varna 1.0.0

Varna — multilingual language engine: phoneme inventories, G2P rules, scripts, grammar, and lexicon for 50+ languages
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
//! Integration tests for varna — cross-module behavior.

use std::borrow::Cow;

use varna::grammar::{GrammarProfile, Morphology, WordOrder};
use varna::lexicon::{LexEntry, Lexicon, PartOfSpeech};
use varna::phoneme::{self, Backness, Height, Manner, Phoneme, PhonemeKind, Place};
use varna::script::{Direction, Script, ScriptStatus, ScriptType};

// ---------------------------------------------------------------------------
// Serde roundtrips
// ---------------------------------------------------------------------------

#[test]
fn test_english_phoneme_serde_roundtrip() {
    let en = phoneme::english();
    let json = serde_json::to_string(&en).unwrap();
    let deserialized: phoneme::PhonemeInventory = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, en);
}

#[test]
fn test_sanskrit_phoneme_serde_roundtrip() {
    let sa = phoneme::sanskrit();
    let json = serde_json::to_string(&sa).unwrap();
    let deserialized: phoneme::PhonemeInventory = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, sa);
}

#[test]
fn test_greek_phoneme_serde_roundtrip() {
    let el = phoneme::greek();
    let json = serde_json::to_string(&el).unwrap();
    let deserialized: phoneme::PhonemeInventory = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, el);
}

#[test]
fn test_script_serde_roundtrip() {
    let latin = Script {
        code: Cow::Borrowed("Latn"),
        name: Cow::Borrowed("Latin"),
        script_type: ScriptType::Alphabet,
        direction: Direction::LeftToRight,
        status: ScriptStatus::Living,
        attestation: None,
        unicode_ranges: vec![(0x0041, 0x005A), (0x0061, 0x007A)],
        languages: vec![Cow::Borrowed("en"), Cow::Borrowed("fr")],
    };
    let json = serde_json::to_string(&latin).unwrap();
    let deserialized: Script = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, latin);
}

#[test]
fn test_grammar_serde_roundtrip() {
    let en = GrammarProfile {
        language_code: Cow::Borrowed("en"),
        morphology: Morphology::Fusional,
        word_order: WordOrder::SVO,
        case_count: 2,
        has_gender: false,
        gender_count: 0,
        has_dual: false,
        has_classifiers: false,
    };
    let json = serde_json::to_string(&en).unwrap();
    let deserialized: GrammarProfile = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, en);
}

#[test]
fn test_lexicon_serde_roundtrip() {
    let lex = Lexicon {
        language_code: Cow::Borrowed("en"),
        entries: vec![LexEntry {
            word: Cow::Borrowed("water"),
            ipa: Cow::Borrowed("ˈwɔːtər"),
            gloss: Cow::Borrowed("water"),
            pos: PartOfSpeech::Noun,
            frequency_rank: Some(250),
            swadesh_index: Some(1),
        }],
    };
    let json = serde_json::to_string(&lex).unwrap();
    let deserialized: Lexicon = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized, lex);
}

// ---------------------------------------------------------------------------
// Cross-module
// ---------------------------------------------------------------------------

#[test]
fn test_english_consonant_vowel_split() {
    let en = phoneme::english();
    let total = en.phonemes.len();
    assert_eq!(en.consonant_count() + en.vowel_count(), total);
}

#[test]
fn test_phoneme_kind_classification() {
    for code in varna::registry::all_codes() {
        let inv = varna::registry::phonemes(code).unwrap();
        for p in &inv.phonemes {
            match &p.kind {
                PhonemeKind::Consonant { .. } => {}
                PhonemeKind::Vowel { .. } => {}
                _ => panic!("unexpected phoneme kind /{}/  in {}", p.ipa, code),
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Error display
// ---------------------------------------------------------------------------

#[test]
fn test_error_display() {
    let err = varna::VarnaError::UnknownLanguage("xx".into());
    assert_eq!(err.to_string(), "unknown language: xx");

    let err = varna::VarnaError::PhonemeNotInInventory {
        phoneme: "ʀ".into(),
        language: "en".into(),
    };
    assert!(err.to_string().contains("ʀ"));
    assert!(err.to_string().contains("en"));
}

#[test]
fn test_all_error_variants_display() {
    let cases: Vec<(varna::VarnaError, &str)> = vec![
        (
            varna::VarnaError::UnknownLanguage("zz".into()),
            "unknown language: zz",
        ),
        (
            varna::VarnaError::UnknownScript("Xxxx".into()),
            "unknown script: Xxxx",
        ),
        (
            varna::VarnaError::InvalidIpa("???".into()),
            "invalid IPA symbol: ???",
        ),
        (
            varna::VarnaError::WordNotFound {
                word: "foo".into(),
                language: "en".into(),
            },
            "word not found: foo in en",
        ),
    ];
    for (err, expected) in cases {
        assert_eq!(err.to_string(), expected);
    }
}

// ---------------------------------------------------------------------------
// Phoneme constructors & serde
// ---------------------------------------------------------------------------

#[test]
fn test_phoneme_kind_serde_consonant() {
    let p = Phoneme::consonant("p", Manner::Plosive, Place::Bilabial, false);
    let json = serde_json::to_string(&p).unwrap();
    let back: Phoneme = serde_json::from_str(&json).unwrap();
    assert_eq!(p, back);
}

#[test]
fn test_phoneme_kind_serde_vowel() {
    let p = Phoneme::vowel("æ", Height::NearOpen, Backness::Front, false);
    let json = serde_json::to_string(&p).unwrap();
    let back: Phoneme = serde_json::from_str(&json).unwrap();
    assert_eq!(p, back);
}

#[test]
fn test_phoneme_constructors() {
    let c = Phoneme::consonant("t", Manner::Plosive, Place::Alveolar, false);
    assert_eq!(c.ipa, "t");
    assert!(matches!(
        c.kind,
        PhonemeKind::Consonant { voiced: false, .. }
    ));

    let v = Phoneme::vowel("", Height::Close, Backness::Front, false);
    assert_eq!(v.ipa, "");
    assert!(matches!(v.kind, PhonemeKind::Vowel { rounded: false, .. }));
}

#[test]
fn test_phoneme_constructor_with_owned_string() {
    let ipa = String::from("custom");
    let p = Phoneme::consonant(ipa, Manner::Fricative, Place::Glottal, false);
    assert_eq!(p.ipa, "custom");
}

// ---------------------------------------------------------------------------
// Registry
// ---------------------------------------------------------------------------

#[test]
fn test_registry_roundtrip_all_languages() {
    for code in varna::registry::all_codes() {
        let inv = varna::registry::phonemes(code).unwrap();
        let json = serde_json::to_string(&inv).unwrap();
        let back: phoneme::PhonemeInventory = serde_json::from_str(&json).unwrap();
        assert_eq!(inv, back, "serde roundtrip failed for {code}");
    }
}

#[test]
fn test_registry_script_consistency() {
    for code in varna::registry::all_codes() {
        let info = varna::registry::info(code).unwrap();
        if let Some(script) = varna::registry::primary_script(code) {
            assert_eq!(
                script.code, info.script_codes[0],
                "script mismatch for {code}"
            );
        }
    }
}

// ---------------------------------------------------------------------------
// Script metadata
// ---------------------------------------------------------------------------

#[test]
fn test_all_scripts_serde_roundtrip() {
    for code in varna::script::all_codes() {
        let script = varna::script::by_code(code).unwrap();
        let json = serde_json::to_string(&script).unwrap();
        let back: Script = serde_json::from_str(&json).unwrap();
        assert_eq!(script, back, "serde roundtrip failed for script {code}");
    }
}

// ---------------------------------------------------------------------------
// Allophone rules
// ---------------------------------------------------------------------------

#[test]
fn test_allophone_realize_cross_module() {
    use varna::phoneme::allophone;
    let rules = allophone::english_allophones();
    // Verify allophone realization agrees with phoneme inventory
    let en = phoneme::english();
    assert!(en.has("t")); // phoneme exists
    let realized = rules.realize("t", &allophone::Environment::Intervocalic);
    assert_eq!(realized, "ɾ");
    assert!(!en.has("ɾ")); // allophone is NOT in the phoneme inventory
}

// ---------------------------------------------------------------------------
// Phonotactics
// ---------------------------------------------------------------------------

#[test]
fn test_phonotactics_serde_roundtrip_all() {
    use varna::phoneme::syllable;
    let profiles = [
        syllable::english_phonotactics(),
        syllable::sanskrit_phonotactics(),
        syllable::japanese_phonotactics(),
    ];
    for p in &profiles {
        let json = serde_json::to_string(p).unwrap();
        let back: syllable::Phonotactics = serde_json::from_str(&json).unwrap();
        assert_eq!(*p, back, "serde roundtrip failed for {}", p.language_code);
    }
}

// ---------------------------------------------------------------------------
// Transliteration
// ---------------------------------------------------------------------------

#[test]
fn test_transliteration_greek_roundtrip_chars() {
    use varna::script::transliteration;
    let table = transliteration::greek_beta_code();
    // Every char in forward map should transliterate correctly
    for (src, tgt) in &table.forward {
        assert_eq!(
            table.transliterate(src),
            tgt.as_ref(),
            "failed for {src} → {tgt}"
        );
    }
}

// ---------------------------------------------------------------------------
// Numerals
// ---------------------------------------------------------------------------

#[test]
fn test_numeral_systems_serde_roundtrip() {
    use varna::script::numerals;
    let systems = [numerals::devanagari_digits(), numerals::greek_isopsephy()];
    for sys in &systems {
        let json = serde_json::to_string(sys).unwrap();
        let back: numerals::NumeralSystem = serde_json::from_str(&json).unwrap();
        assert_eq!(*sys, back, "serde roundtrip failed for {}", sys.name);
    }
}

#[test]
fn test_greek_isopsephy_word_value() {
    use varna::script::numerals;
    let sys = numerals::greek_isopsephy();
    // "θεος" (theos) = 9 + 5 + 70 + 200 = 284
    assert_eq!(sys.string_value("θεος"), Some(284));
}

// ---------------------------------------------------------------------------
// Dialect/variety
// ---------------------------------------------------------------------------

#[test]
fn test_dialect_apply_overlay() {
    let en = phoneme::english();
    let rp = varna::dialect::british_english();
    let rp_inv = rp.apply(&en);

    // RP adds /ɒ/ and removes /ɹ/
    assert!(rp_inv.has("ɒ"));
    assert!(!rp_inv.has("ɹ"));
    // But still has everything else
    assert!(rp_inv.has("p"));
    assert!(rp_inv.has("θ"));
    assert_eq!(rp_inv.language_code, "en-GB");
}

#[test]
fn test_dialect_serde_roundtrip() {
    let rp = varna::dialect::british_english();
    let json = serde_json::to_string(&rp).unwrap();
    let back: varna::dialect::LanguageVariety = serde_json::from_str(&json).unwrap();
    assert_eq!(rp, back);
}

// ---------------------------------------------------------------------------
// Cognate & etymology
// ---------------------------------------------------------------------------

#[test]
fn test_cognate_cross_registry() {
    use varna::lexicon::cognate;
    let cog = cognate::water_cognates();
    // Verify cognate languages are registered
    for entry in &cog.entries {
        if varna::registry::info(&entry.language).is_some() {
            let inv = varna::registry::phonemes(&entry.language).unwrap();
            assert!(
                !inv.phonemes.is_empty(),
                "empty inventory for {}",
                entry.language
            );
        }
    }
}

// ---------------------------------------------------------------------------
// Extended inventories
// ---------------------------------------------------------------------------

#[test]
fn test_all_inventories_serde_roundtrip() {
    for code in varna::registry::all_codes() {
        let inv = varna::registry::phonemes(code).unwrap();
        let json = serde_json::to_string(&inv).unwrap();
        let back: phoneme::PhonemeInventory = serde_json::from_str(&json).unwrap();
        assert_eq!(inv, back, "serde roundtrip failed for {code}");
    }
}

#[test]
fn test_all_inventories_consonant_vowel_split() {
    for code in varna::registry::all_codes() {
        let inv = varna::registry::phonemes(code).unwrap();
        assert_eq!(
            inv.consonant_count() + inv.vowel_count(),
            inv.phonemes.len(),
            "consonant+vowel mismatch for {code}"
        );
    }
}

// ---------------------------------------------------------------------------
// Grammar profiles
// ---------------------------------------------------------------------------

#[test]
fn test_grammar_all_serde_roundtrip() {
    for code in varna::grammar::all_codes() {
        let g = varna::grammar::by_code(code).unwrap();
        let json = serde_json::to_string(&g).unwrap();
        let back: varna::grammar::GrammarProfile = serde_json::from_str(&json).unwrap();
        assert_eq!(g, back, "grammar roundtrip failed for {code}");
    }
}

#[test]
fn test_grammar_gender_consistency() {
    for code in varna::grammar::all_codes() {
        let g = varna::grammar::by_code(code).unwrap();
        if g.has_gender {
            assert!(g.gender_count > 0, "{code} has_gender but gender_count=0");
        } else {
            assert_eq!(g.gender_count, 0, "{code} !has_gender but gender_count>0");
        }
    }
}

// ---------------------------------------------------------------------------
// Swadesh lists
// ---------------------------------------------------------------------------

#[test]
fn test_swadesh_all_serde_roundtrip() {
    for code in varna::lexicon::swadesh::all_codes() {
        let lex = varna::lexicon::swadesh::by_code(code).unwrap();
        let json = serde_json::to_string(&lex).unwrap();
        let back: varna::lexicon::Lexicon = serde_json::from_str(&json).unwrap();
        assert_eq!(lex, back, "swadesh roundtrip failed for {code}");
    }
}

#[test]
fn test_swadesh_cross_language_water() {
    for code in varna::lexicon::swadesh::all_codes() {
        let lex = varna::lexicon::swadesh::by_code(code).unwrap();
        let water = lex.entries.iter().find(|e| e.swadesh_index == Some(23));
        assert!(water.is_some(), "missing water (index 23) in {code}");
        assert_eq!(water.unwrap().gloss, "water");
    }
}

// ---------------------------------------------------------------------------
// Tonal language features
// ---------------------------------------------------------------------------

#[test]
fn test_tonal_languages_have_tones() {
    let tonal = ["zh", "yo", "th", "vi", "lzh"];
    for code in tonal {
        let inv = varna::registry::phonemes(code).unwrap();
        assert!(
            inv.tones.is_some(),
            "{code} should be tonal but has no tones"
        );
        assert_eq!(
            inv.stress,
            phoneme::StressPattern::Tonal,
            "{code} stress should be Tonal"
        );
    }
}

#[test]
fn test_non_tonal_languages_no_tones() {
    let non_tonal = ["en", "es", "fr", "de", "ru", "pt", "la", "fi", "tr"];
    for code in non_tonal {
        let inv = varna::registry::phonemes(code).unwrap();
        assert!(inv.tones.is_none(), "{code} should not have tones");
    }
}