from xml.sax.saxutils import escape # from html import escape import youdaoDict,EnglishDictAI,DictUtils ZH_VOICE = 'zh-CN-XiaochenMultilingualNeural' # EN_VOICE = 'en-US-AvaMultilingualNeural'#太喜悦了,不行 EN_VOICE = "en-US-JaneNeural" def generalTTS(text, voice=''): # Escape the text content and the voice name (in case it ever contains special chars) wordsSpeech = f''' {escape(text)} ''' return wordsSpeech def wordsPronunciation(phraseList): # Escape each phrase in the list *before* joining them escaped_phrases = (escape(phrase) for phrase in phraseList) joined_phrases = ''.join(escaped_phrases) wordsSpeech = f''' {joined_phrases} ''' return wordsSpeech def chineseDefinitionTTS(phraseList): phraseMeaningSSML = '' dictResultList = [] for phrase in phraseList: dictResult = youdaoDict.query(phrase) if not dictResult.get('definition'): dictResult = EnglishDictAI.query(phrase) dictResultList.append(dictResult) chineseDefinition = dictResult.get('definition', '') # Default to empty string if no definition # Ensure definition is a string before escaping (in case it's None) spokenChineseDefinition = DictUtils.toSpokendefinition(chineseDefinition) if spokenChineseDefinition is None: spokenChineseDefinition = "" # Escape all variable content being inserted into the SSML escaped_phrase = escape(phrase) escaped_spell_out = escape(phrase if ' ' not in phrase else '') escaped_chinese_def = escape(spokenChineseDefinition) phraseMeaningSSML += f''' {escaped_phrase}

{escaped_spell_out}

{escaped_phrase}
{escaped_chinese_def} ''' return phraseMeaningSSML, dictResultList if __name__ == "__main__": # chineseDefinitionTTS(['colleague', 'viable', 'crack', 'dissolve', 'reinforce', 'plumber', 'tilt', 'exploration'],'outputAudioPath.mp3') # wordsPronunciation(['colleague', 'viable', 'crack', 'dissolve', 'reinforce', 'plumber', 'tilt', 'exploration'],'outputAudioPath.mp3') words='''ditch preparatory immense snare drum stall inhibit anchor priest affordance bump integrity eavesdrop monetary prerequisite aggregate benign keynote cognitive apathy nobility underperform insane liable conspiracy proprietary ditch nuance bicker addictive logarithm intimately peculiarly eligible apt attribute bizarre misfortune onlooker precursor curriculum phobia impedance hands-off introspection decay exploration deposit quarterly workbench phenomenon indefinitely bracket invariably clumsiness dutiful err entrench intrinsic pin calamity collaborate utterance rudimentary grievous rectify flexibility smoothly aerospace saturated realism bummer prominent impact spatial semantic appliance deficient insurance prohibitive anonymous tuition doctoral master bachelor fringe affiliate pointless emphasize contractor plumber contiguity curriculum vitae dialect domestic nerd faithfully quarter ''' wordsPronunciation(words.split())