forked from rhasspy/piper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonemize-some-sentences.py
More file actions
27 lines (21 loc) · 1004 Bytes
/
phonemize-some-sentences.py
File metadata and controls
27 lines (21 loc) · 1004 Bytes
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
import json
from piper_phonemize import phonemize_espeak, phoneme_ids_espeak, get_espeak_map
def phonemize_sentence(text, language="en-us"):
# Phonemize the input sentence
phonemes = phonemize_espeak(text, language)
# Flatten the phoneme list (espeak might return a list of lists for sentences)
phonemes_flat = [phon for sublist in phonemes for phon in sublist]
# Map phonemes to phoneme IDs
phoneme_ids = phoneme_ids_espeak(phonemes_flat)
# Create the output dictionary
output = {
"text": text,
"phonemes": phonemes_flat,
"phoneme_ids": phoneme_ids
}
# Return as a one-line JSON string for jsonl files
return json.dumps(output, ensure_ascii=False, separators=(',', ':'))
# Example usage
sentence = "cowboy one, this is kenway. bandit bearing one two three, five miles, ten thousand feet. scramble to intercept."
phonemized_output = phonemize_sentence(sentence)
print(phonemized_output)