shabda 2.0.0

shabda — Grapheme-to-phoneme (G2P) conversion: text to phoneme sequences for vocal synthesis
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
//! Integration tests for shabda.

use shabda::engine::ConvertOptions;
use shabda::prelude::*;

#[test]
fn test_hello_world() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p.convert("hello world").unwrap();
    assert!(!events.is_empty(), "should produce phoneme events");
    // Should have phonemes + silence between words
    assert!(events.len() > 4, "hello world needs multiple phonemes");
}

#[test]
fn test_empty_input_errors() {
    let g2p = G2PEngine::new(Language::English);
    assert!(g2p.convert("").is_err());
    assert!(g2p.convert("   ").is_err());
}

#[test]
fn test_dictionary_lookup() {
    let g2p = G2PEngine::new(Language::English);
    // "the" is in the dictionary
    let events = g2p.convert("the").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_rule_fallback() {
    let g2p = G2PEngine::new(Language::English);
    // "flurb" is not in any dictionary — rules handle it
    let events = g2p.convert("flurb").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_speak_produces_audio() {
    let g2p = G2PEngine::new(Language::English);
    let voice = svara::voice::VoiceProfile::new_male();
    let samples = g2p.speak("hello", &voice, 44100.0).unwrap();
    assert!(!samples.is_empty());
    assert!(samples.iter().all(|s| s.is_finite()));
    let max_amp: f32 = samples.iter().map(|s| s.abs()).fold(0.0, f32::max);
    assert!(max_amp > 0.001, "should produce audible output");
}

#[test]
fn test_speak_sentence() {
    let g2p = G2PEngine::new(Language::English);
    let voice = svara::voice::VoiceProfile::new_female();
    let samples = g2p.speak("hello world", &voice, 44100.0).unwrap();
    assert!(!samples.is_empty());
    assert!(samples.iter().all(|s| s.is_finite()));
}

#[test]
fn test_question_intonation() {
    let g2p = G2PEngine::new(Language::English);
    let voice = svara::voice::VoiceProfile::new_male();
    let samples = g2p.speak("hello?", &voice, 44100.0).unwrap();
    assert!(!samples.is_empty());
}

#[test]
fn test_custom_dictionary_entry() {
    let mut g2p = G2PEngine::new(Language::English);
    g2p.dictionary_mut().insert(
        "agnos",
        &[
            svara::phoneme::Phoneme::VowelAsh,
            svara::phoneme::Phoneme::PlosiveG,
            svara::phoneme::Phoneme::NasalN,
            svara::phoneme::Phoneme::VowelO,
            svara::phoneme::Phoneme::FricativeS,
        ],
    );
    let events = g2p.convert("agnos").unwrap();
    assert!(events.len() >= 5);
}

#[test]
fn test_content_vs_function_word_stress() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p.convert("the cat").unwrap();
    // "the" should be unstressed, "cat" should have primary stress
    let the_events: Vec<_> = events.iter().take(2).collect(); // "the" = 2 phonemes
    assert_eq!(the_events[0].stress, svara::prosody::Stress::Unstressed);
}

#[test]
fn test_serde_roundtrip_language() {
    let json = serde_json::to_string(&Language::English).unwrap();
    let l2: Language = serde_json::from_str(&json).unwrap();
    assert_eq!(l2, Language::English);
}

#[test]
fn test_serde_roundtrip_engine() {
    let engine = G2PEngine::new(Language::English);
    let json = serde_json::to_string(&engine).unwrap();
    let e2: G2PEngine = serde_json::from_str(&json).unwrap();
    assert_eq!(e2.language(), Language::English);
    assert_eq!(e2.dictionary().len(), engine.dictionary().len());
}

#[test]
fn test_serde_roundtrip_error() {
    let err = ShabdaError::UnknownWord("test".into());
    let json = serde_json::to_string(&err).unwrap();
    let e2: ShabdaError = serde_json::from_str(&json).unwrap();
    assert_eq!(err.to_string(), e2.to_string());
}

#[test]
fn test_phrase_pause_comma() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p.convert("hello, world").unwrap();
    // Should contain a longer silence for the comma (0.15s)
    let pauses: Vec<_> = events
        .iter()
        .filter(|e| e.phoneme == svara::phoneme::Phoneme::Silence && e.duration > 0.1)
        .collect();
    assert!(!pauses.is_empty(), "comma should produce a phrase pause");
}

