Added performance evaluation table
[sgc2.git] / InitialiseSGC2.praat
blob34657400bb6ece0692ebdc6677b5583c8e8f06fb
2 # SpeakGoodChinese 2.0
3
4 # Intializing Praat script
6 #     SpeakGoodChinese: InitializeSGC2.praat defines and sets the global variables
7 #     and loads general SGC2 code 
8 #     
9 #     Copyright (C) 2007-2010  R.J.J.H. van Son and 2010 the Netherlands Cancer Institute
10 #     The SpeakGoodChinese team are:
11 #     Guangqin Chen, Zhonyan Chen, Stefan de Koning, Eveline van Hagen, 
12 #     Rob van Son, Dennis Vierkant, David Weenink
13
14 #     This program is free software; you can redistribute it and/or modify
15 #     it under the terms of the GNU General Public License as published by
16 #     the Free Software Foundation; either version 2 of the License, or
17 #     (at your option) any later version.
18
19 #     This program is distributed in the hope that it will be useful,
20 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
21 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 #     GNU General Public License for more details.
23
24 #     You should have received a copy of the GNU General Public License
25 #     along with this program; if not, write to the Free Software
26 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
27
29 # Information for logging
30 currentDate$ = date$()
31 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
32 currentLogDirectory$ = ""
34 # Define canvas
35 viewportMargin = 5
36 yWordlist = 11
37 yFeedback = yWordlist + 5
38 yPinyin = yFeedback + 10
39 yContour = yPinyin + 15
40 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
41 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
42 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
43 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
45 # Initialize parameters
46 alertText$ = ""
47 currentWord = 1
48 pinyin$ = ""
49 character$ = ""
50 buttons$ = ""
51 config$ = ""
52 wordlistNum = 1
53 wordlistName$ = ""
54 wordlist$ = ""
55 config.deleteWordlist = -1
56 localWordlistDir$ = "shellDirectory$/wordlists"
57 config.displayNumbers = 1
58 config.displayPinyin = 1
59 config.displayChar = 1
60 config.displayTrans = 1
61 config.useSoundExample = 1
62 config.synthesis$ = "_DISABLED_"
63 config.input$ = "Microphone"
64 config.showBackground = 1
65 sgc2.noiseThresshold = -30
66 config.strict = 0
68 if unix
69         samplingFrequency = 44100
70 elsif macintosh
71         samplingFrequency = 44100
72 elsif windows
73         samplingFrequency = 44100
74 endif
75 recordingTime = 2
77 config.language$ = "EN"
78 config.register = 249
79 config.logPerformance = 0
80 preferencesLogDir$ = "'preferencesDirectory$'/sgc2/log"
81 preferencesTableDir$ = "'preferencesDirectory$'/sgc2/Data"
82 sgc2wordlists$ = "'preferencesDirectory$'/sgc2/wordlists"
83 # Global word lists must be installed BY THE ADMINISTRATOR
84 # This means, create the directory 'globalwordlists$' and
85 # fill it with wordlist directories containing wordlist.Table
86 # and audio files. They can be copied from a local directory.
87 globalwordlists$ = ""
88 globaltablelists$ = ""
89 globalTTSdir$ = ""
90 if unix
91         globalwordlists$ = "/etc/praat/sgc2/wordlists"
92         globaltablelists$ = "/etc/praat/sgc2/Data"      
93         globalTTSdir$ = "/etc/praat/sgc2/TTS"   
94 elsif macintosh
95         globalwordlists$ = "/Library/Praat/sgc2/wordlists"
96         globaltablelists$ = "/Library/Praat/sgc2/Data"
97         globalTTSdir$ = "/Library/Praat/sgc2/TTS"       
98 elsif windows
99         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/wordlists"
100         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/Data"
101         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/TTS"
102 endif
104 ##############################################################
106 config.shuffleLists = 1
107 defaultFontSize = 12
108 defaultFont$ = "Helvetica"
109 defaultLineWidth = 1
111 # Set up directories if they do not exist already
112 call set_up_directories
114 # Get saved preferences
115 call read_preferences ""
117 # Set up TTS system
118 speakCommandDir$ = "'preferencesDirectory$'/sgc2/TTS"
119 speakCommandFile$ = ""
120 call set_up_TTS
122 # Set inital language
123 call set_language 'config.language$'
125 # Get the word-list
126 call load_word_list "'localWordlistDir$'" 0
128 # Set up evaluation table
129 evaluation_table$ = ""
130 call initialize_evaluation_tables
132 # Draw inital window
133 call init_window
135 ###############################################################
137 # Obligatory procedures
139 ###############################################################
141 # Initialize Demo Window
142 procedure init_window
143     demo Erase all
144         if config.showBackground
145                 call draw_background Background
146         endif
147         demo Line width... 'defaultLineWidth'
148         demo 'defaultFont$'
149         call set_font_size 'defaultFontSize'
150         demo Black
151         call reset_viewport
152     # Define buttons in a table
153     call init_buttons
154         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
155         call set_window_title 'buttons$' '.windowTitle$'
156         # Display the word-list
157         call write_word_list
158         # Logging light
159         call paint_logging_light
160 endproc
162 # Make sure all Preferences directories are available
163 procedure set_up_directories
164         .dirPath$ = "'preferencesDirectory$'/sgc2"
165         createDirectory(.dirPath$)
166         createDirectory("'.dirPath$'/wordlists")
167         createDirectory("'.dirPath$'/log")
168         createDirectory("'.dirPath$'/TTS")
169 endproc
172 # Retrieve and store setting between sessions
174 procedure read_preferences .preferencesFile$
175         if not fileReadable(.preferencesFile$)
176                 .preferencesFile$ = preferencesDirectory$+"/sgc2/sgc2rc.txt"
177         endif
178         if fileReadable(.preferencesFile$)
179                 Read from file... 'preferencesDirectory$'/sgc2/sgc2rc.txt
180                 .preferenceTable$ = selected$("Table")
181                 .numPrefKeys = Get number of rows
182                 for .row to .numPrefKeys
183                         .variableName$ = Get value... '.row' Key
184                         if variableExists(.variableName$)
185                                 .variableValue = Get value... '.row' Value
186                                 if .variableValue <> undefined
187                                         '.variableName$' = '.variableValue'
188                                 endif
189                         elsif variableExists(.variableName$+"$")
190                                 .variableValue$ = Get value... '.row' Value
191                                 .variableName$ = .variableName$+"$"
192                                 '.variableName$' = "'.variableValue$'"
193                         endif
194                 endfor
195                 
196                 select Table '.preferenceTable$'
197                 Remove
198         endif
199 endproc
201 procedure write_preferences .preferencesFile$
202         Create Table with column names... Preferences 0 Key Value
203         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
204                 .preferencesFile$ = preferencesDirectory$ + "/sgc2/sgc2rc.txt"
205         endif
206         .row = 0
207         
208         for .tableNum from 1 to 2
209                 if .tableNum = 1
210                         .table$ = "Config"
211                 elsif .tableNum = 2
212                         .table$ = "MainPage"
213                 endif
214                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
216                 select Table '.table$'
217                 .numTableRows = Get number of rows
218                 for .tablerow to .numTableRows
219                         select Table '.table$'
220                         .label$ = Get value... '.tablerow' Label
221                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
222                         .keyName$ = .variableName$
223                         .value$ = ""
224                         if variableExists(.variableName$)
225                                 .value = '.variableName$'
226                                 .value$ = "'.value'"
227                         elsif variableExists(.variableName$ + "$")
228                                 .variableName$ = .variableName$ + "$"
229                                 .value$ = '.variableName$'
230                         elsif index(.variableName$, "_") > 0
231                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
232                                 .keyName$ = .variableName$
233                                 select Table Preferences
234                                 .prefRow = Search column... Key '.keyName$'
235                                 if .prefRow <= 0
236                                         if variableExists(.variableName$)
237                                            .value = '.variableName$'
238                                            .value$ = "'.value'"
239                                         elsif variableExists(.variableName$ + "$")
240                                            .variableName$ = .variableName$ + "$"
241                                            .value$ = '.variableName$'
242                                         endif
243                                 endif
244                         endif
246                         if .value$ <> ""
247                                 select Table Preferences
248                                 Append row
249                                 .row += 1
250                                 Set string value... '.row' Key '.keyName$'
251                                 Set string value... '.row' Value '.value$'
252                         endif
253                 endfor
254         endfor
255         
256         # Some extra settings
257         select Table Preferences
258         Append row
259         .row += 1
260         Set string value... '.row' Key wordlistDir
261         Set string value... '.row' Value 'localWordlistDir$'
263         select Table Preferences
264         Append row
265         .row += 1
266         Set string value... '.row' Key wordlistName
267         Set string value... '.row' Value 'wordlistName$'
269         select Table Preferences
270         Write to table file... '.preferencesFile$'
271         Remove
273 endproc
275 ###############################################################
277 # TTS (speech synthesis). Place commands in user info
279 # Prerequisites
280 # speakCommandDir$ = "'preferencesDirectory$'/sgc2/TTS"
281 # speakCommandFile$ = ""
282 procedure set_up_TTS
283         if macintosh
284                 .ttscommand$ = "speak"
285                 .osName$ = "OSX"
286                 .quote$ = ""
287         elsif unix
288                 .ttscommand$ = "espeak"
289                 .osName$ = "UNIX"
290                 .quote$ = ""
291         elsif windows
292                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
293                 .osName$ = "WIN"
294                 .quote$ = """"
295         endif
296         # Global TTS command has precedence
297         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
298                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
299                 if config.synthesis$ = "_DISABLED_"
300                         # Default is ON
301                         config.synthesis$ = "eSpeak"
302                 endif
303         endif
304         
305         # Local TTS command, if there is no global one
306         # Command does not exist yet, create it if eSpeak is installed
307         # Make sure to quote the path!
308         if speakCommandFile$ = ""
309                 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
310                         if macintosh
311                                 system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
312                                 .command_path$ < 'speakCommandDir$'/command_path.txt
313                         elsif unix
314                                 system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
315                                 .command_path$ < 'speakCommandDir$'/command_path.txt
316                         elsif windows
317                                 if fileReadable(.ttscommand$)
318                                         .command_path$ = .ttscommand$
319                                 endif
320                         endif
321                         # Remove any newlines
322                         .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
324                         # Command path found
325                         if .command_path$ <> "" and fileReadable(.command_path$)
326                                 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
327                                 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
328                                 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
329                                 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
330                         endif
331                 endif
332                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
333                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
334                 endif
335                 deleteFile("'speakCommandDir$'/command_path.txt")
336         endif
337         
338         # Close off (other TTS maybe a later option)
339         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
340                 config.synthesis$ = "eSpeak"
341         endif
342 endproc
344 ###############################################################
346 # Evaluation tables
348 # Prerequisites
349 # evaluation_table$ = ""
350 procedure initialize_evaluation_tables
351         # Remove old table
352         if evaluation_table$ != ""
353                 select Table 'evaluation_table$'
354                 Remove
355         endif
356         evaluation_table$ = ""
357         
358         # Create new table
359         Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
360         evaluation_table$ = "Performance"
361         call get_evaluation_row Total
362 endproc
364 procedure get_evaluation_row .tones$
365         select Table 'evaluation_table$'
366     .row = Search column... Tones '.tones$'
367         if .row < 1
368                 Append row
369                 .row = Get number of rows
370                 Set string value... '.row' Tones '.tones$'
371                 Set numeric value... '.row' Correct 0
372                 Set numeric value... '.row' Wrong 0
373                 Set numeric value... '.row' Total 0
374                 Set numeric value... '.row' High 0
375                 Set numeric value... '.row' Low 0
376                 Set numeric value... '.row' Wide 0
377                 Set numeric value... '.row' Narrow 0
378                 Set numeric value... '.row' Unknown 0
379                 Set numeric value... '.row' Commented 0
380                 Sort rows... Tones
381         .row = Search column... Tones '.tones$'
382         endif
383 endproc
385 procedure increment_evaluation_in_row .row .column$
386         select Table 'evaluation_table$'
387         .tmp = Get value... '.row' '.column$'
388         .tmp += 1
389         Set numeric value... '.row' '.column$' '.tmp'
390 endproc
392 procedure increment_evaluation_value .tones$ .column$
393         call get_evaluation_row '.tones$'
394         .row = get_evaluation_row.row
395         call increment_evaluation_in_row '.row' '.column$'
396         
397         call get_evaluation_row Total
398         .row = get_evaluation_row.row
399         call increment_evaluation_in_row '.row' '.column$'
400 endproc
402 procedure add_feedback_to_evaluation .table$
403     select Table '.table$'
404     .line1$ = Get value... 1 Text
405     .line2$ = Get value... 2 Text
406     .label$ = Get value... 3 Text
407         if .label$ = "6"
408                 .label$ = "Unknown"
409         endif
410         
411         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
412         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
413         
414         # Set evaluation
415         call increment_evaluation_value '.tones$' Total
416         if .tones$ = .recognized$
417                 call increment_evaluation_value '.tones$' Correct
418         else
419                 call increment_evaluation_value '.tones$' Wrong
420         endif
421         if index_regex(.label$, "(Correct|Wrong)") <= 0
422                 call increment_evaluation_value '.tones$' '.label$'
423                 call increment_evaluation_value '.tones$' Commented
424         endif
425 endproc
427 ###############################################################
429 # Miscelaneous procedures
431 ###############################################################
433 # Create a file 'preferencesDirectory$'/sgc2/log/logPerformance
434 # This file contains the path to the target directory where logging
435 # is to be stored.
436 # Note that this procedure is nowhere used in SGC2. It has to
437 # be explicitely called in a separate script.
439 procedure activate_sgc2_logging .targetDirectory$
440         createDirectory("'preferencesDirectory$'/sgc2")
441         createDirectory("'preferencesLogDir$'")
442         
443         if startsWith(.targetDirectory$, "~")
444                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
445         endif
446         
447         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
448         newline$ >> 'preferencesLogDir$'/logPerformance.txt
449 endproc
451 # TTS synthesis
452 procedure synthesize_sound .pinyin$
453         .command$ = ""
454         if fileReadable(speakCommandFile$)
455                 .command$ < 'speakCommandFile$'
456                 .command$ = replace$(.command$, "'newline$'", " ", 0)
457                 if macintosh
458                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
459                 elsif unix
460                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
461                 elsif windows
462                         system_nocheck call '.command$' "'.pinyin$'"'
463                 endif
464         endif
465 endproc