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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Phoneme inventories — IPA phonemes per language, phonological features.
//!
//! Every language has a finite set of contrastive sounds (phonemes). This module
//! provides those inventories with IPA transcription, articulatory features
//! (manner, place, voicing), allophone rules, and phonotactic constraints.

pub mod allophone;
pub mod inventories;
pub mod syllable;

use std::borrow::Cow;

use serde::{Deserialize, Serialize};

/// Articulatory manner of a consonant.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Manner {
    Plosive,
    Nasal,
    Trill,
    TapFlap,
    Fricative,
    LateralFricative,
    Approximant,
    LateralApproximant,
    Affricate,
}

/// Place of articulation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Place {
    Bilabial,
    Labiodental,
    Dental,
    Alveolar,
    Postalveolar,
    Retroflex,
    Palatal,
    Velar,
    Uvular,
    Pharyngeal,
    Glottal,
    /// Simultaneous bilabial and velar (e.g., English /w/).
    LabialVelar,
}

/// Vowel height.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Height {
    Close,
    NearClose,
    CloseMid,
    Mid,
    OpenMid,
    NearOpen,
    Open,
}

/// Vowel backness.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Backness {
    Front,
    Central,
    Back,
}

/// A phoneme with its IPA symbol and articulatory features.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Phoneme {
    /// IPA symbol (e.g., "p", "ʃ", "æ").
    pub ipa: Cow<'static, str>,
    /// Classification.
    pub kind: PhonemeKind,
}

impl Phoneme {
    /// Create a consonant phoneme.
    #[must_use]
    pub fn consonant(
        ipa: impl Into<Cow<'static, str>>,
        manner: Manner,
        place: Place,
        voiced: bool,
    ) -> Self {
        Self {
            ipa: ipa.into(),
            kind: PhonemeKind::Consonant {
                manner,
                place,
                voiced,
            },
        }
    }

    /// Create a vowel phoneme.
    #[must_use]
    pub fn vowel(
        ipa: impl Into<Cow<'static, str>>,
        height: Height,
        backness: Backness,
        rounded: bool,
    ) -> Self {
        Self {
            ipa: ipa.into(),
            kind: PhonemeKind::Vowel {
                height,
                backness,
                rounded,
            },
        }
    }
}

/// Classification of a phoneme.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum PhonemeKind {
    #[non_exhaustive]
    Consonant {
        manner: Manner,
        place: Place,
        voiced: bool,
    },
    #[non_exhaustive]
    Vowel {
        height: Height,
        backness: Backness,
        rounded: bool,
    },
}

/// A language's complete phoneme inventory.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PhonemeInventory {
    /// ISO 639-1 or 639-3 language code.
    pub language_code: Cow<'static, str>,
    /// Language name in English.
    pub language_name: Cow<'static, str>,
    /// All phonemes in this language.
    pub phonemes: Vec<Phoneme>,
    /// Tone system (None for non-tonal languages).
    pub tones: Option<Vec<Cow<'static, str>>>,
    /// Stress pattern.
    pub stress: StressPattern,
}

/// How stress works in a language.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum StressPattern {
    /// Stress on a fixed syllable (e.g., French → final, Finnish → initial).
    Fixed,
    /// Stress is contrastive / unpredictable (e.g., English, Russian).
    Free,
    /// Pitch accent system (e.g., Japanese, Swedish).
    PitchAccent,
    /// Tonal — pitch distinguishes meaning (e.g., Mandarin, Yoruba).
    Tonal,
}

impl PhonemeInventory {
    /// Number of consonants in the inventory.
    #[must_use]
    #[inline]
    pub fn consonant_count(&self) -> usize {
        self.phonemes
            .iter()
            .filter(|p| matches!(p.kind, PhonemeKind::Consonant { .. }))
            .count()
    }

    /// Number of vowels in the inventory.
    #[must_use]
    #[inline]
    pub fn vowel_count(&self) -> usize {
        self.phonemes
            .iter()
            .filter(|p| matches!(p.kind, PhonemeKind::Vowel { .. }))
            .count()
    }

    /// Look up a phoneme by IPA symbol.
    #[must_use]
    #[inline]
    pub fn find(&self, ipa: &str) -> Option<&Phoneme> {
        tracing::trace!(language = %self.language_code, ipa, "phoneme lookup");
        self.phonemes.iter().find(|p| p.ipa == ipa)
    }

    /// Check if a phoneme exists in this language.
    #[must_use]
    #[inline]
    pub fn has(&self, ipa: &str) -> bool {
        self.find(ipa).is_some()
    }
}

