File size: 3,692 Bytes
7c5af53
 
54bc341
 
 
 
 
 
7c5af53
54bc341
7c5af53
 
 
 
 
54bc341
 
 
 
 
7c5af53
 
 
54bc341
7c5af53
 
 
54bc341
 
 
 
 
7c5af53
 
54bc341
 
 
 
 
 
 
7c5af53
54bc341
7c5af53
54bc341
7c5af53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54bc341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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'''

    <voice name="{escape(voice)}">

        {escape(text)}

    </voice>

    '''
    return wordsSpeech

def wordsPronunciation(phraseList):

    # Escape each phrase in the list *before* joining them
    escaped_phrases = (escape(phrase) for phrase in phraseList)
    joined_phrases = '<break time="1s"/>'.join(escaped_phrases)

    wordsSpeech = f'''

    <voice name="{escape(EN_VOICE)}">

        {joined_phrases}

    </voice>

    '''
    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'''

            <voice name="{escape(EN_VOICE)}">

                {escaped_phrase}

                <p><say-as interpret-as="characters">{escaped_spell_out}</say-as></p>

                {escaped_phrase}

            </voice>



            <voice name="{escape(ZH_VOICE)}">

                <lang xml:lang="zh-CN">

                    <s>{escaped_chinese_def}</s>

                </lang>

            </voice>

        '''

    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())