TTS/utils/text/__init__.py

146 lines
4.7 KiB
Python
Raw Normal View History

2018-04-03 10:24:57 +00:00
# -*- coding: utf-8 -*-
2018-01-22 14:59:41 +00:00
import re
2019-01-08 16:08:50 +00:00
import phonemizer
2019-01-11 12:21:58 +00:00
from phonemizer.phonemize import phonemize
2018-11-03 22:19:23 +00:00
from utils.text import cleaners
from utils.text.symbols import symbols, phonemes, _phoneme_punctuations, _bos, \
_eos
2018-01-22 14:59:41 +00:00
# Mappings from symbol to numeric ID and vice versa:
2019-07-19 06:46:23 +00:00
_SYMBOL_TO_ID = {s: i for i, s in enumerate(symbols)}
_ID_TO_SYMBOL = {i: s for i, s in enumerate(symbols)}
2018-01-22 14:59:41 +00:00
2019-07-19 06:46:23 +00:00
_PHONEMES_TO_ID = {s: i for i, s in enumerate(phonemes)}
_ID_TO_PHONEMES = {i: s for i, s in enumerate(phonemes)}
2018-11-21 16:05:45 +00:00
2018-01-22 14:59:41 +00:00
# Regular expression matching text enclosed in curly braces:
2019-07-19 06:46:23 +00:00
_CURLY_RE = re.compile(r'(.*?)\{(.+?)\}(.*)')
2018-01-22 14:59:41 +00:00
2019-01-08 16:08:50 +00:00
# Regular expression matchinf punctuations, ignoring empty space
2019-07-19 06:46:23 +00:00
PHONEME_PUNCTUATION_PATTERN = r'['+_phoneme_punctuations+']+'
2018-01-22 14:59:41 +00:00
def text2phone(text, language):
'''
Convert graphemes to phonemes.
'''
2019-02-05 10:55:54 +00:00
seperator = phonemizer.separator.Separator(' |', '', '|')
2019-01-08 16:08:50 +00:00
#try:
2019-07-19 06:46:23 +00:00
punctuations = re.findall(PHONEME_PUNCTUATION_PATTERN, text)
ph = phonemize(text, separator=seperator, strip=False, njobs=1, backend='espeak', language=language)
2019-03-29 16:05:44 +00:00
ph = ph[:-1].strip() # skip the last empty character
2019-01-08 16:08:50 +00:00
# Replace \n with matching punctuations.
2019-07-19 06:46:23 +00:00
if punctuations:
2019-03-29 14:42:46 +00:00
# if text ends with a punctuation.
if text[-1] == punctuations[-1]:
for punct in punctuations[:-1]:
ph = ph.replace('| |\n', '|'+punct+'| |', 1)
try:
ph = ph + punctuations[-1]
except:
print(text)
else:
for punct in punctuations:
ph = ph.replace('| |\n', '|'+punct+'| |', 1)
return ph
2019-01-08 16:08:50 +00:00
def pad_with_eos_bos(phoneme_sequence):
return [_PHONEMES_TO_ID[_bos]] + list(phoneme_sequence) + [_PHONEMES_TO_ID[_eos]]
2019-04-12 14:12:15 +00:00
def phoneme_to_sequence(text, cleaner_names, language, enable_eos_bos=False):
sequence = []
2019-04-18 15:35:20 +00:00
text = text.replace(":", "")
2018-11-21 17:01:35 +00:00
clean_text = _clean_text(text, cleaner_names)
2019-07-19 06:46:23 +00:00
to_phonemes = text2phone(clean_text, language)
if to_phonemes is None:
2019-01-08 16:08:50 +00:00
print("!! After phoneme conversion the result is None. -- {} ".format(clean_text))
2019-03-27 13:56:40 +00:00
# iterate by skipping empty strings - NOTE: might be useful to keep it to have a better intonation.
2019-07-19 06:46:23 +00:00
for phoneme in filter(None, to_phonemes.split('|')):
2019-01-08 16:08:50 +00:00
sequence += _phoneme_to_sequence(phoneme)
2019-02-25 16:20:05 +00:00
# Append EOS char
2019-04-12 14:12:15 +00:00
if enable_eos_bos:
sequence = pad_with_eos_bos(sequence)
2018-11-21 16:05:45 +00:00
return sequence
2019-01-01 19:10:06 +00:00
def sequence_to_phoneme(sequence):
'''Converts a sequence of IDs back to a string'''
result = ''
for symbol_id in sequence:
2019-07-19 06:46:23 +00:00
if symbol_id in _ID_TO_PHONEMES:
s = _ID_TO_PHONEMES[symbol_id]
2019-01-01 19:10:06 +00:00
result += s
return result.replace('}{', ' ')
2018-01-22 14:59:41 +00:00
def text_to_sequence(text, cleaner_names):
'''Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
The text can optionally have ARPAbet sequences enclosed in curly braces embedded
in it. For example, "Turn left on {HH AW1 S S T AH0 N} Street."
Args:
text: string to convert to a sequence
cleaner_names: names of the cleaner functions to run the text through
Returns:
List of integers corresponding to the symbols in the text
'''
sequence = []
2018-01-22 14:59:41 +00:00
# Check for curly braces and treat their contents as ARPAbet:
2019-07-19 06:46:23 +00:00
while text:
m = _CURLY_RE.match(text)
2018-01-22 14:59:41 +00:00
if not m:
sequence += _symbols_to_sequence(_clean_text(text, cleaner_names))
break
sequence += _symbols_to_sequence(
_clean_text(m.group(1), cleaner_names))
sequence += _arpabet_to_sequence(m.group(2))
text = m.group(3)
return sequence
def sequence_to_text(sequence):
'''Converts a sequence of IDs back to a string'''
result = ''
for symbol_id in sequence:
2019-07-19 06:46:23 +00:00
if symbol_id in _ID_TO_SYMBOL:
s = _ID_TO_SYMBOL[symbol_id]
2018-01-22 14:59:41 +00:00
# Enclose ARPAbet back in curly braces:
if len(s) > 1 and s[0] == '@':
s = '{%s}' % s[1:]
result += s
return result.replace('}{', ' ')
def _clean_text(text, cleaner_names):
for name in cleaner_names:
cleaner = getattr(cleaners, name)
if not cleaner:
raise Exception('Unknown cleaner: %s' % name)
text = cleaner(text)
return text
2019-07-19 06:46:23 +00:00
def _symbols_to_sequence(syms):
return [_SYMBOL_TO_ID[s] for s in syms if _should_keep_symbol(s)]
2018-01-22 14:59:41 +00:00
2019-07-19 06:46:23 +00:00
def _phoneme_to_sequence(phons):
return [_PHONEMES_TO_ID[s] for s in list(phons) if _should_keep_phoneme(s)]
2018-11-21 16:05:45 +00:00
2018-01-22 14:59:41 +00:00
def _arpabet_to_sequence(text):
return _symbols_to_sequence(['@' + s for s in text.split()])
def _should_keep_symbol(s):
2019-07-19 06:46:23 +00:00
return s in _SYMBOL_TO_ID and s not in ['~', '^', '_']
2018-11-21 16:05:45 +00:00
2019-01-01 19:10:06 +00:00
def _should_keep_phoneme(p):
2019-07-19 06:46:23 +00:00
return p in _PHONEMES_TO_ID and p not in ['~', '^', '_']