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