#[test]
fn test_phrase_pause_period() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p.convert("hello. world").unwrap();
    // Should contain a longer silence for the period (0.30s)
    let pauses: Vec<_> = events
        .iter()
        .filter(|e| e.phoneme == svara::phoneme::Phoneme::Silence && e.duration > 0.2)
        .collect();
    assert!(
        !pauses.is_empty(),
        "period should produce a longer phrase pause"
    );
}

#[test]
fn test_number_expansion_in_pipeline() {
    let g2p = G2PEngine::new(Language::English);
    // "42" should be expanded to "forty two" and produce phonemes
    let events = g2p.convert("42").unwrap();
    assert!(
        events.len() > 4,
        "number should expand to words with phonemes"
    );
}

// --- ConvertOptions tests ---

#[test]
fn test_convert_with_default_matches_convert() {
    let g2p = G2PEngine::new(Language::English);
    let events_default = g2p.convert("hello world").unwrap();
    let events_with = g2p
        .convert_with("hello world", &ConvertOptions::default())
        .unwrap();
    assert_eq!(events_default.len(), events_with.len());
}

#[test]
fn test_convert_with_emphasis() {
    let g2p = G2PEngine::new(Language::English);
    let opts = ConvertOptions::new().with_emphasis(true);
    let events = g2p.convert_with("HELLO world", &opts).unwrap();
    assert!(!events.is_empty());
    // Emphasized word should have primary stress on all vowels
    let primary_count = events
        .iter()
        .filter(|e| e.stress == svara::prosody::Stress::Primary)
        .count();
    assert!(primary_count >= 1, "emphasis should produce primary stress");
}

#[test]
fn test_convert_with_speaking_rate() {
    let g2p = G2PEngine::new(Language::English);
    let normal = g2p.convert("hello world").unwrap();
    let slow = g2p
        .convert_with(
            "hello world",
            &ConvertOptions::new().with_speaking_rate(75.0),
        )
        .unwrap();
    // Slow speech should have longer total duration
    let normal_dur: f32 = normal.iter().map(|e| e.duration).sum();
    let slow_dur: f32 = slow.iter().map(|e| e.duration).sum();
    assert!(
        slow_dur > normal_dur * 1.5,
        "75 WPM should be ~2x longer than 150 WPM default"
    );
}

#[test]
fn test_serde_roundtrip_convert_options() {
    let opts = ConvertOptions::new()
        .with_emphasis(true)
        .with_speaking_rate(120.0);
    let json = serde_json::to_string(&opts).unwrap();
    let roundtripped: ConvertOptions = serde_json::from_str(&json).unwrap();
    assert_eq!(opts, roundtripped);
}

// --- Accuracy feature tests ---