// ---------------------------------------------------------------------------
// Builder
// ---------------------------------------------------------------------------

/// Builder for constructing [`PhonemeInventory`] instances.
///
/// # Example
/// ```
/// use varna::phoneme::*;
///
/// let inv = PhonemeInventoryBuilder::new("xx", "Example")
///     .stress(StressPattern::Fixed)
///     .consonant("p", Manner::Plosive, Place::Bilabial, false)
///     .vowel("a", Height::Open, Backness::Central, false)
///     .build();
///
/// assert_eq!(inv.consonant_count(), 1);
/// assert_eq!(inv.vowel_count(), 1);
/// ```
pub struct PhonemeInventoryBuilder {
    language_code: Cow<'static, str>,
    language_name: Cow<'static, str>,
    phonemes: Vec<Phoneme>,
    tones: Option<Vec<Cow<'static, str>>>,
    stress: StressPattern,
}

impl PhonemeInventoryBuilder {
    /// Start building an inventory for the given language.
    #[must_use]
    pub fn new(code: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
        Self::with_capacity(code, name, 48)
    }

    /// Start building with a specific phoneme capacity hint.
    #[must_use]
    pub fn with_capacity(
        code: impl Into<Cow<'static, str>>,
        name: impl Into<Cow<'static, str>>,
        capacity: usize,
    ) -> Self {
        Self {
            language_code: code.into(),
            language_name: name.into(),
            phonemes: Vec::with_capacity(capacity),
            tones: None,
            stress: StressPattern::Free,
        }
    }

    /// Set the stress pattern.
    #[must_use]
    pub fn stress(mut self, pattern: StressPattern) -> Self {
        self.stress = pattern;
        self
    }

    /// Set the tone system.
    #[must_use]
    pub fn tones(mut self, tones: Vec<Cow<'static, str>>) -> Self {
        self.tones = Some(tones);
        self
    }

    /// Add a consonant.
    #[must_use]
    pub fn consonant(
        mut self,
        ipa: impl Into<Cow<'static, str>>,
        manner: Manner,
        place: Place,
        voiced: bool,
    ) -> Self {
        self.phonemes
            .push(Phoneme::consonant(ipa, manner, place, voiced));
        self
    }

    /// Add a vowel.
    #[must_use]
    pub fn vowel(
        mut self,
        ipa: impl Into<Cow<'static, str>>,
        height: Height,
        backness: Backness,
        rounded: bool,
    ) -> Self {
        self.phonemes
            .push(Phoneme::vowel(ipa, height, backness, rounded));
        self
    }

    /// Add a pre-built phoneme.
    #[must_use]
    pub fn phoneme(mut self, phoneme: Phoneme) -> Self {
        self.phonemes.push(phoneme);
        self
    }

    /// Consume the builder and produce the inventory.
    ///
    /// # Panics (debug only)
    ///
    /// Panics in debug builds if duplicate IPA symbols are detected.
    #[must_use]
    pub fn build(self) -> PhonemeInventory {
        debug_assert!(
            {
                let mut seen = std::collections::HashSet::new();
                self.phonemes.iter().all(|p| seen.insert(&p.ipa))
            },
            "duplicate IPA symbol in {} inventory",
            self.language_code
        );
        tracing::debug!(
            language = %self.language_code,
            phonemes = self.phonemes.len(),
            "built phoneme inventory"
        );
        PhonemeInventory {
            language_code: self.language_code,
            language_name: self.language_name,
            phonemes: self.phonemes,
            tones: self.tones,
            stress: self.stress,
        }
    }
}

// ---------------------------------------------------------------------------
// Pre-built inventories
// ---------------------------------------------------------------------------

