TTS/utils/text/__init__.py

173 lines
5.1 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
2018-11-03 22:19:23 +00:00
from utils.text import cleaners
2019-01-01 19:10:06 +00:00
from utils.text.symbols import symbols, phonemes, _punctuations
2018-01-22 14:59:41 +00:00
# Mappings from symbol to numeric ID and vice versa:
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
_id_to_symbol = {i: s for i, s in enumerate(symbols)}
2018-11-21 16:05:45 +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-01-22 14:59:41 +00:00
# Regular expression matching text enclosed in curly braces:
_curly_re = re.compile(r'(.*?)\{(.+?)\}(.*)')
2019-01-08 16:08:50 +00:00
# Regular expression matchinf punctuations, ignoring empty space
pat = r'['+_punctuations[:-1]+']+'
2018-01-22 14:59:41 +00:00
def text2phone(text):
'''
Convert graphemes to phonemes.
'''
2019-01-08 16:08:50 +00:00
seperator = phonemizer.separator.Separator(' ', '', '|')
#try:
punctuations = re.findall(pat, text)
ph = phonemizer.phonemize(text, separator=seperator, strip=False, njobs=1, backend='espeak', language='en-us')
# Replace \n with matching punctuations.
2019-01-08 23:23:13 +00:00
if len(punctuations) > 0:
for punct in punctuations[:-1]:
ph = ph.replace(' \n', punct+'| ', 1)
try:
ph = ph[:-1] + punctuations[-1]
except:
print(text)
return ph
2019-01-08 16:08:50 +00:00
2019-01-01 19:10:06 +00:00
def phoneme_to_sequence(text, cleaner_names):
2018-11-21 17:01:35 +00:00
'''
TODO: This ignores punctuations
'''
2018-11-21 16:05:45 +00:00
sequence = []
2018-11-21 17:01:35 +00:00
clean_text = _clean_text(text, cleaner_names)
2019-01-08 16:08:50 +00:00
phonemes = text2phone(clean_text)
2019-01-08 23:23:13 +00:00
# print(phonemes.replace('|', ''))
2019-01-08 16:08:50 +00:00
if phonemes is None:
print("!! After phoneme conversion the result is None. -- {} ".format(clean_text))
for phoneme in phonemes.split('|'):
2019-01-03 12:43:07 +00:00
# print(word, ' -- ', phonemes_text)
2019-01-08 16:08:50 +00:00
sequence += _phoneme_to_sequence(phoneme)
2019-01-01 19:10:06 +00:00
# Aeepnd EOS char
2018-11-21 16:05:45 +00:00
sequence.append(_phonemes_to_id['~'])
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:
if symbol_id in _id_to_phonemes:
s = _id_to_phonemes[symbol_id]
print(s)
result += s
return result.replace('}{', ' ')
2018-11-21 17:01:35 +00:00
def phonem_to_sequence(text, cleaner_names):
'''
TODO: This ignores punctuations
'''
2018-11-21 16:05:45 +00:00
sequence = []
2018-11-21 17:01:35 +00:00
clean_text = _clean_text(text, cleaner_names)
for word in clean_text.split():
phonems_text = text2phone(word)
if phonems_text == None:
continue
sequence += _phonem_to_sequence(phonems_text)
sequence.append(_phonemes_to_id[' '])
2018-11-21 16:05:45 +00:00
sequence.append(_phonemes_to_id['~'])
return sequence
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 = []
# Check for curly braces and treat their contents as ARPAbet:
while len(text):
m = _curly_re.match(text)
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)
# Append EOS token
sequence.append(_symbol_to_id['~'])
return sequence
def sequence_to_text(sequence):
'''Converts a sequence of IDs back to a string'''
result = ''
for symbol_id in sequence:
if symbol_id in _id_to_symbol:
s = _id_to_symbol[symbol_id]
# Enclose ARPAbet back in curly braces:
if len(s) > 1 and s[0] == '@':
s = '{%s}' % s[1:]
result += s
return result.replace('}{', ' ')
2018-11-21 17:01:35 +00:00
def sequence_to_phonem(sequence):
'''Converts a sequence of IDs back to a string'''
result = ''
for symbol_id in sequence:
if symbol_id in _id_to_phonemes:
s = _id_to_phonemes[symbol_id]
print(s)
result += s
return result.replace('}{', ' ')
2018-01-22 14:59:41 +00:00
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
def _symbols_to_sequence(symbols):
return [_symbol_to_id[s] for s in symbols if _should_keep_symbol(s)]
2018-11-21 17:01:35 +00:00
<<<<<<< HEAD
2019-01-01 19:10:06 +00:00
def _phoneme_to_sequence(phonemes):
2019-01-08 16:08:50 +00:00
return [_phonemes_to_id[s] for s in list(phonemes) if _should_keep_phoneme(s)]
2018-11-21 17:01:35 +00:00
=======
def _phonem_to_sequence(phonemes):
return [_phonemes_to_id[s] for s in phonemes.split(" ") if _should_keep_phonem(s)]
>>>>>>> phonem updates
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):
return s in _symbol_to_id and s is not '_' and s is not '~'
2018-11-21 16:05:45 +00:00
2019-01-01 19:10:06 +00:00
def _should_keep_phoneme(p):
2018-11-21 16:05:45 +00:00
return p in _phonemes_to_id and p is not '_' and p is not '~'