-
Notifications
You must be signed in to change notification settings - Fork 0
/
hed_write.py
268 lines (229 loc) · 15.8 KB
/
hed_write.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
import sys
import os
import glob
import traceback
import fnmatch
import re
import tkinter as tk
from tkinter import *
from tkinter import filedialog
#window screen overall
window = Tk()
window.title("HED writer version 0.1")
photo = PhotoImage(file = "hedwriteicon.png")
window.iconphoto(False, photo)
#Labels and some messy code start here
Label (window, text="Name your HED (there should be no space in the name, use underscore instead)", font="none 12 bold") .grid(row=1, column=0, sticky=W)
hedname = Entry(window, width=40, bg="green", font="none 12")
hedname.grid(row=2, column=0, sticky=W)
Label (window, text="Example: your_mom", font="none 12", fg="gray") .grid(row=1, column=1, sticky=W)
#for edit hed button
def importHED():
entered_name = filedialog.askopenfilename() #yea, open that file like that badussy
file_name_base = os.path.basename(entered_name)
file_name = os.path.splitext(file_name_base)[0]
for file in glob.glob(f"{file_name}" + ".hed"):
base = os.path.splitext(file)[0]
os.rename(file, base + ".txt")
Label (window, text=f"HED name: {file_name}", font="none 12 bold") .grid(row=3, column=1, sticky=W)
Label (window, text="Enter your flag name/category", font="none 12 bold") .grid(row=4, column=0, sticky=W)
flagname = Entry(window, width=40, bg="green", font="none 12")
flagname.grid(row=5, column=0, sticky=W)
def flag_name():
flag_name=flagname.get()
Label (window, text=f"Flag name/category: {flag_name}", font="none 12 bold") .grid(row=6, column=1, sticky=W)
Button(window, text="CONFIRM", width=8, command=flag_name) .grid(row=6, column=0, sticky=W)
Label (window, text="Enter your phoneme name/category", font="none 12 bold") .grid(row=7, column=0, sticky=W)
phonename = Entry(window, width=40, bg="green", font="none 12")
phonename.grid(row=8, column=0, sticky=W)
def phone_name():
phone_name=phonename.get()
Label (window, text=f"Phoneme name/category: {phone_name}", font="none 12 bold") .grid(row=9, column=1, sticky=W)
def click2():
entered_phoneme=phoneme.get() #collect the data from your phoneme section
phone_name1=phonename.get()
Label (window, text=f"Phone [ {entered_phoneme} ] added to the list", font="none 12 bold") .grid(row=15, column=1, sticky=W)
with open(f"{file_name}.txt", "a+") as file:
file.write("\n")
file.write("QS " + f'"L-phoneme_' + f'{entered_phoneme}"' + " " + "{" + f"*^{entered_phoneme}-*" + "}\n")
file.write("QS " + f'"C-phoneme_' + f'{entered_phoneme}"' + " " + "{" + f"*-{entered_phoneme}+*" + "}\n")
file.write("QS " + f'"R-phoneme_' + f'{entered_phoneme}"' + " " + "{" + f"*+{entered_phoneme}=*" + "}\n")
def click4():
entered_phoneme=phoneme.get() #collect the data from your phoneme section
phone_name1=phonename.get()
Label (window, text=f"Phone [ {entered_phoneme} ] added to the list", font="none 12 bold") .grid(row=15, column=1, sticky=W)
with open(f"{file_name}.txt", "a+") as file:
file.write("\n")
file.write("QS " + f'"L-phoneme_' + f'{phone_name1}"' + " " + "{" + f"*^{entered_phoneme}-*" + "tobereplace}\n")
file.write("QS " + f'"C-phoneme_' + f'{phone_name1}"' + " " + "{" + f"*-{entered_phoneme}+*" + "tobereplace}\n")
file.write("QS " + f'"R-phoneme_' + f'{phone_name1}"' + " " + "{" + f"*+{entered_phoneme}=*" + "tobereplace}\n")
def click5():
entered_phoneme=phoneme.get() #collect the data from your phoneme section
phone_name1=phonename.get()
replacerL = "-*," + f"*^{entered_phoneme}-*" + "tobereplace}"
replacerC = "+*," + f"*-{entered_phoneme}+*" + "tobereplace}"
replacerR = "=*," + f"*+{entered_phoneme}=*" + "tobereplace}"
with open(f"{file_name}.txt") as file:
s = file.read()
s = s.replace("-*tobereplace}", replacerL )
s = s.replace("+*tobereplace}", replacerC )
s = s.replace("=*tobereplace}", replacerR )
with open(f"{file_name}.txt", "w") as file:
file.write(s)
def click6():
with open(f"{file_name}.txt") as file:
s = file.read()
s = s.replace("tobereplace", "")
with open(f"{file_name}.txt", "w") as file:
file.write(s)
Label (window, text="Enter your phoneme syllable(s)", font="none 12 bold") .grid(row=13, column=0, sticky=W)
Button(window, text="ADD", width=8, command=click2) .grid(row=17, column=0, sticky=W)
Button(window, text="Create List", width=8, command=click4) .grid(row=15, column=0, sticky=W)
Button(window, text="Add to List", width=8, command=click5) .grid(row=16, column=0, sticky=W)
Button(window, text="New List", width=8, command=click6) .grid(row=18, column=0, sticky=W)
phoneme = Entry(window, width=40, bg="green", font="none 12")
phoneme.grid(row=14, column=0, sticky=W)
def click3():
entered_flag=flag.get() #collect the data from your flag section
flag_name1=flagname.get()
Label (window, text=f"Flag [ {entered_flag} ] added to the list", font="none 12 bold") .grid(row=12, column=1, sticky=W)
with open(f"{file_name}.txt", "a+") as file:
file.write("QS " + f'"flag_' + f'{flag_name1}"' + " " + "{" + f"*^{entered_flag}_*" + "}\n")
Label (window, text="Enter your flag syllable(s)", font="none 12 bold") .grid(row=10, column=0, sticky=W)
Button(window, text="ADD", width=8, command=click3) .grid(row=12, column=0, sticky=W)
flag = Entry(window, width=40, bg="green", font="none 12")
flag.grid(row=11, column=0, sticky=W)
Button(window, text="CONFIRM", width=8, command=phone_name) .grid(row=9, column=0, sticky=W)
Label (window, text=" ", font="none 12 bold") .grid(row=19, column=0, sticky=W)
Label (window, text=" ", font="none 12 bold") .grid(row=20, column=0, sticky=W)
Label (window, text=" ", font="none 12 bold") .grid(row=21, column=0, sticky=W)
Label (window, text="click this button AFTER you click [Make HED]", font="none 12 bold", fg="gray") .grid(row=22, column=0, sticky=W)
def get_val():
with open(f"{file_name}.hed", "r") as linecount:
indim = linecount.read()
indim_count = sum(1 for match in re.finditer("QS", indim))
indim_count4acoustic = indim_count + 4
in_ixf0_idx_count = sum(1 for match in re.finditer(r"\bQS\b", indim))
in_rest_idx_count = in_rest_idx_count = indim.count("_*")
in_ixf0 = in_ixf0_idx_count + 1
in_rest = in_rest_idx_count + 1
Label (window, text=f"values:", font="none 12 bold") .grid(row=23, column=0, sticky=W)
Label (window, text=f"acoustic [ {indim_count4acoustic} ]", font="none 12") .grid(row=24, column=0, sticky=W)
Label (window, text=f"timelag [ {indim_count} ]", font="none 12") .grid(row=25, column=0, sticky=W)
Label (window, text=f"duration [ {indim_count} ]", font="none 12") .grid(row=26, column=0, sticky=W)
Label (window, text=f"in_ixf0_idx [ {in_ixf0} ]", font="none 12") .grid(row=27, column=0, sticky=W)
Label (window, text=f"in_rest_idx [ {in_rest} ]", font="none 12") .grid(row=28, column=0, sticky=W)
Button(window, text="Get Values", width=8, command=get_val) .grid(row=23, column=0, sticky=W)
def touch_end():
for file in glob.glob(f"{file_name}" + ".txt"):
base = os.path.splitext(file)[0]
os.rename(file, base + ".hed")
Label (window, text=f"{file_name}.hed built successfully", font="none 12", fg="green") .grid(row=21, column=1, sticky=W)
Button(window, text="Make Hed", width=8, command=touch_end) .grid(row=23, column=1, sticky=W)
#for making hed from scratch
def click1():
entered_name=hedname.get() #collect the data from name your HED section
Label (window, text=f"HED name: {entered_name}", font="none 12 bold") .grid(row=3, column=1, sticky=W)
with open(f"{entered_name}" + ".txt", "w") as f:
f.write("")
Label (window, text="Enter your flag name/category", font="none 12 bold") .grid(row=4, column=0, sticky=W)
flagname = Entry(window, width=40, bg="green", font="none 12")
flagname.grid(row=5, column=0, sticky=W)
def flag_name():
flag_name=flagname.get()
Label (window, text=f"Flag name/category: {flag_name}", font="none 12 bold") .grid(row=6, column=1, sticky=W)
Button(window, text="CONFIRM", width=8, command=flag_name) .grid(row=6, column=0, sticky=W)
Label (window, text="Enter your phoneme name/category", font="none 12 bold") .grid(row=7, column=0, sticky=W)
phonename = Entry(window, width=40, bg="green", font="none 12")
phonename.grid(row=8, column=0, sticky=W)
def phone_name():
phone_name=phonename.get()
Label (window, text=f"Phoneme name/category: {phone_name}", font="none 12 bold") .grid(row=9, column=1, sticky=W)
def click2():
entered_phoneme=phoneme.get() #collect the data from your phoneme section
phone_name1=phonename.get()
Label (window, text=f"Phone [ {entered_phoneme} ] added to the list", font="none 12 bold") .grid(row=15, column=1, sticky=W)
with open(f"{entered_name}.txt", "a+") as file:
file.write("\n")
file.write("QS " + f'"L-phoneme_' + f'{entered_phoneme}"' + " " + "{" + f"*^{entered_phoneme}-*" + "}\n")
file.write("QS " + f'"C-phoneme_' + f'{entered_phoneme}"' + " " + "{" + f"*-{entered_phoneme}+*" + "}\n")
file.write("QS " + f'"R-phoneme_' + f'{entered_phoneme}"' + " " + "{" + f"*+{entered_phoneme}=*" + "}\n")
def click4():
entered_phoneme=phoneme.get() #collect the data from your phoneme section
phone_name1=phonename.get()
Label (window, text=f"Phone [ {entered_phoneme} ] added to the list", font="none 12 bold") .grid(row=15, column=1, sticky=W)
with open(f"{entered_name}.txt", "a+") as file:
file.write("\n")
file.write("QS " + f'"L-phoneme_' + f'{phone_name1}"' + " " + "{" + f"*^{entered_phoneme}-*" + "tobereplace}\n")
file.write("QS " + f'"C-phoneme_' + f'{phone_name1}"' + " " + "{" + f"*-{entered_phoneme}+*" + "tobereplace}\n")
file.write("QS " + f'"R-phoneme_' + f'{phone_name1}"' + " " + "{" + f"*+{entered_phoneme}=*" + "tobereplace}\n")
def click5():
entered_phoneme=phoneme.get() #collect the data from your phoneme section
phone_name1=phonename.get()
replacerL = "-*," + f"*^{entered_phoneme}-*" + "tobereplace}"
replacerC = "+*," + f"*-{entered_phoneme}+*" + "tobereplace}"
replacerR = "=*," + f"*+{entered_phoneme}=*" + "tobereplace}"
with open(f"{entered_name}.txt") as file:
s = file.read()
s = s.replace("-*tobereplace}", replacerL )
s = s.replace("+*tobereplace}", replacerC )
s = s.replace("=*tobereplace}", replacerR )
with open(f"{entered_name}.txt", "w") as file:
file.write(s)
def click6():
with open(f"{entered_name}.txt") as file:
s = file.read()
s = s.replace("tobereplace", "")
with open(f"{entered_name}.txt", "w") as file:
file.write(s)
Label (window, text="Enter your phoneme syllable(s)", font="none 12 bold") .grid(row=13, column=0, sticky=W)
Button(window, text="ADD", width=8, command=click2) .grid(row=17, column=0, sticky=W)
Button(window, text="Create List", width=8, command=click4) .grid(row=15, column=0, sticky=W)
Button(window, text="Add to List", width=8, command=click5) .grid(row=16, column=0, sticky=W)
Button(window, text="New List", width=8, command=click6) .grid(row=18, column=0, sticky=W)
phoneme = Entry(window, width=40, bg="green", font="none 12")
phoneme.grid(row=14, column=0, sticky=W)
def click3():
entered_flag=flag.get() #collect the data from your flag section
flag_name1=flagname.get()
Label (window, text=f"Flag [ {entered_flag} ] added to the list", font="none 12 bold") .grid(row=12, column=1, sticky=W)
with open(f"{entered_name}.txt", "a+") as file:
file.write("QS " + f'"flag_' + f'{flag_name1}"' + " " + "{" + f"*^{entered_flag}_*" + "}\n")
Label (window, text="Enter your flag syllable(s)", font="none 12 bold") .grid(row=10, column=0, sticky=W)
Button(window, text="ADD", width=8, command=click3) .grid(row=12, column=0, sticky=W)
flag = Entry(window, width=40, bg="green", font="none 12")
flag.grid(row=11, column=0, sticky=W)
Button(window, text="CONFIRM", width=8, command=phone_name) .grid(row=9, column=0, sticky=W)
Label (window, text=" ", font="none 12 bold") .grid(row=19, column=0, sticky=W)
Label (window, text=" ", font="none 12 bold") .grid(row=20, column=0, sticky=W)
Label (window, text=" ", font="none 12 bold") .grid(row=21, column=0, sticky=W)
Label (window, text="click this button AFTER you click [Make HED]", font="none 12 bold", fg="gray") .grid(row=22, column=0, sticky=W)
def get_val():
with open(f"{entered_name}.hed", "r") as linecount:
indim = linecount.read()
indim_count = sum(1 for match in re.finditer("QS", indim))
indim_count4acoustic = indim_count + 4
in_ixf0_idx_count = sum(1 for match in re.finditer(r"\bQS\b", indim))
in_rest_idx_count = indim.count("_*")
in_ixf0 = in_ixf0_idx_count + 1
in_rest = in_rest_idx_count + 1
Label (window, text=f"values:", font="none 12 bold") .grid(row=23, column=0, sticky=W)
Label (window, text=f"acoustic [ {indim_count4acoustic} ]", font="none 12") .grid(row=24, column=0, sticky=W)
Label (window, text=f"timelag [ {indim_count} ]", font="none 12") .grid(row=25, column=0, sticky=W)
Label (window, text=f"duration [ {indim_count} ]", font="none 12") .grid(row=26, column=0, sticky=W)
Label (window, text=f"in_ixf0_idx [ {in_ixf0} ]", font="none 12") .grid(row=27, column=0, sticky=W)
Label (window, text=f"in_rest_idx [ {in_rest} ]", font="none 12") .grid(row=28, column=0, sticky=W)
Button(window, text="Get Values", width=8, command=get_val) .grid(row=23, column=0, sticky=W)
def touch_cqs():
with open("cNp.txt", "r") as CQS_content, open(f"{entered_name}.txt", "a") as final_hed:
for line in CQS_content:
final_hed.write(line)
for file in glob.glob(f"{entered_name}" + ".txt"):
base = os.path.splitext(file)[0]
os.rename(file, base + ".hed")
Label (window, text=f"{entered_name}.hed built successfully", font="none 12", fg="green") .grid(row=21, column=1, sticky=W)
Button(window, text="Make Hed", width=8, command=touch_cqs) .grid(row=23, column=1, sticky=W)
Button(window, text="CONFIRM", width=8, command=click1) .grid(row=3, column=0, sticky=W)
Button(window, text="Edit HED", width=8, command=importHED) .grid(row=3, column=1, sticky=W)
#main loop
window.mainloop()