piper-plus-g2p 0.2.0

Multilingual G2P (Grapheme-to-Phoneme) for TTS — eSpeak-ng free
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
//! Phoneme token-to-ID conversion.
//!
//! Converts phoneme token strings to phoneme_id integers using the
//! phoneme_id_map from config.json.

use crate::error::G2pError;
use crate::phonemizer::PhonemeIdMap;

use crate::phonemizer::ProsodyFeature;
use crate::phonemizer::ProsodyInfo;
use crate::token_map::token_to_pua;

/// Convert a sequence of phoneme token strings to phoneme IDs.
///
/// Each token is a single character (regular or PUA). The token is looked up
/// in the phoneme_id_map to get the corresponding integer ID(s).
pub fn tokens_to_ids(
    tokens: &[String],
    phoneme_id_map: &PhonemeIdMap,
) -> Result<Vec<i64>, G2pError> {
    let mut ids = Vec::with_capacity(tokens.len() * 2);
    for token in tokens {
        match phoneme_id_map.get(token) {
            Some(id_list) => ids.extend(id_list.iter().copied()),
            None => {
                return Err(G2pError::PhonemeIdNotFound {
                    phoneme: token.clone(),
                });
            }
        }
    }
    Ok(ids)
}

/// Convert prosody info list to prosody features array (for ONNX input).
/// Each `ProsodyInfo` becomes `[a1, a2, a3]`. `None` becomes `[0, 0, 0]`.
pub fn prosody_to_features(prosody: &[Option<ProsodyInfo>]) -> Vec<ProsodyFeature> {
    prosody
        .iter()
        .map(|p| match p {
            Some(info) => [info.a1, info.a2, info.a3],
            None => [0, 0, 0],
        })
        .collect()
}

/// Encoding mode for handling unknown tokens.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum UnknownTokenMode {
    /// Raise an error on unknown tokens (strict mode).
    Strict,
    /// Skip unknown tokens with a warning log (default).
    #[default]
    Skip,
}

/// High-level encoder that converts IPA token sequences into
/// Piper-compatible phoneme ID arrays with BOS/EOS/PAD insertion.
pub struct PiperEncoder {
    id_map: PhonemeIdMap,
    mode: UnknownTokenMode,
    bos_id: i64,
    eos_id: i64,
    pad_id: i64,
}

impl PiperEncoder {
    /// Create a new encoder from a phoneme ID map.
    pub fn new(id_map: PhonemeIdMap, mode: UnknownTokenMode) -> Result<Self, G2pError> {
        let bos_id = id_map
            .get("^")
            .and_then(|ids| ids.first().copied())
            .ok_or_else(|| G2pError::Phonemize("phoneme_id_map missing '^' (BOS)".into()))?;
        let eos_id = id_map
            .get("$")
            .and_then(|ids| ids.first().copied())
            .ok_or_else(|| G2pError::Phonemize("phoneme_id_map missing '$' (EOS)".into()))?;
        let pad_id = id_map
            .get("_")
            .and_then(|ids| ids.first().copied())
            .ok_or_else(|| G2pError::Phonemize("phoneme_id_map missing '_' (PAD)".into()))?;
        Ok(Self {
            id_map,
            mode,
            bos_id,
            eos_id,
            pad_id,
        })
    }

    /// Resolve the EOS phoneme ID for the given token.
    ///
    /// - `None` → default EOS (`"$"`)
    /// - `Some(token)` → look up the token in the id_map (direct, then PUA)
    fn resolve_eos_id(&self, eos_token: Option<&str>) -> Result<i64, G2pError> {
        match eos_token {
            None => Ok(self.eos_id),
            Some(token) => {
                // Try direct lookup first
                if let Some(&id) = self.id_map.get(token).and_then(|ids| ids.first()) {
                    return Ok(id);
                }
                // Try PUA mapping
                if let Some(pua_char) = token_to_pua(token) {
                    let pua_str = pua_char.to_string();
                    if let Some(&id) = self.id_map.get(&pua_str).and_then(|ids| ids.first()) {
                        return Ok(id);
                    }
                }
                Err(G2pError::PhonemeIdNotFound {
                    phoneme: token.to_string(),
                })
            }
        }
    }

    /// Encode IPA tokens to phoneme IDs with BOS/EOS/PAD insertion.
    pub fn encode(&self, tokens: &[String]) -> Result<Vec<i64>, G2pError> {
        self.encode_with_eos(tokens, None)
    }

    /// Encode IPA tokens with a custom EOS token.
    ///
    /// - `eos_token = None` → default EOS (`"$"`)
    /// - `eos_token = Some("?")` → look up `"?"` in id_map
    /// - `eos_token = Some("?!")` → look up PUA-mapped `"?!"` in id_map
    pub fn encode_with_eos(
        &self,
        tokens: &[String],
        eos_token: Option<&str>,
    ) -> Result<Vec<i64>, G2pError> {
        let (ids, _) = self.encode_with_prosody_and_eos(tokens, &[], eos_token)?;
        Ok(ids)
    }

