forked from EdgeTX/edgetx-sdcard-sounds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvconvert.py
329 lines (316 loc) · 10.7 KB
/
csvconvert.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# This is a Python script to convert EdgeTX CSV files from the old to the new format.
# Author Michael Ansorge - [email protected]
# Version 0.1
import csv
import json
import glob
languages_names = [
('aa', 'Afar'),
('ab', 'Abkhazian'),
('af', 'Afrikaans'),
('ak', 'Akan'),
('sq', 'Albanian'),
('am', 'Amharic'),
('ar', 'Arabic'),
('an', 'Aragonese'),
('hy', 'Armenian'),
('as', 'Assamese'),
('av', 'Avaric'),
('ae', 'Avestan'),
('ay', 'Aymara'),
('az', 'Azerbaijani'),
('ba', 'Bashkir'),
('bm', 'Bambara'),
('eu', 'Basque'),
('be', 'Belarusian'),
('bn', 'Bengali'),
('bh', 'Bihari languages'),
('bi', 'Bislama'),
('bo', 'Tibetan'),
('bs', 'Bosnian'),
('br', 'Breton'),
('bg', 'Bulgarian'),
('my', 'Burmese'),
('ca', 'Catalan; Valencian'),
('cs', 'Czech'),
('ch', 'Chamorro'),
('ce', 'Chechen'),
('zh', 'Chinese'),
('cu', 'Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic'),
('cv', 'Chuvash'),
('kw', 'Cornish'),
('co', 'Corsican'),
('cr', 'Cree'),
('cy', 'Welsh'),
('cs', 'Czech'),
('da', 'Danish'),
('de', 'German'),
('dv', 'Divehi; Dhivehi; Maldivian'),
('nl', 'Dutch; Flemish'),
('dz', 'Dzongkha'),
('el', 'Greek, Modern (1453-)'),
('en', 'English'),
('eo', 'Esperanto'),
('et', 'Estonian'),
('eu', 'Basque'),
('ee', 'Ewe'),
('fo', 'Faroese'),
('fa', 'Persian'),
('fj', 'Fijian'),
('fi', 'Finnish'),
('fr', 'French'),
('fy', 'Western Frisian'),
('ff', 'Fulah'),
('Ga', 'Georgian'),
('de', 'German'),
('gd', 'Gaelic; Scottish Gaelic'),
('ga', 'Irish'),
('gl', 'Galician'),
('gv', 'Manx'),
('el', 'Greek, Modern (1453-)'),
('gn', 'Guarani'),
('gu', 'Gujarati'),
('ht', 'Haitian; Haitian Creole'),
('ha', 'Hausa'),
('he', 'Hebrew'),
('hz', 'Herero'),
('hi', 'Hindi'),
('ho', 'Hiri Motu'),
('hr', 'Croatian'),
('hu', 'Hungarian'),
('hy', 'Armenian'),
('ig', 'Igbo'),
('is', 'Icelandic'),
('io', 'Ido'),
('ii', 'Sichuan Yi; Nuosu'),
('iu', 'Inuktitut'),
('ie', 'Interlingue; Occidental'),
('ia', 'Interlingua (International Auxiliary Language Association)'),
('id', 'Indonesian'),
('ik', 'Inupiaq'),
('is', 'Icelandic'),
('it', 'Italian'),
('jv', 'Javanese'),
('ja', 'Japanese'),
('kl', 'Kalaallisut; Greenlandic'),
('kn', 'Kannada'),
('ks', 'Kashmiri'),
('ka', 'Georgian'),
('kr', 'Kanuri'),
('kk', 'Kazakh'),
('km', 'Central Khmer'),
('ki', 'Kikuyu; Gikuyu'),
('rw', 'Kinyarwanda'),
('ky', 'Kirghiz; Kyrgyz'),
('kv', 'Komi'),
('kg', 'Kongo'),
('ko', 'Korean'),
('kj', 'Kuanyama; Kwanyama'),
('ku', 'Kurdish'),
('lo', 'Lao'),
('la', 'Latin'),
('lv', 'Latvian'),
('li', 'Limburgan; Limburger; Limburgish'),
('ln', 'Lingala'),
('lt', 'Lithuanian'),
('lb', 'Luxembourgish; Letzeburgesch'),
('lu', 'Luba-Katanga'),
('lg', 'Ganda'),
('mk', 'Macedonian'),
('mh', 'Marshallese'),
('ml', 'Malayalam'),
('mi', 'Maori'),
('mr', 'Marathi'),
('ms', 'Malay'),
('Mi', 'Micmac'),
('mk', 'Macedonian'),
('mg', 'Malagasy'),
('mt', 'Maltese'),
('mn', 'Mongolian'),
('mi', 'Maori'),
('ms', 'Malay'),
('my', 'Burmese'),
('na', 'Nauru'),
('nv', 'Navajo; Navaho'),
('nr', 'Ndebele, South; South Ndebele'),
('nd', 'Ndebele, North; North Ndebele'),
('ng', 'Ndonga'),
('ne', 'Nepali'),
('nl', 'Dutch; Flemish'),
('nn', 'Norwegian Nynorsk; Nynorsk, Norwegian'),
('nb', 'Bokmål, Norwegian; Norwegian Bokmål'),
('no', 'Norwegian'),
('oc', 'Occitan (post 1500)'),
('oj', 'Ojibwa'),
('or', 'Oriya'),
('om', 'Oromo'),
('os', 'Ossetian; Ossetic'),
('pa', 'Panjabi; Punjabi'),
('fa', 'Persian'),
('pi', 'Pali'),
('pl', 'Polish'),
('pt', 'Portuguese'),
('ps', 'Pushto; Pashto'),
('qu', 'Quechua'),
('rm', 'Romansh'),
('ro', 'Romanian; Moldavian; Moldovan'),
('ro', 'Romanian; Moldavian; Moldovan'),
('rn', 'Rundi'),
('ru', 'Russian'),
('sg', 'Sango'),
('sa', 'Sanskrit'),
('si', 'Sinhala; Sinhalese'),
('sk', 'Slovak'),
('sk', 'Slovak'),
('sl', 'Slovenian'),
('se', 'Northern Sami'),
('sm', 'Samoan'),
('sn', 'Shona'),
('sd', 'Sindhi'),
('so', 'Somali'),
('st', 'Sotho, Southern'),
('es', 'Spanish; Castilian'),
('sq', 'Albanian'),
('sc', 'Sardinian'),
('sr', 'Serbian'),
('ss', 'Swati'),
('su', 'Sundanese'),
('sw', 'Swahili'),
('sv', 'Swedish'),
('ty', 'Tahitian'),
('ta', 'Tamil'),
('tt', 'Tatar'),
('te', 'Telugu'),
('tg', 'Tajik'),
('tl', 'Tagalog'),
('th', 'Thai'),
('bo', 'Tibetan'),
('ti', 'Tigrinya'),
('to', 'Tonga (Tonga Islands)'),
('tn', 'Tswana'),
('ts', 'Tsonga'),
('tk', 'Turkmen'),
('tr', 'Turkish'),
('tw', 'Twi'),
('ug', 'Uighur; Uyghur'),
('uk', 'Ukrainian'),
('ur', 'Urdu'),
('uz', 'Uzbek'),
('ve', 'Venda'),
('vi', 'Vietnamese'),
('vo', 'Volapük'),
('cy', 'Welsh'),
('wa', 'Walloon'),
('wo', 'Wolof'),
('xh', 'Xhosa'),
('yi', 'Yiddish'),
('yo', 'Yoruba'),
('za', 'Zhuang; Chuang'),
('zh', 'Chinese'),
('zu', 'Zulu')
]
sounddata = {
"language": [],
"name": [],
"description": [],
"directory": []
}
# Read all csv files in currenty directory.
filenames_voices = []
for file in glob.glob("*.csv"):
filenames_voices.append(file)
filenames_voices.sort() # sort alphabetically
for x in range(len(filenames_voices)):
print("###################################################################")
print("Converting: " + filenames_voices[x])
language = filenames_voices[x][:2]
print("language = " + language)
country_code = filenames_voices[x][3:5]
print("country code = " + country_code)
gender = filenames_voices[x][6:7]
print("gender = " + gender)
voice_name = filenames_voices[x][8: filenames_voices[x].find(".csv")]
print("voice Name = " + voice_name)
# Create sounds.json data example:
# "language": "en-GB",
# "name": "British English Female",
# "description": "British English Female Voice (en-GB-Libby)",
# "directory": "en_gb-libby"
if gender == "f":
gender_long = "Female"
else:
gender_long = "Male"
for y in range(len(languages_names)):
if language == languages_names[y][0]: # Get language name from 2 letter code
language_noun = languages_names[y][1]
print("language: " + language + "-" + country_code)
print("name: " + " " + language_noun + " " + gender_long)
print("description: " + " " + language_noun + " " + gender_long + " Voice (" + language + "-" +
country_code + "-" + voice_name + ")")
if voice_name == "": # Check if voice name exists.
print("directory " + language + "-" + country_code)
else:
print("directory " + language + "-" + country_code + "-" + voice_name)
# add data to dictonary for export in json
sounddata["language"].append(language + "-" + country_code)
sounddata["name"].append(language_noun + " " + gender_long)
if voice_name == "": # Check if voice name exists.
sounddata["description"].append(language_noun + " " + gender_long + " Voice (" + language + "-" +
country_code + ")")
sounddata["directory"].append(language + "-" + country_code)
else:
sounddata["description"].append(language_noun + " " + gender_long + " Voice (" + language + "-" +
country_code + "-" + voice_name + ")")
sounddata["directory"].append(language + "-" + country_code + "-" + voice_name)
json_object = json.dumps(sounddata, indent=4)
# Converting csv file.
with open(filenames_voices[x]) as csv_input: # open original csv file
with open(filenames_voices[x][:-4] + "-new.csv", 'w') as csv_output: # create new csv file
# CSV converting
csv_read = list(csv.reader(csv_input, delimiter=';')) # import csv file and create list of csv lines
"""
Field 0: File location on SD Card. Subfolder SOUNDS + langauge + if applicable: „-“ followed by
gender followed by „-“ followed by country code in Alpha 2 letter Capital followed „-“
followed by Name of voice with starting with capital Letter + subfolder SYSTEMS
Field 1: Filename
Field 2: Spoken Text
Field 3: Language
Field 4: country code / Accent in Alpha 2 letter Capital
Field 5: Gender of voice either m or f
Field 6: Name of voice with starting with Capital letter.
Field 7: Written description.
"""
for row in range(len(csv_read)): # Loop through all lines of the csv file.
output = list() # Create an empty list.
# Filed 0 Assignment: Check if original location is a SYSTEM location
if voice_name == "":
if csv_read[row][0][-6:] == "SYSTEM":
output.append(str("SOUNDS/" + language + "-" + country_code + "-" + gender + "/SYSTEM"))
else:
output.append(str("SOUNDS/" + language + "-" + country_code + "-" + gender))
else:
if csv_read[row][0][-6:] == "SYSTEM":
output.append(str("SOUNDS/" + language + "-" + country_code + "-" + gender + "-" +
voice_name + "/SYSTEM"))
else:
output.append(str("SOUNDS/" + language + "-" + country_code + "-" + gender + "-" +
voice_name))
# Add Field 1 filename
output.append(str(csv_read[row][1]))
# Add Field 2 spoken text
output.append(str(csv_read[row][2]))
# Add Field 3 language
output.append(str(language))
# Add Field 4 country code
output.append(str(country_code))
# Add Field 5 gender
output.append(str(gender))
# Add Field 6 voice name
output.append(str(voice_name))
# Write into new .csv file
writer = csv.writer(csv_output, delimiter=';')
writer.writerow(output)
print("Saving: " + filenames_voices[x])
print("Write Sound.json file")
with open("sample.json", "a") as outfile:
outfile.write(json_object)