File size: 953 Bytes
3e989b2 |
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 |
from gruut import sentences
from collections.abc import Iterable
class PhonemeConverter:
def phonemize(self, text):
pass
class GruutPhonemizer(PhonemeConverter):
def phonemize(self, text, lang='en-us'):
phonemized = []
for sent in sentences(text, lang=lang):
for word in sent:
if isinstance(word.phonemes, Iterable):
phonemized.append(''.join(word.phonemes))
elif isinstance(word.phonemes, str):
phonemized.append(word.phonemes)
phonemized_text = ' '.join(phonemized)
return phonemized_text
# class YourPhonemizer(Phonemizer):
# def phonemize(self, text):
# ...
class PhonemeConverterFactory:
@staticmethod
def load_phoneme_converter(name: str, **kwargs):
if name == 'gruut':
return GruutPhonemizer()
else:
raise ValueError("Invalid phoneme converter.") |