Rewriting performance evaluation
[sgc2.git] / InitialiseSGC2.praat
blob8ddc04e01acaa85d4704f9389d2112044fb48d77
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 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
32 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
33 currentLogDirectory$ = ""
34 feedbackTablePrefix$ = "Feedback"
35 feedbackTableName$ = ""
37 # Define canvas
38 viewportMargin = 5
39 yWordlist = 11
40 yFeedback = yWordlist + 5
41 yPinyin = yFeedback + 10
42 yContour = yPinyin + 15
43 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
44 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
45 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
46 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
48 # Pop-Up window colors
49 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
50 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
52 # Initialize parameters
53 alertText$ = ""
54 te.currentWord = 1
55 te.numberOfWords = 0
56 te.recordedSound = 0
57 pinyin$ = ""
58 character$ = ""
59 buttons$ = ""
60 config$ = ""
61 wordlistNum = 1
62 wordlistName$ = ""
63 wordlist$ = ""
64 config.deleteWordlist = -1
65 config.displayNumbers = 1
66 config.displayPinyin = 1
67 config.displayChar = 0
68 config.displayTrans = 1
69 config.useSoundExample = 1
70 config.synthesis$ = "_DISABLED_"
71 config.input$ = "Microphone"
72 config.showBackground = 1
73 sgc2.noiseThresshold = -30
74 config.strict = 0
76 sgc_ToneProt.minimumPitch = 40
77 sgc_ToneProt.maximumPitch = 600
79 # Platform dependent settings
80 if macintosh or windows
81         config.displayChar = 1
82         config.displayNumbers = 0
83 endif
84 if windows and endsWith(build_SHA$, " XP")
85         config.displayChar = 0
86         config.displayNumbers = 0
87 endif
88 if unix
89         config.displayChar = 0
90         config.displayNumbers = 1
91 endif
93 if unix
94         samplingFrequency = 44100
95 elsif macintosh
96         samplingFrequency = 44100
97 elsif windows
98         samplingFrequency = 44100
99 endif
100 recordingTime = 2
102 logging = 0
103 config.language$ = "EN"
104 config.register = 249
105 config.logPerformance = 0
106 preferencesLogDir$ = "'preferencesAppDir$'/log"
107 preferencesTableDir$ = "'preferencesAppDir$'/Data"
108 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
109 localWordlistDir$ = sgc2wordlists$
111 # Global word lists must be installed BY THE ADMINISTRATOR
112 # This means, create the directory 'globalwordlists$' and
113 # fill it with wordlist directories containing wordlist.Table
114 # and audio files. They can be copied from a local directory.
115 globalwordlists$ = ""
116 globaltablelists$ = ""
117 globalTTSdir$ = ""
118 if unix
119         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
120         globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"     
121         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
122 elsif macintosh
123         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
124         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
125         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
126 elsif windows
127         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
128         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
129         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
130 endif
131 sgc2.synthesizer = -1
132 # Define a Praat TTS command that will set sgc2.synthesizer
133 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
134 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
135 sgc2.alignedTextGrid = -1
137 ##############################################################
139 config.shuffleLists = 1
140 defaultFontSize = 12
141 defaultFont$ = "Helvetica"
142 defaultLineWidth = 1
144 # Set up directories if they do not exist already
145 call set_up_directories
147 # Get saved preferences
148 call read_preferences ""
150 # Set up TTS system
151 speakCommandDir$ = "'preferencesAppDir$'/TTS"
152 speakCommandFile$ = ""
153 call set_up_TTS
155 # Set inital language
156 call set_language 'config.language$'
158 # Get the word-list
159 call load_word_list "'localWordlistDir$'" 0
161 # Set up evaluation table
162 initialiseSGC2.toneevaluation_table$ = ""
163 call initialize_toneevaluation_tables
165 # Draw inital window
166 call init_window
168 ###############################################################
170 # Obligatory procedures
172 ###############################################################
173 # Initialize Demo Window
174 procedure init_window
175     demo Erase all
176         demo Line width... 'defaultLineWidth'
177         demo 'defaultFont$'
178         call set_font_size 'defaultFontSize'
179         demo Black
180         call reset_viewport
181         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
182         call set_window_title 'buttons$' '.windowTitle$'
183         # Display the word-list
184         call write_word_list
185     # Wipe screen and draw background
186     call wipeArea 'wipeContourArea$'
187         if config.showBackground
188                 call draw_background Background
189         endif
190     # Define buttons in a table
191     call init_buttons
192         # Draw the contour
193         call draw_tone_contour
194         # Logging light
195         call paint_logging_light
196 endproc
198 # Make sure all Preferences directories are available
199 procedure set_up_directories
200         .dirPath$ = "'preferencesAppDir$'"
201         createDirectory(.dirPath$)
202         .dirPath$ > '.dirPath$'/directory.txt
203         createDirectory("'.dirPath$'/wordlists")
204         .dirPath$ > '.dirPath$'/wordlists/directory.txt
205         createDirectory("'.dirPath$'/pitchmodels")
206         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
207         createDirectory("'.dirPath$'/log")
208         .dirPath$ > '.dirPath$'/log/directory.txt
209         createDirectory("'.dirPath$'/TTS")
210         .dirPath$ > '.dirPath$'/TTS/directory.txt
211 endproc
214 # Retrieve and store setting between sessions
216 procedure read_preferences .preferencesFile$
217         if not fileReadable(.preferencesFile$)
218                 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
219         endif
220         if fileReadable(.preferencesFile$)
221                 Read from file... 'preferencesAppDir$'/sgc2rc.txt
222                 .preferenceTable$ = selected$("Table")
223                 .numPrefKeys = Get number of rows
224                 for .row to .numPrefKeys
225                         .variableName$ = Get value... '.row' Key
226                         if variableExists(.variableName$)
227                                 .variableValue = Get value... '.row' Value
228                                 if .variableValue <> undefined
229                                         '.variableName$' = '.variableValue'
230                                 endif
231                         elsif variableExists(.variableName$+"$")
232                                 .variableValue$ = Get value... '.row' Value
233                                 .variableName$ = .variableName$+"$"
234                                 '.variableName$' = "'.variableValue$'"
235                         endif
236                 endfor
237                 
238                 select Table '.preferenceTable$'
239                 Remove
240         endif
241 endproc
243 procedure write_preferences .preferencesFile$
244         Create Table with column names... Preferences 0 Key Value
245         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
246                 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
247         endif
248         .row = 0
249         
250         for .tableNum from 1 to 2
251                 if .tableNum = 1
252                         .table$ = "Config"
253                 elsif .tableNum = 2
254                         .table$ = "MainPage"
255                 endif
256                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
258                 select Table '.table$'
259                 .numTableRows = Get number of rows
260                 for .tablerow to .numTableRows
261                         select Table '.table$'
262                         .label$ = Get value... '.tablerow' Label
263                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
264                         .keyName$ = .variableName$
265                         .value$ = ""
266                         if variableExists(.variableName$)
267                                 .value = '.variableName$'
268                                 .value$ = "'.value'"
269                         elsif variableExists(.variableName$ + "$")
270                                 .variableName$ = .variableName$ + "$"
271                                 .value$ = '.variableName$'
272                         elsif index(.variableName$, "_") > 0
273                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
274                                 .keyName$ = .variableName$
275                                 select Table Preferences
276                                 .prefRow = Search column... Key '.keyName$'
277                                 if .prefRow <= 0
278                                         if variableExists(.variableName$)
279                                            .value = '.variableName$'
280                                            .value$ = "'.value'"
281                                         elsif variableExists(.variableName$ + "$")
282                                            .variableName$ = .variableName$ + "$"
283                                            .value$ = '.variableName$'
284                                         endif
285                                 endif
286                         endif
288                         if .value$ <> ""
289                                 select Table Preferences
290                                 Append row
291                                 .row += 1
292                                 Set string value... '.row' Key '.keyName$'
293                                 Set string value... '.row' Value '.value$'
294                         endif
295                 endfor
296         endfor
297         
298         # Some extra settings
299         select Table Preferences
300         Append row
301         .row += 1
302         Set string value... '.row' Key wordlistDir
303         Set string value... '.row' Value 'localWordlistDir$'
305         select Table Preferences
306         Append row
307         .row += 1
308         Set string value... '.row' Key wordlistName
309         Set string value... '.row' Value 'wordlistName$'
311         select Table Preferences
312         Write to table file... '.preferencesFile$'
313         Remove
315 endproc
317 ###############################################################
319 # TTS (speech synthesis). Place commands in user info
321 # Prerequisites
322 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
323 # speakCommandFile$ = ""
325 # Make sure sgc2.synthesizer is set!
326 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
327         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
328         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
329 endproc
331 procedure set_up_TTS
332         if sgc2.synthesizer > 0
333                 select sgc2.synthesizer
334                 Remove
335                 sgc2.synthesizer = -1
336         endif
337         # Note, the TTScommand must set sgc2.synthesizer!
338         if config.strict
339                 'sgc2.advancedTTScommand$'
340         else
341                 'sgc2.normalTTScommand$'
342         endif
344         if macintosh
345                 .ttscommand$ = "speak"
346                 .osName$ = "OSX"
347                 .quote$ = ""
348         elsif unix
349                 .ttscommand$ = "espeak"
350                 .osName$ = "UNIX"
351                 .quote$ = ""
352         elsif windows
353                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
354                 .osName$ = "WIN"
355                 .quote$ = """"
356         endif
357         # Global TTS command has precedence
358         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
359                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
360                 if config.synthesis$ = "_DISABLED_"
361                         # Default is ON
362                         config.synthesis$ = "eSpeak"
363                 endif
364         endif
366         # Local TTS command, if there is no global one
367         # Command does not exist yet, create it if eSpeak is installed
368         # Make sure to quote the path!
369         if speakCommandFile$ = ""
370                 # Autodetect synthesizer command
371 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
372 #                       .command_path$ = ""
373 #                       if macintosh
374 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
375 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
376 #                       elsif unix
377 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
378 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
379 #                       elsif windows
380 #                               if fileReadable(.ttscommand$)
381 #                                       .command_path$ = .ttscommand$
382 #                               endif
383 #                       endif
384 #                       # Remove any newlines
385 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
387 #                       # Command path found
388 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
389 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
390 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
391 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
392 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
393 #                       endif
394 #               endif
395 #               if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
396                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
397                 endif
398                 deleteFile("'speakCommandDir$'/command_path.txt")
399         endif
400         
401         # Close off (other TTS maybe a later option)
402         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
403                 config.synthesis$ = "eSpeak"
404         endif
405 endproc
407 ###############################################################
409 # Evaluation tables
411 # Prerequisites
412 # initialiseSGC2.toneevaluation_table$ = ""
413 procedure initialize_toneevaluation_tables
414         # Remove old table
415         if initialiseSGC2.toneevaluation_table$ != ""
416                 select Table 'initialiseSGC2.toneevaluation_table$'
417                 Remove
418         endif
419         initialiseSGC2.toneevaluation_table$ = ""
420         
421         # Create new table
422         Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
423         initialiseSGC2.toneevaluation_table$ = "Performance"
424         call get_toneevaluation_row Total
425 endproc
427 procedure get_toneevaluation_row .tones$
428         select Table 'initialiseSGC2.toneevaluation_table$'
429     .row = Search column... Tones '.tones$'
430         if .row < 1
431                 Append row
432                 .row = Get number of rows
433                 Set string value... '.row' Tones '.tones$'
434                 Set numeric value... '.row' Correct 0
435                 Set numeric value... '.row' Wrong 0
436                 Set numeric value... '.row' Total 0
437                 Set numeric value... '.row' High 0
438                 Set numeric value... '.row' Low 0
439                 Set numeric value... '.row' Wide 0
440                 Set numeric value... '.row' Narrow 0
441                 Set numeric value... '.row' Unknown 0
442                 Set numeric value... '.row' Commented 0
443                 Sort rows... Tones
444         .row = Search column... Tones '.tones$'
445         endif
446 endproc
448 procedure increment_toneevaluation_in_row .row .column$
449         select Table 'initialiseSGC2.toneevaluation_table$'
450         .tmp = Get value... '.row' '.column$'
451         .tmp += 1
452         Set numeric value... '.row' '.column$' '.tmp'
453 endproc
455 procedure increment_toneevaluation_value .tones$ .column$
456         call get_toneevaluation_row '.tones$'
457         .row = get_toneevaluation_row.row
458         call increment_toneevaluation_in_row '.row' '.column$'
459         
460         call get_toneevaluation_row Total
461         .row = get_toneevaluation_row.row
462         call increment_toneevaluation_in_row '.row' '.column$'
463 endproc
465 procedure add_feedback_to_toneevaluation .table$
466     select Table '.table$'
467     .line1$ = Get value... 1 Text
468     .line2$ = Get value... 2 Text
469     .label$ = Get value... 3 Text
470         if .label$ = "6"
471                 .label$ = "Unknown"
472         endif
473         
474         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^a-z0-9]", "", 0)
475         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
476         
477         # Set evaluation
478         call increment_toneevaluation_value '.tones$' Total
479         if .tones$ = .recognized$
480                 call increment_toneevaluation_value '.tones$' Correct
481         else
482                 call increment_toneevaluation_value '.tones$' Wrong
483         endif
484         if index_regex(.label$, "(Correct|Wrong)") <= 0
485                 call increment_toneevaluation_value '.tones$' '.label$'
486                 call increment_toneevaluation_value '.tones$' Commented
487         endif
488 endproc
490 procedure CreateSummaryToneEvaluation
491         .font$ = defaultFont$
492         # Summarize tone evaluation
493         Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
494         # Create texts
495         call get_toneevaluation_row Total
496         .row = get_toneevaluation_row.row
497         select Table 'initialiseSGC2.toneevaluation_table$'
498         .total = Get value... '.row' Total
499         .correct = Get value... '.row' Correct
500         if .total > 0
501                 .correctPerc = 100 * .correct / .total
502         else
503                 .correctPerc = 0
504         endif
505         .problematic = Get value... '.row' Commented
506         if .total > 0
507                 .problematicPerc = 100 * .problematic / .total
508         else
509                 .problematicPerc = 0
510         endif
512         select Table SummaryToneEvaluation
513         Append row
514         Set numeric value... 1 Total '.total'
515         Set numeric value... 1 Correct '.correctPerc'
516         Set numeric value... 1 Problematic '.problematicPerc'
517 endproc
519 ###############################################################
521 # Miscelaneous procedures
523 ###############################################################
525 # Create a file 'preferencesAppDir$'/log/logPerformance
526 # This file contains the path to the target directory where logging
527 # is to be stored.
528 # Note that this procedure is nowhere used in SGC2. It has to
529 # be explicitely called in a separate script.
531 procedure activate_sgc2_logging .targetDirectory$
532         createDirectory("'preferencesAppDir$'")
533         preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
534         createDirectory("'preferencesLogDir$'")
535         preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
536         
537         if startsWith(.targetDirectory$, "~")
538                 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
539         endif
540         
541         .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
542         newline$ >> 'preferencesLogDir$'/logPerformance.txt
543 endproc
545 # TTS synthesis
546 procedure synthesize_sound .pinyin$
547         .command$ = ""
548         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
549                 .command$ < 'speakCommandFile$'
550                 .command$ = replace$(.command$, "'newline$'", " ", 0)
551                 if macintosh
552                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
553                 elsif unix
554                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
555                 elsif windows
556                         system_nocheck call '.command$' "'.pinyin$'"'
557                 endif
558         elsif sgc2.synthesizer > 0
559                 select sgc2.synthesizer
560                 Play text... '.pinyin$'
561         endif
562 endproc
564 procedure align_recordedSound .pinyin$
565         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
566                 select Sound 'recordedSound$'
567                 .recordedTextGrid = To TextGrid... pinyin
568                 Set interval text... 1 1 '.pinyin$'
569                 select sgc2.synthesizer
570                 plus Sound 'recordedSound$'
571                 plus .recordedTextGrid
572                 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
573                 Rename... AlignedTextGrid
574                 select .recordedTextGrid
575                 Remove
576         endif
577 endproc