#[test]
fn test_abbreviation_in_pipeline() {
    let g2p = G2PEngine::new(Language::English);
    // "Dr." should be expanded to "doctor" and produce phonemes
    let events = g2p.convert("Dr. Smith").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_acronym_in_pipeline() {
    let g2p = G2PEngine::new(Language::English);
    // "FBI" spelled out should produce phonemes for each letter
    let events = g2p.convert("the FBI").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_foreign_word_in_pipeline() {
    let g2p = G2PEngine::new(Language::English);
    // "café" should still produce phonemes (diacritics stripped)
    let events = g2p.convert("café").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_heteronym_in_pipeline() {
    let g2p = G2PEngine::new(Language::English);
    // Both contexts should produce phonemes (even if same variant for now)
    let events1 = g2p.convert("I read a book").unwrap();
    let events2 = g2p.convert("please read this").unwrap();
    assert!(!events1.is_empty());
    assert!(!events2.is_empty());
}

// --- SSML tests ---

#[test]
fn test_ssml_basic() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p.convert_ssml("<speak>hello world</speak>").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_ssml_break() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p
        .convert_ssml("hello <break time=\"500ms\"/> world")
        .unwrap();
    // Should contain a 500ms silence
    let pauses: Vec<_> = events
        .iter()
        .filter(|e| e.phoneme == svara::phoneme::Phoneme::Silence && e.duration > 0.4)
        .collect();
    assert!(!pauses.is_empty(), "should have a 500ms break");
}

#[test]
fn test_ssml_emphasis() {
    let g2p = G2PEngine::new(Language::English);
    let events = g2p
        .convert_ssml("<emphasis level=\"strong\">important</emphasis>")
        .unwrap();
    assert!(!events.is_empty());
    // Strong emphasis should have primary stress
    let has_primary = events
        .iter()
        .any(|e| e.stress == svara::prosody::Stress::Primary);
    assert!(has_primary, "strong emphasis should produce primary stress");
}

#[test]
fn test_ssml_prosody_rate() {
    let g2p = G2PEngine::new(Language::English);
    let normal = g2p.convert("hello world").unwrap();
    let slow = g2p
        .convert_ssml("<prosody rate=\"slow\">hello world</prosody>")
        .unwrap();
    let normal_dur: f32 = normal.iter().map(|e| e.duration).sum();
    let slow_dur: f32 = slow.iter().map(|e| e.duration).sum();
    assert!(
        slow_dur > normal_dur,
        "slow prosody should be longer than normal"
    );
}

#[test]
fn test_ssml_empty_errors() {
    let g2p = G2PEngine::new(Language::English);
    assert!(g2p.convert_ssml("").is_err());
}

// --- Timing + Streaming tests ---

#[test]
fn test_timing_profile() {
    use shabda::engine::TimingProfile;
    let g2p = G2PEngine::new(Language::English);
    let normal = g2p.convert("hello").unwrap();
    let stretched = g2p
        .convert_with(
            "hello",
            &ConvertOptions::new().with_timing(TimingProfile::new(2.0, 1.0, 1.0)),
        )
        .unwrap();
    let normal_dur: f32 = normal.iter().map(|e| e.duration).sum();
    let stretched_dur: f32 = stretched.iter().map(|e| e.duration).sum();
    assert!(
        stretched_dur > normal_dur,
        "2x vowel scale should increase total duration"
    );
}

#[test]
fn test_convert_streaming() {
    let g2p = G2PEngine::new(Language::English);
    let mut words_seen = Vec::new();
    g2p.convert_streaming("hello world", |word, events| {
        words_seen.push(String::from(word));
        assert!(!events.is_empty());
    })
    .unwrap();
    assert!(words_seen.len() >= 2);
}

#[test]
fn test_convert_streaming_empty_errors() {
    let g2p = G2PEngine::new(Language::English);
    assert!(g2p.convert_streaming("", |_, _| {}).is_err());
}

#[test]
fn test_serde_roundtrip_timing_profile() {
    use shabda::engine::TimingProfile;
    let profile = TimingProfile::new(1.2, 0.9, 1.5);
    let json = serde_json::to_string(&profile).unwrap();
    let roundtripped: TimingProfile = serde_json::from_str(&json).unwrap();
    assert_eq!(profile, roundtripped);
}

// --- Spanish G2P tests ---

#[test]
fn test_spanish_hola_mundo() {
    let g2p = G2PEngine::new(Language::Spanish);
    let events = g2p.convert("hola mundo").unwrap();
    assert!(!events.is_empty(), "Spanish G2P should produce phonemes");
}

#[test]
fn test_spanish_convert_with_options() {
    let g2p = G2PEngine::new(Language::Spanish);
    let opts = ConvertOptions::new().with_speaking_rate(100.0);
    let events = g2p.convert_with("buenos días", &opts).unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_spanish_speak() {
    let g2p = G2PEngine::new(Language::Spanish);
    let voice = svara::voice::VoiceProfile::new_male();
    let samples = g2p.speak("hola", &voice, 44100.0).unwrap();
    assert!(!samples.is_empty());
}

#[test]
fn test_serde_roundtrip_spanish_language() {
    let lang = Language::Spanish;
    let json = serde_json::to_string(&lang).unwrap();
    let roundtripped: Language = serde_json::from_str(&json).unwrap();
    assert_eq!(lang, roundtripped);
}

// --- German tests ---

#[test]
fn test_german_hallo_welt() {
    let g2p = G2PEngine::new(Language::German);
    let events = g2p.convert("hallo welt").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_german_speak() {
    let g2p = G2PEngine::new(Language::German);
    let voice = svara::voice::VoiceProfile::new_male();
    let samples = g2p.speak("guten tag", &voice, 44100.0).unwrap();
    assert!(!samples.is_empty());
}

#[test]
fn test_serde_roundtrip_german_language() {
    let lang = Language::German;
    let json = serde_json::to_string(&lang).unwrap();
    let roundtripped: Language = serde_json::from_str(&json).unwrap();
    assert_eq!(lang, roundtripped);
}

// --- Hindi tests ---

#[test]
fn test_hindi_namaste() {
    let g2p = G2PEngine::new(Language::Hindi);
    let events = g2p.convert("नमस्ते").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_hindi_romanized() {
    let g2p = G2PEngine::new(Language::Hindi);
    let events = g2p.convert("namaste").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_hindi_speak() {
    let g2p = G2PEngine::new(Language::Hindi);
    let voice = svara::voice::VoiceProfile::new_male();
    let samples = g2p.speak("namaste", &voice, 44100.0).unwrap();
    assert!(!samples.is_empty());
}

#[test]
fn test_serde_roundtrip_hindi_language() {
    let lang = Language::Hindi;
    let json = serde_json::to_string(&lang).unwrap();
    let roundtripped: Language = serde_json::from_str(&json).unwrap();
    assert_eq!(lang, roundtripped);
}

#[cfg(feature = "varna")]
#[test]
fn test_phoneme_inventory_german() {
    let g2p = G2PEngine::new(Language::German);
    let inv = g2p.phoneme_inventory();
    assert!(inv.has("p"));
    assert!(inv.has("ʃ"));
}

#[cfg(feature = "varna")]
#[test]
fn test_phoneme_inventory_hindi() {
    let g2p = G2PEngine::new(Language::Hindi);
    let inv = g2p.phoneme_inventory();
    assert!(inv.has("p"));
    assert!(inv.has("ə"));
}

// --- Arabic tests ---

#[test]
fn test_arabic_convert() {
    let g2p = G2PEngine::new(Language::Arabic);
    let events = g2p.convert("كتاب").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_arabic_romanized() {
    let g2p = G2PEngine::new(Language::Arabic);
    let events = g2p.convert("salam").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_serde_roundtrip_arabic_language() {
    let lang = Language::Arabic;
    let json = serde_json::to_string(&lang).unwrap();
    let roundtripped: Language = serde_json::from_str(&json).unwrap();
    assert_eq!(lang, roundtripped);
}

// --- Sanskrit tests ---

#[test]
fn test_sanskrit_convert() {
    let g2p = G2PEngine::new(Language::Sanskrit);
    let events = g2p.convert("धर्म").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_sanskrit_romanized() {
    let g2p = G2PEngine::new(Language::Sanskrit);
    let events = g2p.convert("dharma").unwrap();
    assert!(!events.is_empty());
}

#[test]
fn test_serde_roundtrip_sanskrit_language() {
    let lang = Language::Sanskrit;
    let json = serde_json::to_string(&lang).unwrap();
    let roundtripped: Language = serde_json::from_str(&json).unwrap();
    assert_eq!(lang, roundtripped);
}

#[cfg(feature = "varna")]
#[test]
fn test_phoneme_inventory_arabic() {
    let g2p = G2PEngine::new(Language::Arabic);
    let inv = g2p.phoneme_inventory();
    assert!(inv.has("b"));
    assert!(inv.has("ʔ"));
}

#[cfg(feature = "varna")]
#[test]
fn test_phoneme_inventory_sanskrit() {
    let g2p = G2PEngine::new(Language::Sanskrit);
    let inv = g2p.phoneme_inventory();
    assert!(inv.has("p"));
    assert!(inv.has("ɐ"));
}

#[cfg(feature = "varna")]
#[test]
fn test_phoneme_inventory_english() {
    let g2p = G2PEngine::new(Language::English);
    let inv = g2p.phoneme_inventory();
    assert!(inv.has("p"));
    assert!(inv.has("ʃ"));
    assert!(inv.has("ə"));
}

#[cfg(feature = "varna")]
#[test]
fn test_phoneme_inventory_spanish() {
    let g2p = G2PEngine::new(Language::Spanish);
    let inv = g2p.phoneme_inventory();
    assert!(inv.has("p"));
    assert!(inv.has("θ"));
    assert!(inv.has("a"));
}