def read_text(self, text: str, **config) -> None: """Reads out text.""" text = self.clean_text(text) text = TextParser.escape_tags(text) split_text = TextParser.split_text(text) rate = config.get('rate') volume = config.get('volume') voice = config.get('voice') assert voice, "Voice needs to be provided" # TODO: Does it? ssml = SSML(text, rate=rate, volume=volume) if self._cached_ssml == ssml and self._cached_voice == voice: self._logger.debug("Playing cached file") filepaths = self._cached_filepaths else: self._logger.debug("Re_cached_textquest from Polly") filepaths = [] # TODO: This should obviously be asynchronous! for idx, parted_text in enumerate(split_text): parted_ssml = SSML(parted_text, rate=rate, volume=volume) response = self.ask_polly(str(parted_ssml), voice) filename = create_filename(AbstractSpeaker.TMP_FILEPATH, idx) saved_filepath = save_mp3(response["AudioStream"].read(), filename) filepaths.append(saved_filepath) self.save_cache(ssml, filepaths, voice) self.play_files(filepaths) return
def test_split_text_above_3000_below_6000(self): document = 200 * self.sentece_24chars self.assertEqual(len(document), 4800, "Document should have 4800 chars length") split_text = list(TextParser.split_text(document)) self.assertEqual(len(split_text), 2, "Two parts")
def test_split_text_below_3000(self): document = 10 * self.sentece_24chars self.assertEqual(len(document), 240, "Document should have 240 chars lenght") split_text = TextParser.split_text(document) list_split_text = list(split_text) self.assertEqual(len(list_split_text), 1, "Only one part") self.assertEqual(list_split_text[0], document, "First part is the document")
def test_split_text_above_6000(self): document = "no dots in this text " * 300 # 21*300 = 6300 self.assertEqual(len(document), 6300, "Document should have 6300 chars length") split_text = list(TextParser.split_text(document)) self.assertEqual(len(split_text), 3, "Two parts") self.assertEqual(len(split_text[0]), len(split_text[1]), "Both parts should have the same length") self.assertEqual(len(split_text[2]), 300, "Simple maths: 6300 - 6000 = 300")
def test_split_text_generator_type(self): document = self.sentece_24chars split_text = TextParser.split_text(document) self.assertIsInstance(split_text, types.GeneratorType, "Return generator type")