From c401cc5146158ece9c3e242894327a9c1d2730df Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 4 Jul 2022 02:10:46 -0400 Subject: [PATCH] tests --- tts/text/pipeline/english_pipeline_v1.py | 26 ++++++++++++++++--- tts/text/pipeline/test_english_pipeline_v1.py | 21 +++++++++++++-- tts/text/sequencing/english_symbols_v1.py | 3 +++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/tts/text/pipeline/english_pipeline_v1.py b/tts/text/pipeline/english_pipeline_v1.py index 2e704a9..8f67066 100644 --- a/tts/text/pipeline/english_pipeline_v1.py +++ b/tts/text/pipeline/english_pipeline_v1.py @@ -1,13 +1,14 @@ from typing import List from tts.text.input_parsing.parser import ArpabetSequence, GraphemeSequence, ParsedSequence, parse_grapheme_and_arpabet_sequence from tts.text.normalization.english_normalization import EnglishNormalization -from tts.text.phonetization.cmudict import CMUDict +from tts.text.phonetization.cmudict import DEFAULT_CMUDICT_PATH, CMUDict from tts.text.phonetization.english_phonetization import EnglishPhonetization from tts.text.pipeline.base_pipeline import BasePipeline +from tts.text.sequencing.english_symbols_v1 import SPACE_INTEGER_ENCODING, arpabet_to_sequence, symbols_to_sequence _ENGLISH_NORMALIZER = EnglishNormalization() -_ENGLISH_DICTIONARY = CMUDict() +_ENGLISH_DICTIONARY = CMUDict(DEFAULT_CMUDICT_PATH) _ENGLISH_PHONETIZER = EnglishPhonetization(_ENGLISH_DICTIONARY) class EnglishPipelineV1(BasePipeline): @@ -46,4 +47,23 @@ class EnglishPipelineV1(BasePipeline): return updated_sequence def _convert_to_integer_sequence(self, parsed_sequence: ParsedSequence) -> List[int]: - pass \ No newline at end of file + integer_sequence = [] + last_index = len(parsed_sequence) - 1 + + for i, subsequence in enumerate(parsed_sequence): + integer_subsequence = [] + + if isinstance(subsequence, ArpabetSequence): + integer_subsequence = arpabet_to_sequence(subsequence.sequence) + elif isinstance(subsequence, GraphemeSequence): + integer_subsequence = symbols_to_sequence(subsequence.sequence) + + if integer_subsequence: + integer_sequence.extend(integer_subsequence) + + if i < last_index and integer_sequence[-1] != SPACE_INTEGER_ENCODING: + # Separate words with single spaces if not already present. + integer_sequence.append(SPACE_INTEGER_ENCODING) + + return integer_sequence + \ No newline at end of file diff --git a/tts/text/pipeline/test_english_pipeline_v1.py b/tts/text/pipeline/test_english_pipeline_v1.py index 938cfc2..3cfda4f 100644 --- a/tts/text/pipeline/test_english_pipeline_v1.py +++ b/tts/text/pipeline/test_english_pipeline_v1.py @@ -21,6 +21,23 @@ class TestEnglishPipelineV1: def test_two_words(self): pipeline = EnglishPipelineV1() output = pipeline.user_input_to_sequence("hello hello") - assert output == [106, 73, 117, 123, 11, 106, 73, 117, 123] + assert output == [ + 106, 73, 117, 123, # NB: Arpabet encoded + 11, # Space + 106, 73, 117, 123, # Arpabet encoded + ] - \ No newline at end of file + def test_words_and_punctuation(self): + pipeline = EnglishPipelineV1() + output = pipeline.user_input_to_sequence("hello friends, welcome!") + assert output == [ + 106, 73, 117, 123, # NB: Arpabet encoded + 11, # Space + 104, 130, 94, 119, 90, 146, + 11, # Space (TODO: Get rid of extraneous spaces with richer parsing) + 6, # Comma + 11, # Space + 144, 94, 117, 116, 73, 118, + 11, # Space + 2, # Exclamation mark + ] diff --git a/tts/text/sequencing/english_symbols_v1.py b/tts/text/sequencing/english_symbols_v1.py index d064316..3ff0d3d 100644 --- a/tts/text/sequencing/english_symbols_v1.py +++ b/tts/text/sequencing/english_symbols_v1.py @@ -34,6 +34,9 @@ symbols = [_pad] + list(_special) + list(_punctuation) + list(_letters) + _arpab _symbol_to_id = {s: i for i, s in enumerate(symbols)} _id_to_symbol = {i: s for i, s in enumerate(symbols)} +# Spaces can be injected when joining words. +SPACE_INTEGER_ENCODING = _symbol_to_id[' '] + def symbols_to_sequence(symbols: str) -> List[int]: return [_symbol_to_id[s] for s in symbols if _should_keep_symbol(s)]