/// Build the English (General American) phoneme inventory.
///
/// 24 consonants + 12 vowels. Stress: free (contrastive).
#[must_use]
pub fn english() -> PhonemeInventory {
    use Backness::*;
    use Height::*;
    use Manner::*;
    use Place::*;

    PhonemeInventoryBuilder::with_capacity("en", "English", 36)
        .stress(StressPattern::Free)
        // Plosives
        .consonant("p", Plosive, Bilabial, false)
        .consonant("b", Plosive, Bilabial, true)
        .consonant("t", Plosive, Alveolar, false)
        .consonant("d", Plosive, Alveolar, true)
        .consonant("k", Plosive, Velar, false)
        .consonant("ɡ", Plosive, Velar, true)
        // Fricatives
        .consonant("f", Fricative, Labiodental, false)
        .consonant("v", Fricative, Labiodental, true)
        .consonant("θ", Fricative, Dental, false)
        .consonant("ð", Fricative, Dental, true)
        .consonant("s", Fricative, Alveolar, false)
        .consonant("z", Fricative, Alveolar, true)
        .consonant("ʃ", Fricative, Postalveolar, false)
        .consonant("ʒ", Fricative, Postalveolar, true)
        .consonant("h", Fricative, Glottal, false)
        // Nasals
        .consonant("m", Nasal, Bilabial, true)
        .consonant("n", Nasal, Alveolar, true)
        .consonant("ŋ", Nasal, Velar, true)
        // Approximants
        .consonant("ɹ", Approximant, Alveolar, true)
        .consonant("l", LateralApproximant, Alveolar, true)
        .consonant("w", Approximant, LabialVelar, true)
        .consonant("j", Approximant, Palatal, true)
        // Affricates
        .consonant("t͡ʃ", Affricate, Postalveolar, false)
        .consonant("d͡ʒ", Affricate, Postalveolar, true)
        // Vowels (General American)
        .vowel("", Close, Front, false)
        .vowel("ɪ", NearClose, Front, false)
        .vowel("", CloseMid, Front, false)
        .vowel("ɛ", OpenMid, Front, false)
        .vowel("æ", NearOpen, Front, false)
        .vowel("ɑː", Open, Back, false)
        .vowel("ɔː", OpenMid, Back, true)
        .vowel("", CloseMid, Back, true)
        .vowel("ʊ", NearClose, Back, true)
        .vowel("", Close, Back, true)
        .vowel("ʌ", OpenMid, Central, false)
        .vowel("ə", Mid, Central, false)
        .build()
}

/// Build the Sanskrit (Classical) phoneme inventory.
///
/// Sanskrit has a systematic phoneme inventory organized by place and manner,
/// critical for the Katapayadi numeral encoding system used by sankhya.
///
/// 36 consonants + 14 vowels. Stress: pitch accent.
#[must_use]
pub fn sanskrit() -> PhonemeInventory {
    use Backness::*;
    use Height::*;
    use Manner::*;
    use Place::*;

    PhonemeInventoryBuilder::with_capacity("sa", "Sanskrit", 50)
        .stress(StressPattern::PitchAccent)
        // --- Sparsha (stop/plosive) consonants: 5 vargas × 5 ---
        // Kavarga (velar)
        .consonant("k", Plosive, Velar, false)
        .consonant("", Plosive, Velar, false) // aspirated
        .consonant("ɡ", Plosive, Velar, true)
        .consonant("ɡʰ", Plosive, Velar, true) // aspirated
        .consonant("ŋ", Nasal, Velar, true)
        // Chavarga (palatal)
        .consonant("t͡ɕ", Affricate, Palatal, false)
        .consonant("t͡ɕʰ", Affricate, Palatal, false)
        .consonant("d͡ʑ", Affricate, Palatal, true)
        .consonant("d͡ʑʰ", Affricate, Palatal, true)
        .consonant("ɲ", Nasal, Palatal, true)
        // Tavarga (retroflex)
        .consonant("ʈ", Plosive, Retroflex, false)
        .consonant("ʈʰ", Plosive, Retroflex, false)
        .consonant("ɖ", Plosive, Retroflex, true)
        .consonant("ɖʰ", Plosive, Retroflex, true)
        .consonant("ɳ", Nasal, Retroflex, true)
        // Tavarga (dental)
        .consonant("", Plosive, Dental, false)
        .consonant("t̪ʰ", Plosive, Dental, false)
        .consonant("", Plosive, Dental, true)
        .consonant("d̪ʰ", Plosive, Dental, true)
        .consonant("", Nasal, Dental, true)
        // Pavarga (bilabial)
        .consonant("p", Plosive, Bilabial, false)
        .consonant("", Plosive, Bilabial, false)
        .consonant("b", Plosive, Bilabial, true)
        .consonant("", Plosive, Bilabial, true)
        .consonant("m", Nasal, Bilabial, true)
        // --- Antastha (semivowels/approximants) ---
        .consonant("j", Approximant, Palatal, true)
        .consonant("r", Trill, Alveolar, true)
        .consonant("l", LateralApproximant, Alveolar, true)
        .consonant("ʋ", Approximant, Labiodental, true)
        // --- Ushman (sibilants/fricatives) ---
        .consonant("ɕ", Fricative, Palatal, false)
        .consonant("ʂ", Fricative, Retroflex, false)
        .consonant("s", Fricative, Alveolar, false)
        .consonant("ɦ", Fricative, Glottal, true)
        // --- Additional ---
        .consonant("ɭ", LateralApproximant, Retroflex, true) // Vedic ḷ
        // Conjuncts (phonologically biphonemic, but single aksharas for Katapayadi)
        .consonant("", Affricate, Velar, false) // kṣa (k+ṣ)
        .consonant("d͡ʑɲ", Affricate, Palatal, true) // jña (jñ)
        // --- Vowels (svara) ---
        // Short
        .vowel("ɐ", NearOpen, Central, false) // a
        .vowel("i", Close, Front, false) // i
        .vowel("u", Close, Back, true) // u
        .vowel("", Close, Central, false) // ṛ (syllabic r)
        .vowel("", Close, Central, false) // ḷ (syllabic l)
        // Long
        .vowel("ɐː", NearOpen, Central, false) // ā
        .vowel("", Close, Front, false) // ī
        .vowel("", Close, Back, true) // ū
        .vowel("r̩ː", Close, Central, false) //        .vowel("l̩ː", Close, Central, false) //        // Diphthongs
        .vowel("", CloseMid, Front, false) // e
        .vowel("ɐi", NearOpen, Front, false) // ai
        .vowel("", CloseMid, Back, true) // o
        .vowel("ɐu", NearOpen, Back, true) // au
        // Anusvāra and visarga are suprasegmental, not separate phonemes
        .build()
}

