-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextToMySpeech.py
170 lines (141 loc) · 6.28 KB
/
textToMySpeech.py
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
# -*- coding: utf-8 -*-
#IMPORT all the packages
pip install pydub
from bs4 import BeautifulSoup
from IPython.display import Audio
from google.colab import drive
import re
import requests
import wave as wv
from pydub import AudioSegment
#Setup the file system with a connection to google drive. Here are your phonetic files located
drive.mount('/content/drive/', force_remount=True)
def play(file):
return Audio(file, autoplay=True)
#The phonetic functions
listWoordAf = []
def getFonetic(word):
toReturn = ''
with requests.Session() as s:
url = 'https://nl.wiktionary.org/wiki/{}'.format(word)
r = s.get(url)
if 'Deze pagina bevat geen tekst' in r.text:
word = word.capitalize()
url = 'https://nl.wiktionary.org/wiki/{}'.format(word)
r = s.get(url)
soup = BeautifulSoup(r.text, 'html')
if "Woorden in het Nederlands met IPA-weergave" not in soup.text:
ipa = False
else:
ipa = soup.find('a', text='IPA')
if ipa:
ipa = ipa.parent()[1].text.replace('/', '').replace('\u202f', '')
ipa = ipa.split('\n')[0].replace('(Noord-Nederland): ', '').split(',')[0]
toReturn = list(ipa)
woordAf = soup.find('span', {'class' : 'mw-headline', 'id' : 'Woordafbreking'}).parent.find_next_sibling()
woordAf = woordAf.text.split('·')
if woordAf:
listWoordAf.append(woordAf)
else:
listWoordAf.append(word)
else:
woordAf = soup.find('span', {'class' : 'mw-headline', 'id' : 'Woordafbreking'})
if woordAf:
woordAf = woordAf.parent.find_next_sibling()
woordAf = woordAf.text.split('·')
listWoordAf.append(woordAf)
else:
listWoordAf.append(word)
word = word.lower()
url = 'http://www.woorden.org/woord/{}'.format(word)
r = s.get(url)
soup = BeautifulSoup(r.text, 'html')
if len(soup.find_all('table')) != 0:
table = soup.find_all('table')[0]
if len(table.find_all('td')) > 0:
tData = table.find_all('td')[1].text
tData = tData.replace('[', '').replace(']', '')
toReturn = list(tData)
else:
print("Dit woord is onbekend:", word)
toReturn = list(word)
else:
print("Dit woord is onbekend:", word)
toReturn = list(word)
if 'ː' in toReturn:
index = toReturn.index('ː')
toReturn[(index - 1):(index+1)] = [''.join(toReturn[(index -1 ):(index+1)])]
if ':' in toReturn:
index = toReturn.index(':')
toReturn[(index - 1):(index+1)] = [''.join(toReturn[(index -1 ):(index+1)])]
if '(' in toReturn:
toReturn.remove('(')
toReturn.remove(')')
# index = toReturn.index('(')
# toReturn[(index):(index+3)] = [''.join(toReturn[(index):(index+3)])]
if 'ˈ' in toReturn:
toReturn.remove('ˈ')
#index = toReturn.index('ˈ')
#toReturn[(index):(index+2)] = [''.join(toReturn[(index):(index+2)])]
if "'" in toReturn:
toReturn.remove("'")
#index = toReturn.index("'")
#toReturn[(index):(index+2)] = [''.join(toReturn[(index):(index+2)])]
return toReturn
def toFonArray(string):
fonArray = []
string = string.replace(' ', ' ').replace(' .', '.').replace(' ,', ',').lower()
array = string.split(' ')
for word in array:
if word == ',' :
fonArray.append('comma')
elif word == '.' :
fonArray.append('do')
elif word == ' ' :
fonArray.append('space')
else:
word = word.lower()
fonArray.append(getFonetic(word))
return fonArray
ipaSampa = {"χ" : "x", "a":"adp","e":"edp","ɪː":"idp", "o": "odp","p":"p", "t":"t", "k":"k", "f":"f", "s":"s", "ʃ":"ss", "x":"x", "b":"b", "d":"d", "ɡ":"g", "v":"v", "z":"z", "ʒ":"zz", "ɣ":"gg", "ɦ":"h_", "l":"l", "r":"r", "m":"m", "n":"n", "ŋ":"nn", "j":"j", "w": "v_", "ʋ":"v_", "tʃ":"tS", "ts":"ts", "dʒ":"dZ", "ɑ":"aa", "ɛ":"eedp", "ɪ":"ii", "ɔ":"oo", "ʏ":"yy", "ə":"@", "aː":"adp","eː":"edp", "i":"i", "oː":"odp", "y":"y", "øː":"2dp", "u":"u", "ɛː":"eedp", "œː":"9dp","iː":"idp", "yː":"ydp", "ɔː":"oodp", "uː":"udp", "ɛi":"Ei", "œy":"9y", "ʌu":"Vu", "ɑi":"Ai", "ɔi":"i", "aːi":"adpi", "eːu":"edpu", "iu":"iu", "oːi":"odpi", "ui":"ui"}
def toAudio(lijst):
audioList = []
outFileTo = '/content/drive/My Drive/speech/results/{}.wav'.format(file_name)
outfilePlay = '/content/drive/My Drive/speech/results/{}.wav'.format(file_name)
for fonWord in lijst:
if type(fonWord) is str:
fonWord = '{}.wav'.format(fonWord)
audioList.append(fonWord)
elif type(fonWord) is list:
for ch in fonWord:
if ch == "ø":
ch = "øː"
if ch in ipaSampa:
ch = ipaSampa[ch]
print('character ', ch, 'in ipaSampa')
else:
ch = ch
print('character ', ch, 'not in ipaSampa')
ch = '{}.wav'.format(ch)
audioList.append(ch)
data= []
begEind = AudioSegment.from_wav("/content/drive/My Drive/speech/first.wav")
fileEx = begEind
for file in audioList:
file = '/content/drive/My Drive/speech/{}'.format(file)
song = AudioSegment.from_wav(file)
fileEx = fileEx.append(song, crossfade=10)
fileEx = fileEx.append(begEind, crossfade=100)
fileEx.export(outFileTo, format="wav")
print('DONE')
return True
ipaSampa = {"ɱ":"mm","ɲ":"ng", "a":"adp","e":"edp","ɪː":"idp", "o": "odp","p":"p", "t":"t", "k":"k", "f":"f", "s":"s", "ʃ":"ss", "x":"x", "b":"b", "d":"d", "ɡ":"g", "v":"v", "z":"z", "ʒ":"zz", "ɣ":"gg", "ɦ":"h_", "l":"l", "r":"r", "m":"m", "n":"n", "ŋ":"nn", "j":"j", "ʋ":"v_", "tʃ":"tS", "ts":"ts", "dʒ":"dZ", "ɑ":"aa", "ɛ":"eedp", "ɪ":"ii", "ɔ":"oo", "ʏ":"yy", "ə":"@", "aː":"adp","eː":"edp", "i":"i", "oː":"odp", "y":"y", "øː":"2dp", "u":"u", "ɛː":"eedp", "œː":"9dp","iː":"idp", "yː":"ydp", "ɔː":"oodp", "uː":"udp", "ɛi":"Ei", "œy":"9y", "ʌu":"Vu", "ɑi":"Ai", "ɔi":"i", "aːi":"adpi", "eːu":"edpu", "iu":"iu", "oːi":"odpi", "ui":"ui"}
# Result
#@title Form {vertical-output: true }
#@markdown After pressing ctrl+enter there will be an audiofile created
listWoordAf = []
text = 'The text to my speech' #@param {type:"string"}
file_name = 'myFirstSpeech' #@param {type:"string"}
forAudio = toFonArray(text)
TF = toAudio(forAudio)
play('/content/drive/My Drive/spraak/results/{}.wav'.format(file_naam))