-
Notifications
You must be signed in to change notification settings - Fork 10
/
prep_lab_files.py
434 lines (322 loc) · 11 KB
/
prep_lab_files.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#make_lab_file.py
#takes in folder with textgrid files, outputs lab files with text
#encoded in utf-8
# TODO:
# - naming convention for lab files
import codecs
import re
from glob import glob
from os import makedirs
from os.path import exists
from subprocess import call
# extracts text from a textgrid file given the file name, file encoding, and the tier
# on which the text is found
def extract_textgrid(filename, utf8, tiernum):
if utf8 == False:
with codecs.open(filename, 'r', 'utf-16') as f:
myList = f.readlines()
elif utf8 == True:
with codecs.open(filename, 'r', 'utf-8') as f:
myList = f.readlines()
f.close()
size = len(myList)
tierstring = "item [" + tiernum + "]:"
for x in myList:
if tierstring in x:
tierindex = myList.index(x)
for y in range (tierindex, size):
if "text" in myList[y]:
textindex = y
break;
textline = myList[textindex]
tmp = re.search('\"(.+?)\"', textline)
if tmp:
text = tmp.group(1)
return text
# extract text lines from eaf file given the file name
def extract_eaf(filename, tierid):
textlist = []
with codecs.open(filename, 'r', 'utf-8') as f:
myList = f.readlines()
f.close()
tierstring = "TIER_ID=\"" + tierid + "\""
# start index
for x in myList:
if tierstring in x:
tierindex = myList.index(x)
size = len(myList)
endindex = size
# end index
for y in range (tierindex, size):
if "</TIER>" in myList[y]:
endindex = y
break;
tmplist = []
for z in range (tierindex, endindex):
if "<ANNOTATION_VALUE>" in myList[z]:
tmplist.append(myList[z])
for item in tmplist:
if "<ANNOTATION_VALUE></ANNOTATION_VALUE>" not in item:
tmp_tuple = item.partition('<ANNOTATION_VALUE>')
tmp_string = tmp_tuple[2]
another_tuple = tmp_string.partition('</ANNOTATION_VALUE>')
final_string = another_tuple[0]
textlist.append(final_string)
return textlist
# creates a .lab file with the text from the textgrid file given the original file
# and the line of text
def create_file(file, textline):
newfilename = file.replace(".TextGrid", ".lab")
newfile = codecs.open(newfilename, 'w', 'utf-8')
newfile.write(textline)
newfile.close()
return newfile
# extracts individual words from lab list and puts into list
def extract_word(file, list):
f = codecs.open(file, "r", "utf-8")
string = f.readline()
newlist = string.rsplit(" ")
for word in newlist:
list.append(word)
f.close()
return list
# eliminates duplicates, taken from get_german_dict.py code
def no_copies(data):
"""Eliminates identical entries in a list."""
data_new = []; prev = None
for line in data:
if prev is not None and not line == prev:
data_new.append(prev)
prev = line
return data_new
# on same line as each word, add its pronunciation
# change special chars to utf-8 encode
def add_pronunciation(words):
new_words = []
for item in words:
list_string = list(item)
for character in list_string:
new_char = character.encode('unicode_escape')
if '\\' in new_char:
new_char = new_char.replace('\\', "")
item = item + " " + new_char
new_words.append(item)
return new_words
# create list of words from old dictionary file
def list_from_old_dict(dictname):
tmpList = []
old_dict_list = []
# if it does, extract the first word on each line
if exists(dictname):
with codecs.open(dictname, 'r', 'utf-8') as f:
tmpList = f.readlines()
f.close()
for item in tmpList:
item = item.split()[0]
old_dict_list.append(item)
return old_dict_list
# create list of words from lab files in file directory
def list_of_words(filedir):
# updated list of files
lab_list = glob(filedir+"*")
dictionary_list = []
# for each file, get words from file and
for file in lab_list:
if ".lab" in file:
#extract each word in file, put into dictionary list
dictionary_list = extract_word(file, dictionary_list)
return dictionary_list
# create dictionary from list of words
def create_dict(dictionary_list):
# sort list
sorted_words = sorted(dictionary_list)
# remove duplicates
unique_words = no_copies(sorted_words)
# make pronunciations
words_pronounced = add_pronunciation(unique_words)
# put list into a dictionary text file
dictionary_file = codecs.open(filedir + "/dictionary.txt", 'w', 'utf-8')
for word in words_pronounced:
dictionary_file.write(word)
dictionary_file.write("\n")
dictionary_file.close()
return dictionary_file
#form
menu = True
while menu == True:
print"""
What kind of files would you like to convert to .lab files?
Please enter the number for your selection:
1. .TextGrid
2. .eaf
3. quit
"""
filetype = raw_input("> ")
if filetype == "1":
print"""
You have selected .TextGrid files to convert.
Please make sure all TextGrid files in this directory
have the same encoding, either UTF-8 or UTF-16.
If you have both types in your directory, split them into
two sub-directories and run this script for both of them.
Are the files in this directory UTF-8 or UTF-16?
Enter 8 for UTF-8 and 16 for UTF-16.
"""
type = raw_input("> ")
if type == '8':
utf8 = True
elif type == '16':
utf8 = False
else: print("incorrect input. run script again.")
print """
Enter the tier number for your line of text in your TextGrid files.
If you don't know what tier it is, open up your file in praat, and find the number to the left of your tier.
Please note: this script only works if all TextGrid files in the directory have the same tier number for the line of text you want to extract.
"""
tierid = raw_input("> ")
print"""
What is the file directory?
You can drag and drop the files into the Terminal window to fill out this space
WARNING: No individual directory should have a space character
If so, please go back and replace any spaces with underscores
"""
filedir = raw_input("> ")
if filedir[-1] == ' ':
filedir = filedir.replace(" ", '')
if filedir[-1] != '/':
filedir = filedir + '/'
print"""
What would you like to call the directory for old files?
Default is: 0_old_file_textgrid/
Press enter to use default"""
olddir = raw_input("> ")
if olddir == '':
olddir = "0_old_file_textgrid/"
if olddir[-1] != '/':
olddir = olddir + "/"
# check for directory & make a new one
goodname = False
while goodname == False:
if exists(filedir + olddir):
print "Directory already exists!\nPlease pick a new directory name for old labfiles:"
olddir = raw_input("> ")
if olddir[-1] != '/':
olddir = olddir + '/'
else:
goodname = True
makedirs(filedir + olddir)
# make a list of the files
file_list = glob(filedir+"*")
# for each textgrid file, make a .lab file and save the textgrid to a separate folder
for file in file_list:
# textgrid files
if ".TextGrid" in file:
# extract line of text we need
textline = extract_textgrid(file, utf8, tierid)
# move the TextGrid file to a directory for the old files
call(["mv", file, filedir + olddir])
#create a new file with line of text
newfile = create_file(file, textline)
print"""
If there is a dictionary in another directory you would like to merge with the dictionary you are creating, input the file name (must include the directory)
If not, just hit enter.
You can drag and drop the files into the Terminal window to fill out this space
WARNING: No individual directory should have a space character
If so, please go back and replace any spaces with underscores
"""
dictdir = raw_input("> ")
other_dict_list = []
if dictdir == '':
print(" ")
else:
if dictdir[-1] == ' ':
dictdir = dictdir.replace(" ", '')
other_dict_list = list_from_old_dict(dictdir)
# check if dictionary already exists
dictname = filedir + "/dictionary.txt"
old_dict_list = list_from_old_dict(dictname)
# make a dictionary using the lab files just created and any old dictionary available
dictionary_list = list_of_words(filedir)
dictionary_list = dictionary_list + old_dict_list + other_dict_list
dictionary_file = create_dict(dictionary_list)
elif filetype == "2":
print"""
You have selected .eaf files to convert.
Enter the TIER_ID for your list of phrases.
If you don't know what it is, open your .eaf file in a text editing program and search for TIER_ID.
There will be several TIERS with different IDs, make sure you select the tier that lists your entire phrase in the notation you want."""
tierid = raw_input("> ")
print"""
What is the file directory?
You can drag and drop the files into the Terminal window to fill out this space
WARNING: No individual directory should have a space character
If so, please go back and replace any spaces with underscores
"""
filedir = raw_input("> ")
if filedir[-1] == ' ':
filedir = filedir.replace(" ", '')
if filedir[-1] != '/':
filedir = filedir + '/'
print"""
What would you like to call the directory for old files?
Default is: 0_old_file_eaf/
Press enter to use default"""
olddir = raw_input("> ")
if olddir == '':
olddir = "0_old_file_eaf/"
if olddir[-1] != '/':
olddir = olddir + "/"
# check for directory & make a new one
goodname = False
while goodname == False:
if exists(filedir + olddir):
print "Directory already exists!\nPlease pick a new directory name for old labfiles:"
olddir = raw_input("> ")
if olddir[-1] != '/':
olddir = olddir + '/'
else:
goodname = True
makedirs(filedir + olddir)
# make a list of the files
file_list = glob(filedir+"*")
# for each eaf file, make all possible .lab files and save eaf to a separate folder
for file in file_list:
# textgrid files
if ".eaf" in file:
# extract all the lines of text needed
textlist = extract_eaf(file, tierid)
# move eaf file to a directory for the old files
call(["mv", file, filedir + olddir])
#create all files necessary with lines of text from list
for text in textlist:
filename = filedir + text.replace(" ", "_") + ".lab"
newfile = codecs.open(filename, 'w', 'utf-8')
newfile.write(text)
newfile.close()
print"""
If there is a dictionary in another directory you would like to merge with the dictionary you are creating, input the file name (must include the directory)
If not, just hit enter.
You can drag and drop the files into the Terminal window to fill out this space
WARNING: No individual directory should have a space character
If so, please go back and replace any spaces with underscores
"""
dictdir = raw_input("> ")
other_dict_list = []
if dictdir == '':
print(" ")
else:
if dictdir[-1] == ' ':
dictdir = dictdir.replace(" ", '')
other_dict_list = list_from_old_dict(dictdir)
# check if dictionary already exists
dictname = filedir + "/dictionary.txt"
old_dict_list = list_from_old_dict(dictname)
# make a dictionary using the lab files just created and any old dictionary available
dictionary_list = list_of_words(filedir)
dictionary_list = dictionary_list + old_dict_list + other_dict_list
dictionary_file = create_dict(dictionary_list)
elif filetype == "3":
print("quit.")
menu = False
else:
print("Incorrect input. Try again.")