/// Build the Greek (Modern Standard) phoneme inventory.
///
/// Provides the phoneme set needed for Greek mathematical notation
/// and script metadata used by sankhya.
///
/// 20 consonants + 5 vowels. Stress: free (contrastive).
#[must_use]
pub fn greek() -> PhonemeInventory {
    use Backness::*;
    use Height::*;
    use Manner::*;
    use Place::*;

    PhonemeInventoryBuilder::with_capacity("el", "Greek", 25)
        .stress(StressPattern::Free)
        // Plosives
        .consonant("p", Plosive, Bilabial, false)
        .consonant("b", Plosive, Bilabial, true)
        .consonant("t", Plosive, Alveolar, false)
        .consonant("d", Plosive, Alveolar, true)
        .consonant("k", Plosive, Velar, false)
        .consonant("ɡ", Plosive, Velar, true)
        // Fricatives
        .consonant("f", Fricative, Labiodental, false)
        .consonant("v", Fricative, Labiodental, true)
        .consonant("θ", Fricative, Dental, false)
        .consonant("ð", Fricative, Dental, true)
        .consonant("s", Fricative, Alveolar, false)
        .consonant("z", Fricative, Alveolar, true)
        .consonant("x", Fricative, Velar, false)
        .consonant("ɣ", Fricative, Velar, true)
        // Nasals
        .consonant("m", Nasal, Bilabial, true)
        .consonant("n", Nasal, Alveolar, true)
        // Liquids
        .consonant("l", LateralApproximant, Alveolar, true)
        .consonant("r", Trill, Alveolar, true)
        // Affricates
        .consonant("t͡s", Affricate, Alveolar, false)
        .consonant("d͡z", Affricate, Alveolar, true)
        // Vowels (5-vowel system)
        .vowel("i", Close, Front, false)
        .vowel("e", CloseMid, Front, false)
        .vowel("a", Open, Central, false)
        .vowel("o", CloseMid, Back, true)
        .vowel("u", Close, Back, true)
        .build()
}

#[cfg(test)]
mod tests {
    use super::*;

    // -- English tests --

    #[test]
    fn test_english_inventory_size() {
        let en = english();
        assert_eq!(en.consonant_count(), 24);
        assert_eq!(en.vowel_count(), 12);
    }

    #[test]
    fn test_english_has_th() {
        let en = english();
        assert!(en.has("θ"));
        assert!(en.has("ð"));
    }

    #[test]
    fn test_english_no_tones() {
        let en = english();
        assert!(en.tones.is_none());
        assert_eq!(en.stress, StressPattern::Free);
    }

    #[test]
    fn test_find_phoneme() {
        let en = english();
        let p = en.find("ʃ").unwrap();
        assert!(matches!(
            p.kind,
            PhonemeKind::Consonant {
                manner: Manner::Fricative,
                place: Place::Postalveolar,
                voiced: false
            }
        ));
    }

    #[test]
    fn test_missing_phoneme() {
        let en = english();
        assert!(!en.has("ʀ"));
    }

