async def process(self, text: str, lang: str) -> Union[str, PhonemeList]: """Beeps out parts of the text that are tagged with double asterisks. It basicaly replaces the opening and closing asterisk with two opening and closing 'stop words' then finds the phonemic form of these two and replaces the phonems inside with an equivalently long beep""" occ_list = re.findall(r"\*\*.+?\*\*", text) if occ_list: # replace the "**text**" by "king text gink" tagged_occ_list = [" king %s gink " % occ.strip("*") for occ in occ_list] for occ, tagged_occ in zip(occ_list, tagged_occ_list): text = text.replace(occ, tagged_occ) # getting the phonemic form of the text phonemes = await self.renderer.string_to_phonemes(text, lang, self.voice_params) # then using a simple state machine (in_beep is the state), replaces the phonems between # the right phonemic occurence with the phonems of a beep in_beep = False output, buffer = PhonemeList([]), PhonemeList([]) while phonemes: if PhonemeList(phonemes[:3]).phonemes_str == self._tags_phonems[lang][0] and not in_beep: in_beep, buffer = True, PhonemeList([]) phonemes = PhonemeList(phonemes[3:]) elif PhonemeList(phonemes[:4]).phonemes_str == self._tags_phonems[lang][1] and in_beep: in_beep = False # creating a beep of the buffer's duration if buffer: output += self._gen_beep(sum([pho.duration for pho in buffer]), lang) phonemes = phonemes[4:] elif not in_beep: output.append(phonemes.pop(0)) elif in_beep: buffer.append(phonemes.pop(0)) return output else: return text
def find(self, phoneme_list: PhonemeList, original_string: str): """Recursively, through the tree, tries to find a good rhyme that is *not* equal to the input word (here passed as an argument in original string""" if not phoneme_list: return self.find_random() current_pho = phoneme_list.pop() if current_pho in self.children: current_child = self.children[current_pho] curr_child_output = current_child.find(phoneme_list, original_string) if curr_child_output is not None: return curr_child_output rnd_child = self.find_random() if isinstance(rnd_child, Leaf) and levenshtein( seq1=original_string, seq2=rnd_child.text) <= 2: return None else: return rnd_child #nothing worked