Parakeet/parakeet/frontend/phonectic.py

315 lines
8.4 KiB
Python
Raw Normal View History

# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2021-08-17 15:29:30 +08:00
from abc import ABC
from abc import abstractmethod
2020-10-20 16:06:11 +08:00
from g2p_en import G2p
from g2pM import G2pM
2021-08-17 15:29:30 +08:00
2021-08-25 20:55:24 +08:00
from parakeet.frontend.vocab import Vocab
2021-08-17 15:29:30 +08:00
from parakeet.frontend.normalizer.normalizer import normalize
from parakeet.frontend.punctuation import get_punctuations
# discard opencc untill we find an easy solution to install it on windows
# from opencc import OpenCC
2020-10-20 16:06:11 +08:00
__all__ = ["Phonetics", "English", "EnglishCharacter", "Chinese"]
2020-10-20 16:06:11 +08:00
class Phonetics(ABC):
@abstractmethod
def __call__(self, sentence):
pass
2020-10-20 16:06:11 +08:00
@abstractmethod
def phoneticize(self, sentence):
pass
2020-10-20 16:06:11 +08:00
@abstractmethod
def numericalize(self, phonemes):
pass
2020-10-20 16:06:11 +08:00
class English(Phonetics):
2020-12-18 19:31:44 +08:00
""" Normalize the input text sequence and convert into pronunciation id sequence.
"""
2020-10-20 16:06:11 +08:00
def __init__(self):
self.backend = G2p()
self.phonemes = list(self.backend.phonemes)
self.punctuations = get_punctuations("en")
self.vocab = Vocab(self.phonemes + self.punctuations)
2020-10-20 16:06:11 +08:00
def phoneticize(self, sentence):
2020-12-18 19:31:44 +08:00
""" Normalize the input text sequence and convert it into pronunciation sequence.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
List[str]
The list of pronunciation sequence.
"""
start = self.vocab.start_symbol
end = self.vocab.end_symbol
phonemes = ([] if start is None else [start]) \
2021-08-17 15:29:30 +08:00
+ self.backend(sentence) \
+ ([] if end is None else [end])
add ge2e and tacotron2_aishell3 example (#107) * hacky thing, add tone support for acoustic model * fix experiments for waveflow and wavenet, only write visual log in rank-0 * use emb add in tacotron2 * 1. remove space from numericalized representation; 2. fix decoder paddign mask's unsqueeze dim. * remove bn in postnet * refactoring code * add an option to normalize volume when loading audio. * add an embedding layer. * 1. change the default min value of LogMagnitude to 1e-5; 2. remove stop logit prediction from tacotron2 model. * WIP: baker * add ge2e * fix lstm speaker encoder * fix lstm speaker encoder * fix speaker encoder and add support for 2 more datasets * simplify visualization code * add a simple strategy to support multispeaker for tacotron. * add vctk example for refactored tacotron * fix indentation * fix class name * fix visualizer * fix root path * fix root path * fix root path * fix typos * fix bugs * fix text log extention name * add example for baker and aishell3 * update experiment and display * format code for tacotron_vctk, add plot_waveform to display * add new trainer * minor fix * add global condition support for tacotron2 * add gst layer * add 2 frontend * fix fmax for example/waveflow * update collate function, data loader not does not convert nested list into numpy array. * WIP: add hifigan * WIP:update hifigan * change stft to use conv1d * add audio datasets * change batch_text_id, batch_spec, batch_wav to include valid lengths in the returned value * change wavenet to use on-the-fly prepeocessing * fix typos * resolve conflict * remove imports that are removed * remove files not included in this release * remove imports to deleted modules * move tacotron2_msp * clean code * fix argument order * fix argument name * clean code for data processing * WIP: add README * add more details to thr README, fix some preprocess scripts * add voice cloning notebook * add an optional to alter the loss and model structure of tacotron2, add an alternative config * add plot_multiple_attentions and update visualization code in transformer_tts * format code * remove tacotron2_msp * update tacotron2 from_pretrained, update setup.py * update tacotron2 * update tacotron_aishell3's README * add images for exampels/tacotron2_aishell3's README * update README for examples/ge2e * add STFT back * add extra_config keys into the default config of tacotron * fix typos and docs * update README and doc * update docstrings for tacotron * update doc * update README * add links to downlaod pretrained models * refine READMEs and clean code * add praatio into requirements for running the experiments * format code with pre-commit * simplify text processing code and update notebook
2021-05-13 17:49:50 +08:00
phonemes = [item for item in phonemes if item in self.vocab.stoi]
return phonemes
2020-10-20 16:06:11 +08:00
def numericalize(self, phonemes):
2020-12-18 19:31:44 +08:00
""" Convert pronunciation sequence into pronunciation id sequence.
2021-08-17 15:29:30 +08:00
2020-12-18 19:31:44 +08:00
Parameters
-----------
phonemes: List[str]
The list of pronunciation sequence.
Returns
----------
List[int]
The list of pronunciation id sequence.
"""
ids = [
self.vocab.lookup(item) for item in phonemes
if item in self.vocab.stoi
]
return ids
def reverse(self, ids):
2020-12-18 19:31:44 +08:00
""" Reverse the list of pronunciation id sequence to a list of pronunciation sequence.
2021-08-17 15:29:30 +08:00
2020-12-18 19:31:44 +08:00
Parameters
-----------
ids: List[int]
The list of pronunciation id sequence.
Returns
----------
List[str]
The list of pronunciation sequence.
"""
return [self.vocab.reverse(i) for i in ids]
def __call__(self, sentence):
2020-12-18 19:31:44 +08:00
""" Convert the input text sequence into pronunciation id sequence.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
List[str]
The list of pronunciation id sequence.
"""
return self.numericalize(self.phoneticize(sentence))
@property
def vocab_size(self):
2020-12-18 19:31:44 +08:00
""" Vocab size.
"""
return len(self.vocab)
class EnglishCharacter(Phonetics):
2020-12-18 19:31:44 +08:00
""" Normalize the input text sequence and convert it into character id sequence.
"""
def __init__(self):
self.backend = G2p()
self.graphemes = list(self.backend.graphemes)
self.punctuations = get_punctuations("en")
self.vocab = Vocab(self.graphemes + self.punctuations)
def phoneticize(self, sentence):
2020-12-18 19:31:44 +08:00
""" Normalize the input text sequence.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
str
A text sequence after normalize.
"""
words = normalize(sentence)
return words
def numericalize(self, sentence):
2020-12-18 19:31:44 +08:00
""" Convert a text sequence into ids.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
List[int]
List of a character id sequence.
"""
ids = [
self.vocab.lookup(item) for item in sentence
if item in self.vocab.stoi
]
2020-10-20 16:06:11 +08:00
return ids
2020-10-20 16:06:11 +08:00
def reverse(self, ids):
2020-12-18 19:31:44 +08:00
""" Convert a character id sequence into text.
Parameters
-----------
ids: List[int]
List of a character id sequence.
Returns
----------
str
The input text sequence.
2021-08-17 15:29:30 +08:00
2020-12-18 19:31:44 +08:00
"""
2020-10-20 16:06:11 +08:00
return [self.vocab.reverse(i) for i in ids]
2020-10-20 16:06:11 +08:00
def __call__(self, sentence):
2020-12-18 19:31:44 +08:00
""" Normalize the input text sequence and convert it into character id sequence.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
List[int]
List of a character id sequence.
"""
2020-10-20 16:06:11 +08:00
return self.numericalize(self.phoneticize(sentence))
2020-12-01 18:13:30 +08:00
@property
2020-10-20 16:06:11 +08:00
def vocab_size(self):
2020-12-18 19:31:44 +08:00
""" Vocab size.
"""
2020-10-20 16:06:11 +08:00
return len(self.vocab)
class Chinese(Phonetics):
2020-12-18 19:31:44 +08:00
"""Normalize Chinese text sequence and convert it into ids.
"""
2020-10-20 16:06:11 +08:00
def __init__(self):
# self.opencc_backend = OpenCC('t2s.json')
2020-10-20 16:06:11 +08:00
self.backend = G2pM()
self.phonemes = self._get_all_syllables()
self.punctuations = get_punctuations("cn")
self.vocab = Vocab(self.phonemes + self.punctuations)
2020-10-20 16:06:11 +08:00
def _get_all_syllables(self):
all_syllables = set([
syllable for k, v in self.backend.cedict.items() for syllable in v
])
2020-10-20 16:06:11 +08:00
return list(all_syllables)
def phoneticize(self, sentence):
2020-12-18 19:31:44 +08:00
""" Normalize the input text sequence and convert it into pronunciation sequence.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
List[str]
The list of pronunciation sequence.
"""
# simplified = self.opencc_backend.convert(sentence)
simplified = sentence
2020-10-20 16:06:11 +08:00
phonemes = self.backend(simplified)
start = self.vocab.start_symbol
end = self.vocab.end_symbol
phonemes = ([] if start is None else [start]) \
2021-08-17 15:29:30 +08:00
+ phonemes \
+ ([] if end is None else [end])
2020-10-20 16:06:11 +08:00
return self._filter_symbols(phonemes)
2020-10-20 16:06:11 +08:00
def _filter_symbols(self, phonemes):
cleaned_phonemes = []
for item in phonemes:
if item in self.vocab.stoi:
cleaned_phonemes.append(item)
else:
for char in item:
if char in self.vocab.stoi:
cleaned_phonemes.append(char)
return cleaned_phonemes
2020-10-20 16:06:11 +08:00
def numericalize(self, phonemes):
2020-12-18 19:31:44 +08:00
""" Convert pronunciation sequence into pronunciation id sequence.
2021-08-17 15:29:30 +08:00
2020-12-18 19:31:44 +08:00
Parameters
-----------
phonemes: List[str]
The list of pronunciation sequence.
Returns
----------
List[int]
The list of pronunciation id sequence.
"""
2020-10-20 16:06:11 +08:00
ids = [self.vocab.lookup(item) for item in phonemes]
return ids
2020-10-20 16:06:11 +08:00
def __call__(self, sentence):
2020-12-18 19:31:44 +08:00
""" Convert the input text sequence into pronunciation id sequence.
Parameters
-----------
sentence: str
The input text sequence.
Returns
----------
List[str]
The list of pronunciation id sequence.
"""
2020-10-20 16:06:11 +08:00
return self.numericalize(self.phoneticize(sentence))
2020-12-01 18:13:30 +08:00
@property
2020-10-20 16:06:11 +08:00
def vocab_size(self):
2020-12-18 19:31:44 +08:00
""" Vocab size.
"""
2020-10-20 16:06:11 +08:00
return len(self.vocab)
2020-10-20 16:06:11 +08:00
def reverse(self, ids):
2020-12-18 19:31:44 +08:00
""" Reverse the list of pronunciation id sequence to a list of pronunciation sequence.
2021-08-17 15:29:30 +08:00
2020-12-18 19:31:44 +08:00
Parameters
-----------
ids: List[int]
The list of pronunciation id sequence.
Returns
----------
List[str]
The list of pronunciation sequence.
"""
2020-10-20 16:06:11 +08:00
return [self.vocab.reverse(i) for i in ids]