    #[test]
    fn test_w_is_labial_velar() {
        let en = english();
        let w = en.find("w").unwrap();
        assert!(matches!(
            w.kind,
            PhonemeKind::Consonant {
                place: Place::LabialVelar,
                ..
            }
        ));
    }

    #[test]
    fn test_phoneme_eq() {
        let a = Phoneme::consonant("p", Manner::Plosive, Place::Bilabial, false);
        let b = a.clone();
        assert_eq!(a, b);
    }

    // -- Sanskrit tests --

    #[test]
    fn test_sanskrit_inventory_size() {
        let sa = sanskrit();
        assert_eq!(sa.consonant_count(), 36);
        assert_eq!(sa.vowel_count(), 14);
        assert_eq!(sa.language_code, "sa");
    }

    #[test]
    fn test_sanskrit_five_vargas() {
        let sa = sanskrit();
        // Each varga has 5 consonants: unvoiced, unvoiced-aspirated,
        // voiced, voiced-aspirated, nasal
        // Kavarga (velar)
        assert!(sa.has("k"));
        assert!(sa.has(""));
        assert!(sa.has("ɡ"));
        assert!(sa.has("ɡʰ"));
        assert!(sa.has("ŋ"));
    }

    #[test]
    fn test_sanskrit_retroflexes() {
        let sa = sanskrit();
        assert!(sa.has("ʈ"));
        assert!(sa.has("ɖ"));
        assert!(sa.has("ɳ"));
    }

    #[test]
    fn test_sanskrit_sibilants() {
        let sa = sanskrit();
        assert!(sa.has("ɕ")); // palatal
        assert!(sa.has("ʂ")); // retroflex
        assert!(sa.has("s")); // alveolar
    }

    #[test]
    fn test_sanskrit_vowels() {
        let sa = sanskrit();
        // Syllabic r and l are distinctive Sanskrit vowels
        assert!(sa.has(""));
        assert!(sa.has(""));
        // Short/long pairs
        assert!(sa.has("ɐ"));
        assert!(sa.has("ɐː"));
    }

    #[test]
    fn test_sanskrit_stress() {
        let sa = sanskrit();
        assert_eq!(sa.stress, StressPattern::PitchAccent);
        assert!(sa.tones.is_none());
    }

    // -- Greek tests --

    #[test]
    fn test_greek_inventory_size() {
        let el = greek();
        assert_eq!(el.consonant_count(), 20);
        assert_eq!(el.vowel_count(), 5);
        assert_eq!(el.language_code, "el");
    }

    #[test]
    fn test_greek_five_vowels() {
        let el = greek();
        assert!(el.has("i"));
        assert!(el.has("e"));
        assert!(el.has("a"));
        assert!(el.has("o"));
        assert!(el.has("u"));
    }

    #[test]
    fn test_greek_velar_fricatives() {
        let el = greek();
        assert!(el.has("x"));
        assert!(el.has("ɣ"));
    }

    #[test]
    fn test_greek_stress() {
        let el = greek();
        assert_eq!(el.stress, StressPattern::Free);
    }

    // -- Builder tests --

    #[test]
    fn test_builder_minimal() {
        let inv = PhonemeInventoryBuilder::new("xx", "Test")
            .consonant("t", Manner::Plosive, Place::Alveolar, false)
            .vowel("a", Height::Open, Backness::Central, false)
            .build();
        assert_eq!(inv.language_code, "xx");
        assert_eq!(inv.consonant_count(), 1);
        assert_eq!(inv.vowel_count(), 1);
        assert_eq!(inv.stress, StressPattern::Free); // default
    }

    #[test]
    fn test_builder_with_tones() {
        let inv = PhonemeInventoryBuilder::new("xx", "Tonal Test")
            .stress(StressPattern::Tonal)
            .tones(vec![
                Cow::Borrowed("˥"),
                Cow::Borrowed("˧˥"),
                Cow::Borrowed("˨˩˦"),
                Cow::Borrowed("˥˩"),
            ])
            .vowel("a", Height::Open, Backness::Central, false)
            .build();
        assert_eq!(inv.stress, StressPattern::Tonal);
        assert_eq!(inv.tones.as_ref().unwrap().len(), 4);
    }

    #[test]
    fn test_builder_phoneme_method() {
        let custom = Phoneme::consonant("ɬ", Manner::LateralFricative, Place::Alveolar, false);
        let inv = PhonemeInventoryBuilder::new("xx", "Test")
            .phoneme(custom.clone())
            .build();
        assert_eq!(inv.phonemes[0], custom);
    }
}