    /// Encode IPA tokens with prosody alignment.
    pub fn encode_with_prosody(
        &self,
        tokens: &[String],
        prosody: &[Option<ProsodyInfo>],
    ) -> Result<(Vec<i64>, Vec<ProsodyFeature>), G2pError> {
        self.encode_with_prosody_and_eos(tokens, prosody, None)
    }

    /// Encode IPA tokens with prosody alignment and a custom EOS token.
    pub fn encode_with_prosody_and_eos(
        &self,
        tokens: &[String],
        prosody: &[Option<ProsodyInfo>],
        eos_token: Option<&str>,
    ) -> Result<(Vec<i64>, Vec<ProsodyFeature>), G2pError> {
        let resolved_eos = self.resolve_eos_id(eos_token)?;
        let mut ids = Vec::with_capacity(tokens.len() * 3 + 3);
        let mut pros = Vec::with_capacity(tokens.len() * 3 + 3);

        // BOS + PAD
        ids.push(self.bos_id);
        pros.push([0, 0, 0]);
        ids.push(self.pad_id);
        pros.push([0, 0, 0]);

        for (i, token) in tokens.iter().enumerate() {
            // If the token has a PUA mapping, use the single PUA char;
            // otherwise iterate the chars of the original token.
            let mapped: String = match token_to_pua(token) {
                Some(pua_char) => pua_char.to_string(),
                None => token.clone(),
            };
            for ch in mapped.chars() {
                let ch_str = ch.to_string();
                match self.id_map.get(&ch_str) {
                    Some(id_list) => {
                        let p = prosody.get(i).and_then(|o| o.as_ref());
                        let feat = match p {
                            Some(info) => [info.a1, info.a2, info.a3],
                            None => [0, 0, 0],
                        };
                        for &id in id_list {
                            ids.push(id);
                            pros.push(feat);
                        }
                    }
                    None => match self.mode {
                        UnknownTokenMode::Strict => {
                            return Err(G2pError::PhonemeIdNotFound { phoneme: ch_str });
                        }
                        UnknownTokenMode::Skip => {
                            tracing::warn!(phoneme = %ch_str, "unknown symbol dropped");
                        }
                    },
                }
            }
            ids.push(self.pad_id);
            pros.push([0, 0, 0]);
        }

        ids.push(resolved_eos);
        pros.push([0, 0, 0]);
        Ok((ids, pros))
    }
}

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

    /// Helper: build a PhonemeIdMap from (key, ids) pairs.
    fn make_map(entries: &[(&str, &[i64])]) -> PhonemeIdMap {
        let mut map = HashMap::new();
        for (key, ids) in entries {
            map.insert(key.to_string(), ids.to_vec());
        }
        map
    }

    #[test]
    fn test_basic_token_to_id() {
        let map = make_map(&[
            ("^", &[1]),
            ("_", &[0]),
            ("$", &[2]),
            ("a", &[15]),
            ("k", &[30]),
        ]);
        let tokens: Vec<String> = vec!["^", "a", "_", "k", "$"]
            .into_iter()
            .map(String::from)
            .collect();

        let ids = tokens_to_ids(&tokens, &map).unwrap();
        assert_eq!(ids, vec![1, 15, 0, 30, 2]);
    }

    #[test]
    fn test_pua_character_conversion() {
        // PUA char U+E000 represents "a:" (long vowel)
        let map = make_map(&[("^", &[1]), ("\u{E000}", &[45]), ("$", &[2])]);
        let tokens: Vec<String> = vec!["^", "\u{E000}", "$"]
            .into_iter()
            .map(String::from)
            .collect();

        let ids = tokens_to_ids(&tokens, &map).unwrap();
        assert_eq!(ids, vec![1, 45, 2]);
    }

    #[test]
    fn test_unknown_phoneme_error() {
        let map = make_map(&[("a", &[15])]);
        let tokens: Vec<String> = vec!["a", "Z"].into_iter().map(String::from).collect();

        let result = tokens_to_ids(&tokens, &map);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("Z"),
            "error message should contain the unknown phoneme 'Z', got: {msg}"
        );
    }

    #[test]
    fn test_prosody_conversion() {
        let prosody = vec![
            Some(ProsodyInfo {
                a1: -2,
                a2: 1,
                a3: 5,
            }),
            None,
            Some(ProsodyInfo {
                a1: 0,
                a2: 3,
                a3: 4,
            }),
        ];

        let features = prosody_to_features(&prosody);
        assert_eq!(features.len(), 3);
        assert_eq!(features[0], [-2, 1, 5]);
        assert_eq!(features[1], [0, 0, 0]);
        assert_eq!(features[2], [0, 3, 4]);
    }

    #[test]
    fn test_multi_id_mapping() {
        // Some phoneme_id_map entries map to multiple IDs
        let map = make_map(&[("a", &[10, 11]), ("b", &[20])]);
        let tokens: Vec<String> = vec!["a", "b"].into_iter().map(String::from).collect();

        let ids = tokens_to_ids(&tokens, &map).unwrap();
        assert_eq!(ids, vec![10, 11, 20]);
    }

    #[test]
    fn test_empty_tokens() {
        let map = make_map(&[("a", &[1])]);
        let tokens: Vec<String> = vec![];

        let ids = tokens_to_ids(&tokens, &map).unwrap();
        assert!(ids.is_empty());
    }

    #[test]
    fn test_piper_encoder_basic() {
        let map = make_map(&[
            ("^", &[1]),
            ("_", &[0]),
            ("$", &[2]),
            ("a", &[15]),
            ("k", &[30]),
        ]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a", "k"].into_iter().map(String::from).collect();
        let ids = encoder.encode(&tokens).unwrap();
        assert_eq!(ids[0], 1); // BOS
        assert_eq!(*ids.last().unwrap(), 2); // EOS
        assert!(ids.contains(&15));
        assert!(ids.contains(&30));
    }

    #[test]
    fn test_piper_encoder_strict_error() {
        let map = make_map(&[("^", &[1]), ("_", &[0]), ("$", &[2]), ("a", &[15])]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Strict).unwrap();
        let tokens: Vec<String> = vec!["a", "Z"].into_iter().map(String::from).collect();
        assert!(encoder.encode(&tokens).is_err());
    }

    #[test]
    fn test_piper_encoder_skip_unknown() {
        let map = make_map(&[("^", &[1]), ("_", &[0]), ("$", &[2]), ("a", &[15])]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a", "Z"].into_iter().map(String::from).collect();
        let ids = encoder.encode(&tokens).unwrap();
        assert!(ids.contains(&15));
    }

    #[test]
    fn test_piper_encoder_missing_bos() {
        let map = make_map(&[("_", &[0]), ("$", &[2])]);
        assert!(PiperEncoder::new(map, UnknownTokenMode::Skip).is_err());
    }

    #[test]
    fn test_encode_with_default_eos() {
        let map = make_map(&[
            ("^", &[1]),
            ("_", &[0]),
            ("$", &[2]),
            ("a", &[15]),
            ("k", &[30]),
        ]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a", "k"].into_iter().map(String::from).collect();
        let ids_default = encoder.encode(&tokens).unwrap();
        let ids_none = encoder.encode_with_eos(&tokens, None).unwrap();
        assert_eq!(ids_default, ids_none);
    }

    #[test]
    fn test_encode_with_question_eos() {
        let map = make_map(&[
            ("^", &[1]),
            ("_", &[0]),
            ("$", &[2]),
            ("?", &[99]),
            ("a", &[15]),
        ]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a"].into_iter().map(String::from).collect();
        let ids = encoder.encode_with_eos(&tokens, Some("?")).unwrap();
        // Last element should be "?" ID (99), not default EOS (2)
        assert_eq!(*ids.last().unwrap(), 99);
        // BOS should still be first
        assert_eq!(ids[0], 1);
    }

    #[test]
    fn test_encode_with_pua_eos() {
        // "?!" maps to PUA U+E016 via token_to_pua
        let pua_char = crate::token_map::token_to_pua("?!").unwrap();
        let pua_str = pua_char.to_string();
        let map = make_map(&[
            ("^", &[1]),
            ("_", &[0]),
            ("$", &[2]),
            (&pua_str, &[88]),
            ("a", &[15]),
        ]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a"].into_iter().map(String::from).collect();
        let ids = encoder.encode_with_eos(&tokens, Some("?!")).unwrap();
        assert_eq!(*ids.last().unwrap(), 88);
    }

    #[test]
    fn test_encode_with_prosody_and_eos() {
        let map = make_map(&[
            ("^", &[1]),
            ("_", &[0]),
            ("$", &[2]),
            ("?", &[99]),
            ("a", &[15]),
        ]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a"].into_iter().map(String::from).collect();
        let prosody = vec![Some(ProsodyInfo {
            a1: -2,
            a2: 1,
            a3: 5,
        })];
        let (ids, pros) = encoder
            .encode_with_prosody_and_eos(&tokens, &prosody, Some("?"))
            .unwrap();
        // Last ID should be custom EOS
        assert_eq!(*ids.last().unwrap(), 99);
        // Prosody for EOS should be zero
        assert_eq!(*pros.last().unwrap(), [0, 0, 0]);
        // ids and pros must have same length
        assert_eq!(ids.len(), pros.len());
    }

    #[test]
    fn test_resolve_eos_invalid() {
        let map = make_map(&[("^", &[1]), ("_", &[0]), ("$", &[2]), ("a", &[15])]);
        let encoder = PiperEncoder::new(map, UnknownTokenMode::Skip).unwrap();
        let tokens: Vec<String> = vec!["a"].into_iter().map(String::from).collect();
        let result = encoder.encode_with_eos(&tokens, Some("NONEXISTENT"));
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(
            msg.contains("NONEXISTENT"),
            "error should mention the unknown token, got: {msg}"
        );
    }
}