Grapheme-to-Phoneme Converter
speechline.utils.g2p
g2p_en(text)
Convert English text to string of phonemes via gruut.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str |
English text to convert. |
required |
Returns:
Type | Description |
---|---|
str |
Phoneme string. |
Source code in speechline/utils/g2p.py
def g2p_en(text: str) -> str:
"""
Convert English text to string of phonemes via gruut.
Args:
text (str):
English text to convert.
Returns:
str:
Phoneme string.
"""
phonemes = []
for words in sentences(text):
for word in words:
if word.is_major_break or word.is_minor_break:
phonemes += word.text
elif word.phonemes:
phonemes += word.phonemes
return " ".join(phonemes)
g2p_id(text)
Convert Indonesian text to string of phonemes via g2p_id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str |
Indonesian text to convert. |
required |
Returns:
Type | Description |
---|---|
str |
Phoneme string. |
Source code in speechline/utils/g2p.py
get_g2p(language)
Gets the corresponding g2p function given language
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
language |
str |
Language code. Can be in the form of |
required |
Exceptions:
Type | Description |
---|---|
NotImplementedError |
Language has no g2p function implemented yet. |
Returns:
Type | Description |
---|---|
Callable |
G2p callable function. |
Source code in speechline/utils/g2p.py
def get_g2p(language: str) -> Callable:
"""
Gets the corresponding g2p function given `language`.
Args:
language (str):
Language code. Can be in the form of `en-US` or simply `en`.
Raises:
NotImplementedError: Language has no g2p function implemented yet.
Returns:
Callable:
G2p callable function.
"""
LANG2G2P = {
"en": g2p_en,
"id": g2p_id,
}
if language.lower() not in LANG2G2P:
raise NotImplementedError(f"{language} has no g2p function yet!")
return LANG2G2P[language.lower()]