Started with pronunciation alignment, not working yet
[sgc2.git] / Config.praat
bloba71d605a6fd978f4a217bdb244e2ec6ec35e06c8
2 # SpeakGoodChinese 2.0
3
4 # Praat script handling configuration page
6 #     SpeakGoodChinese: Config.praat loads the code needed for the 
7 #     settings and the Settings page of SpeakGoodChinese.
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 ###############################################################
31 # Button Drawing Routines
33 ###############################################################
35 procedure DrawCredits .color$ .x .y .size
36         .size *= 0.71
37         .lineheight = 2*.size
38         call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
39         .currentFontSize = adjustFontSizeOnHeight.newFontSize
40     demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
41         demo Colour... White
42         demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 i
43         demoShow()
44         demo Colour... Black
45         call set_font_size 'defaultFontSize'    
46 endproc
48 procedure DrawDeleteList .color$ .x .y .size
49     .y += 2*.size
50     .leftX = .x - 10
51     .rightX = .x + 10
52     .botY = .y+1
53     .topY = .botY + 5
54         call set_font_size 12
55     demo Red
56     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
58         # Adapt wide of text
59         .maxWidth = (.rightX - .leftX)
60     .currentFontSize = defaultFontSize
61         call adjustFontSizeOnWidth 'defaultFont$' '.currentFontSize' '.maxWidth' '.displayWordList$'
62         .currentFontSize = adjustFontSizeOnWidth.newFontSize
63         if adjustFontSizeOnWidth.diff > 0
64                 .rightX += adjustFontSizeOnWidth.diff/2
65                 .leftX -= adjustFontSizeOnWidth.diff/2
66         endif
67         call set_font_size '.currentFontSize'
68     demo Paint rectangle... White '.leftX' '.rightX' '.botY' '.topY'
70         demo Text... '.x' Centre '.botY' Bottom '.displayWordList$'
71     demo Black
72         demoShow()
73         call set_font_size 'defaultFontSize'
74 endproc
76 procedure DrawLogging .color$ .x .y .size
77     .size /= 2
78         .y += .size
79     if .color$ = "Blue"
80         .color$ = "{0.5,0.5,1}"
81      else
82         .color$ = "Blue"
83      endif
84     demo Paint circle... '.color$' '.x' '.y' '.size'
85 endproc
87 ###############################################################
89 # Obligatory button Drawing Routines
90
91 # These MUST be defined
93 ###############################################################
95 procedure DrawReturn .color$ .x .y .size
96     call DrawConfig '.color$' '.x' '.y' '.size'
97 endproc
99 # Set the correct button states after redrawing the window
100 procedure setConfigMainPage
101     # Handle logging buttons with forced button draw
102     if fileReadable("'preferencesLogDir$'/logPerformance.txt")
103         call Draw_button 'config$' +!Logging 'config.logPerformance'
104     endif
105 endproc
107 ###############################################################
109 # Button Processing Routines
111 ###############################################################
113 procedure processConfigShuffleLists .clickX .clickY .pressed$
114         .table$ = "Config"
115         .label$ = "ShuffleLists"
116         config.shuffleLists = not config.shuffleLists
117         .displayButton = 2*config.shuffleLists
118     call Draw_button '.table$' '.label$' '.displayButton'
119 endproc
121 procedure processConfigUseSoundExample .clickX .clickY .pressed$
122         .table$ = "Config"
123         .label$ = "UseSoundExample"
124         config.useSoundExample = not config.useSoundExample
125         .displayButton = 2*config.useSoundExample
126     call Draw_button '.table$' '.label$' '.displayButton'
127 endproc
128         
129 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
130         .table$ = "Config"
131         .label$ = "Synthesis_'.tts$'"
132         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
133                 if config.synthesis$ = ""
134                         config.synthesis$ = .tts$
135                         .displayButton = 2
136                 else
137                         config.synthesis$ = ""
138                         .displayButton = 0
139                 endif
140         else
141                 config.synthesis$ = "_DISABLED_"
142                 .displayButton = -1
143         endif
144     call Draw_button '.table$' '.label$' '.displayButton'
145 endproc
146         
147 procedure processConfigStrict .clickX .clickY .pressed$
148         .table$ = "Config"
149         .label$ = "Strict"
150         config.strict = not config.strict
151         if config.strict > 0
152                 .displayButton = 2
153         else
154                 .displayButton = 0
155         endif
156         # Change TTS for Strict!
157         call set_up_TTS
158     call Draw_button '.table$' '.label$' '.displayButton'
159 endproc
161 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
162         .table$ = "Config"
163         .label$ = "DisplayPinyin"
164         config.displayPinyin = not config.displayPinyin
165         .displayButton = 2*config.displayPinyin
166     call Draw_button '.table$' '.label$' '.displayButton'
167 endproc
168                 
169 procedure processConfigDisplayChar .clickX .clickY .pressed$
170         .table$ = "Config"
171         .label$ = "DisplayChar"
172         config.displayChar = not config.displayChar
173         .displayButton = 2*config.displayChar
174     call Draw_button 'table$' '.label$' '.displayButton'
175 endproc
177 procedure processConfigDisplayTrans .clickX .clickY .pressed$
178         .table$ = "Config"
179         .label$ = "DisplayTrans"
180         config.displayTrans = not config.displayTrans
181         .displayButton = 2*config.displayTrans
182     call Draw_button 'table$' '.label$' '.displayButton'
183 endproc
185 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
186         .table$ = "Config"
187         .label$ = "DisplayNumbers"
188         config.displayNumbers = not config.displayNumbers
189         .displayButton = 2*config.displayNumbers
190     call Draw_button 'table$' '.label$' '.displayButton'
191 endproc
193 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
194         .table$ = "Config"
195         .label$ = "Language_'.language$'"
196         call processLanguageCodes '.table$' '.label$'
197 endproc
198         
199 procedure processConfigShowBackground .clickX .clickY .pressed$
200         .table$ = "Config"
201         .label$ = "ShowBackground"
202         config.showBackground = not config.showBackground
203         .displayButton = 2*config.showBackground
204     call Draw_button 'table$' '.label$' '.displayButton'
205 endproc
207 procedure processConfigInput .input$ .clickX .clickY .pressed$
208         .table$ = "Config"
209         .label$ = "Input_'.input$'"
210     call Draw_button '.table$' Input_'config.input$' 0
211         config.input$ = .input$
212     call Draw_button '.table$' Input_'config.input$' 2
213 endproc
215 procedure processConfigRegister .register .clickX .clickY .pressed$
216         .table$ = "Config"
217         .label$ = "Register_'.register'"
218         call setRegisterFromLabel '.table$' '.label$'
219 endproc
220         
221 procedure setRegisterFromLabel .table$ .label$
222     call Draw_button '.table$' Register_'config.register' 0
223     call Draw_button '.table$' '.label$' 2
224     # Someone might have to use more than 3 chars for the config.register code
225     .numChars = length(.label$) - length("Register_")
226         .registerText$ = right$(.label$, .numChars)
227         config.register = '.registerText$'
228 endproc
230 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
231         .table$ = "Config"
232         .label$ = "DeleteWordlist"
234     # Do not process undeletable word lists, only those stored in the 
235     # preferencesDirectory$ can be deleted
236     if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or     fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
237                 call findLabel '.table$' !'.label$'
238                 if findLabel.row > 0
239                 select Table '.table$'
240                 .alertText$ = Get value... 'findLabel.row' Text
241                 .confirmKey$ = Get value... 'findLabel.row' Key
242                 .popupText$ = Get value... 'findLabel.row' Helptext
243         else
244             exit Cannot find delete directive: '.table$' !'.label$'
245         endif
246         call write_text_popup 'defaultFont$' 14 '.popupText$'
247         call Draw_button '.table$' '.label$' 2
248         alertText$ = .alertText$
249         call Draw_button '.table$' '.label$' 3
250         alertText$ = ""
251         
252                 # Wait for confirmation
253                 demoWaitForInput()
254         if demoInput(.confirmKey$)
255                 .deleteWordListDir$ = wordlistName$
256                 call load_word_list "'localWordlistDir$'" 1
257                 call removeWordlist '.deleteWordListDir$'
258                         call load_word_list "'localWordlistDir$'" 0
259         endif
260                 call Draw_button '.table$' '.label$' 0
261                 call Draw_config_page
262     endif
263 endproc
265 wordlistTag$ = "Wordlist name"        
266 procedure processConfigInstallWordlist .clickX .clickY .pressed$
267         .table$ = "Config"
268         .label$ = "InstallWordlist"
269     call Draw_button '.table$' '.label$' 1
270         if windows
271                 # Do not use the automatic sgc list option, ask for a wordlist NAME
272                 # Get help text
273                 call findLabel '.table$' '.label$'
274                 .row = findLabel.row
275                 select Table '.table$'
276                 .openDialogue$ = Get value... '.row' Helptext
277                 call convert_praat_to_latin1 '.openDialogue$'
278                 .openDialogue$ = convert_praat_to_latin1.text$
279                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
280                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
281                 beginPause(.openDialogue$)
282                         sentence (wordlistTag$, "")
283                 clicked = endPause ("Cancel", "Open", 2)
284                 .wordlist_Name$ = ""
285                 if clicked = 2
286                         .wordlist_Name$ = '.wordlistButton$'$
287                 endif
288                 call install_wordlists_by_name '.wordlist_Name$'
289         else
290         call sgc2wordlist 'homeDirectory$'
291         call sgc2wordlist 'homeDirectory$'/Downloads
292         call sgc2wordlist 'homeDirectory$'/Documents
293         call sgc2wordlist 'homeDirectory$'/My Documents
294         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
295         call sgc2wordlist 'homeDirectory$'/Desktop
296         call sgc2wordlist 'preferencesAppDir$'
297         endif
298         call load_word_list "'localWordlistDir$'" 0
299     call Draw_button '.table$' '.label$' 0
300         call Draw_config_page
301 endproc
303 procedure processConfigOpenWordlist .clickX .clickY .pressed$
304         .table$ = "Config"
305         .label$ = "OpenWordlist"
306     call Draw_button '.table$' '.label$' 1
308         # Get help text
309         call findLabel '.table$' '.label$'
310         .row = findLabel.row
311         select Table '.table$'
312         .openDialogue$ = Get value... '.row' Helptext
313         call convert_praat_to_latin1 '.openDialogue$'
314         .openDialogue$ = convert_praat_to_latin1.text$
316         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
317         call install_wordlists_by_name '.wordlist_Name$'
318         call load_word_list "'localWordlistDir$'" 0
319     call Draw_button '.table$' '.label$' 0
320         call Draw_config_page
321 endproc
323 procedure processConfigPerfSummary .clickX .clickY .pressed$
324         .table$ = "Config"
325         .label$ = "PerfSummary"
326     call Draw_button '.table$' '.label$' 1
327         
328         call loadTable SummaryToneEvaluation
329         call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
330         demoWaitForInput()
331         select Table SummaryToneEvaluation
332         Remove
334         call Draw_button '.table$' '.label$' 0
335         call Draw_config_page
336 endproc
338 procedure processConfigListPerf .clickX .clickY .pressed$
339         .table$ = "Config"
340         .label$ = "ListPerf"
341     call Draw_button '.table$' '.label$' 1
342         
343         call write_tabbed_table 'initialiseSGC2.toneevaluation_table$' Evaluation_'config.language$'
344         demoWaitForInput()
345         
346     call Draw_button '.table$' '.label$' 0
347         call Draw_config_page
348 endproc
350 procedure processConfigSavePerf .clickX .clickY .pressed$
351         .table$ = "Config"
352         .label$ = "SavePerf"
353     call Draw_button '.table$' '.label$' 1
354         
355         # Get help text
356         call findLabel '.table$' '.label$'
357         .row = findLabel.row
358         select Table '.table$'
359         .openDialogue$ = Get value... '.row' Helptext
360         call convert_praat_to_latin1 '.openDialogue$'
361         .openDialogue$ = convert_praat_to_latin1.text$
363         .wordlist_Name$ = chooseWriteFile$ (.openDialogue$, "Performance.Table")
364         if .wordlist_Name$ <> ""
365                 select Table 'initialiseSGC2.toneevaluation_table$'
366                 Write to table file... '.wordlist_Name$'
367         endif
368         
369     call Draw_button '.table$' '.label$' 0
370         call Draw_config_page
371 endproc
373 procedure processConfigManual .clickX .clickY .pressed$
374         .table$ = "Config"
375         .label$ = "Manual"
376         call Draw_button '.table$' '.label$' 1
377         if fileReadable("ManPages/Demo_Application.man")
378                 Read from file... ManPages/Demo_Application.man
379         else
380                 Go to manual page... Demo Application
381         endif
382         # Wait until the manual is put to the background
383         demoWaitForInput()
384     call Draw_button '.table$' '.label$' 0
385     demo Erase all
386     call Draw_config_page
387 endproc
389 ###############################################################
391 # Obligatory button Processing Routines
393 # These MUST be defined
395 ###############################################################
397 procedure processConfigReturn .clickX .clickY .pressed$
398         .table$ = "Config"
399         .label$ = "Return"
400         call Draw_button '.table$' '.label$' 1
401         call write_preferences ""
402 endproc
404 procedure processConfigRefresh .clickX .clickY .pressed$
405         .table$ = "Config"
406         .label$ = "Refresh"
407         call Draw_config_page
408 endproc
410 procedure processConfigCredits .clickX .clickY .pressed$
411         .table$ = "Config"
412         .label$ = "Credits"
413         call Draw_button '.table$' '.label$' 1
414         call write_text_table Credits_'config.language$'
415         demoWaitForInput()
416     call Draw_button '.table$' '.label$' 0
417     demo Erase all
418     call Draw_config_page
419 endproc
421 procedure processConfigHelp .clickX .clickY .pressed$
422         .table$ = "Config"
423         .label$ = "Help"
424         call help_loop  '.table$' Draw_config_page
425 endproc
427 ###############################################################
429 # Miscelaneous supporting code
431 ###############################################################
432 procedure install_wordlists_by_name .wordlist_Name$
433         if .wordlist_Name$ <> ""
434                 if index(.wordlist_Name$, "wordlist.")
435                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\]wordlist\.([^/\\]+)$", "", 0)
436                 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
437                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
438                 endif
439                 if index_regex(.wordlist_Name$, "[/\\]")
440                         .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
441                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
442                         call readWordlist "'.sourceDir$'" '.file$'
443                 else
444                         .start = 1
445                         if index(.wordlist_Name$, ".")
446                                 .start = 4
447                         endif
448                         .extension1$ = ".sgc"
449                         .extension2$ = ".Table"
450                         .extension3$ = ".txt"
451                         .extension4$ = ""
452                         for .e from .start to 4
453                                 .currentExtension$ = .extension'.e'$
454                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
455                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
456                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
457                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
458                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
459                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
460                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
461                         endfor
462                 endif
463         endif
464 endproc
466 # Word lists: It is a VERY good idea to make sure that word-lists
467 # have unique names.
468 procedure load_word_list .localdir$ .relnumber
469     # Remove old word list
470     if wordlist$ <> ""
471                 select Table 'wordlist$'
472                 Remove
473                 call wipeArea 'wipeWordlistArea$'
474                 wordlist$ = ""
475     endif
476     
477         # Create Table that will recieve the wordlists and directories
478         Create Table with column names... AllWordLists 0 Name Directory
479         .currentWordlistRow = 0
480         
481         # Start with the word lists in the distribution, unless the local directory exists!
482         call CreateCreateWordlists
483         select Table CreateWordlists
484     .numLists = Get number of rows
485         for .i to .numLists
486                 select Table CreateWordlists
487        .currentName$ = Get value... '.i' Name
488            if not (fileReadable("'.localdir$'/'.currentName$'") or fileReadable("'.localdir$'/'.currentName$'/wordlist.Table") or fileReadable("'.localdir$'/'.currentName$'/wordlist.txt"))
489                    .currentDirectory$ = "*call Create'.currentName$'"
490                         select Table AllWordLists
491                         Append row
492                         .currentWordlistRow = Get number of rows
493                         Set string value... '.currentWordlistRow' Name '.currentName$'
494                     .currentDirectory$ = "*call Create'.currentName$'"
495                         Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
496                 endif
497         endfor
498         select Table CreateWordlists
499         Remove
501         # First the global word lists
502         if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
503         Create Strings as directory list... WordList 'globalwordlists$'
504         .numLists = Get number of strings
505         for .i to .numLists
506             select Strings WordList
507             .currentName$ = Get string... '.i'
508                         if .currentName$ <> "directory.txt"
509                                 select Table AllWordLists
510                                 Append row
511                                 .currentWordlistRow = Get number of rows
512                                 Set string value... '.currentWordlistRow' Name '.currentName$'
513                         .currentDirectory$ = globalwordlists$+"/"+.currentName$
514                                 Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
515                         endif
516         endfor
517         select Strings WordList
518         Remove
519         endif
520         
521         # Now the preferences word lists
522         if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
523         Create Strings as directory list... WordList 'sgc2wordlists$'
524         .numLists = Get number of strings
525         for .i to .numLists
526             select Strings WordList
527             .currentName$ = Get string... '.i'
528                         if .currentName$ <> "directory.txt"
529                                 select Table AllWordLists
530                                 Append row
531                                 .currentWordlistRow = Get number of rows
532                                 Set string value... '.currentWordlistRow' Name '.currentName$'
533                         .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
534                                 Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
535                         endif
536         endfor
537         select Strings WordList
538         Remove
539         endif
540         
541         # Finally, the local word lists
542         if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
543         Create Strings as directory list... WordList '.localdir$'
544         .numLists = Get number of strings
545         for .i to .numLists
546             select Strings WordList
547             .currentName$ = Get string... '.i'
548                         if .currentName$ <> "directory.txt"
549                                 select Table AllWordLists
550                                 Append row
551                                 .currentWordlistRow = Get number of rows
552                                 Set string value... '.currentWordlistRow' Name '.currentName$'
553                         .currentDirectory$ = .localdir$+"/"+.currentName$
554                                 Set string value... '.currentWordlistRow' Directory '.currentDirectory$'
555                         endif
556         endfor
557         select Strings WordList
558         Remove
559     endif
561         # Get the position of the current word list
562         select Table AllWordLists
563         .currentNumber = 1
564     .numLists = Get number of rows
566         if wordlistName$ <> ""
567                 select Table AllWordLists
568                 .currentNumber = Search column... Name 'wordlistName$'
569         endif
571     wordlistNum = .currentNumber + .relnumber
572     if wordlistNum > .numLists
573         wordlistNum = 1
574         elsif wordlistNum < 1 and .numLists > 0
575                 wordlistNum = .numLists
576     endif
577     select Table AllWordLists
578     wordlistName$ = Get value... 'wordlistNum' Name
579         .dirWordlistName$ = Get value... 'wordlistNum' Directory
580     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
581         
582         # Read in full tables
583         if fileReadable("'.dirString$'/wordlist.Table")
584         Read from file... '.dirString$'/wordlist.Table
585         Rename... 'wordlistName$'
586         # Praat wil change the name if it feels like it
587         wordlist$ = selected$("Table")
588         # Handle (legacy) simple word lists
589         elsif fileReadable("'.dirString$'/wordlist.txt")
590                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
591         wordlist$ = selected$("Table")
592                 Read Strings from raw text file... '.dirString$'/wordlist.txt
593                 Rename... RawWordList
594                 .numWordStrings = Get number of strings
595                 for .i to .numWordStrings
596                         select Strings RawWordList
597                         .currentLine$ = Get string... '.i'
598                         .currentPinyin$ = extractWord$(.currentLine$, "")
599                         if length(.currentLine$) > length(.currentPinyin$)+1
600                                 .currentFile$ = right$(.currentLine$, length(.currentPinyin$)+1)
601                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.spx")
602                                 .currentFile$ = .currentPinyin$+".spx"
603                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
604                                 .currentFile$ = .currentPinyin$+".flac"
605                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
606                                 .currentFile$ = .currentPinyin$+".wav"
607                         elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
608                                 .currentFile$ = .currentPinyin$+".mp3"
609                         else
610                                 .currentFile$ = "-"
611                         endif
612                         select Table 'wordlist$'
613                         Append row
614                         Set string value... '.i' Pinyin '.currentPinyin$'
615                         Set string value... '.i' Sound '.currentFile$'
616                 endfor
617                 select Strings RawWordList
618                 Remove
619                 
620                 select Table 'wordlist$'
621         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
622                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
623         wordlist$ = selected$("Table")
624                 Create Strings as file list... RawWordList '.dirString$'/*
625                 .numWordStrings = Get number of strings
626                 .i = 0
627                 for .j to .numWordStrings
628                         select Strings RawWordList
629                         .currentLine$ = Get string... '.j'
630                         .currentFile$ = extractWord$(.currentLine$, "")
631                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
632                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
633                                 select Table 'wordlist$'
634                                 Append row
635                                 .i += 1
636                                 Set string value... '.i' Pinyin '.currentPinyin$'
637                                 Set string value... '.i' Sound '.currentFile$'
638                         endif
639                 endfor
640                 select Strings RawWordList
641                 Remove
642                 
643                 select Table 'wordlist$'
644         elsif startsWith(.dirString$, "*call ")
645                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
646                 '.callProcedure$'
647                 wordlist$ = selected$("Table")
648         endif
650         # Can this wordlist be deleted?
651         if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
652                 config.deleteWordlist = 0
653         else
654                 config.deleteWordlist = -1              
655         endif
657         # Add a Character and Translation column if missing
658         if wordlist$ <> ""
659                 select Table 'wordlist$'
660         numberOfWords = Get number of rows
661                 .characterColumn = Get column index... Character
662                 if not .characterColumn
663                         Append column... Character
664                         for .i to numberOfWords
665                                 Set string value... '.i' Character -
666                         endfor
667                 endif
668                 .translationColumn = Get column index... Translation
669                 if not .translationColumn
670                         Append column... Translation
671                         for .i to numberOfWords
672                                 Set string value... '.i' Translation -
673                         endfor
674                 endif
675         endif
676         
677         # Remove all rows without Pinyin
678         .numRows = Get number of rows
679         for i to .numRows
680                 .rowNum = .numRows - i + 1
681                 .pinyinValue$ = Get value... '.rowNum' Pinyin
682                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
683                         Remove row... '.rowNum'
684                 endif
685         endfor
686         numberOfWords = Get number of rows
687         
688         # Shuffle words if requested
689     if config.shuffleLists
690         Randomize rows
691     endif
693         # Clean up
694     select Table AllWordLists
695     Remove
696         
697     # There were no Word Lists
698     label NOWORDLISTS
699     if .numLists <= 0
700         wordlistName$ = "No Word Lists available"
701         wordlistNum = 1
702                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
703         wordlist$ = selected$("Table")
704         .i = 0
705                 Append row
706                 .i += 1
707                 Set string value... '.i' Pinyin ni3hao3
708                 Set string value... '.i' Character -
709                 Set string value... '.i' Translation -
710                 Set string value... '.i' Sound -
711                 Append row
712                 .i += 1
713                 Set string value... '.i' Pinyin xie4xie0
714                 Set string value... '.i' Character -
715                 Set string value... '.i' Translation -
716                 Set string value... '.i' Sound -
717                 Append row
718                 .i += 1
719                 Set string value... '.i' Pinyin zai4jian4
720                 Set string value... '.i' Character -
721                 Set string value... '.i' Translation -
722                 Set string value... '.i' Sound -
723         numberOfWords = Get number of rows
724     endif
726         call set_window_title 'buttons$' 'wordlistName$'
727 endproc
729 procedure display_word_list_name
730     .xtext = 50
731     .ytext = 12
732    call reset_viewport
733     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
734     call wipeArea 'wipeWordlistArea$'
735         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
736         .currentFontSize = adjustFontSizeOnHeight.newFontSize
738     demo Blue
739         demo Text special... '.xtext' Centre '.ytext' Bottom Helvetica '.currentFontSize' 0 '.displayWordList$'
740     demo Black
741         demoShow()
742         call set_font_size 'defaultFontSize'
743 endproc
745 procedure write_word_list
746         # Draw the contour
747         call draw_tone_contour
749         # Write current Pinyin text
750         call display_text Black
751         
752         # Write the current word list name
753         call display_word_list_name
754 endproc
756 procedure start_logging
757         if fileReadable("'preferencesLogDir$'/logPerformance.txt")
758                 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
759                 .logDirectory$ = extractWord$(.logDirectory$, "")
760                 if .logDirectory$ = "" or not (fileReadable(.logDirectory$) or  fileReadable("'.logDirectory$'/directory.txt")
761                         .logDirectory$ = "'preferencesLogDir$'"
762                 endif
763                 currentLogDirectory$ = "'.logDirectory$'/log'logtimeStamp$'"
764                 createDirectory(currentLogDirectory$)
765                 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
766                         .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
767                         .headerLine$ > 'currentLogDirectory$'/wordlist.Table
768                 endif
769                         # Flip switch
770                         config.logPerformance = 1
771         endif
772 endproc
774 procedure log_command .logtext$
775         if logging
776                 fileappend "'currentLogDirectory$'/logFile.txt" '.logtext$''newline$'
777         endif
778 endproc
780 procedure log_performance .recordedSound$ .register .proficiency .pinyin$ .choice$
781         # Log files
782         .currentDate$ = date$()
783         .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
784         .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
786         if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
787            .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
788            .logtext$ = "# #+.pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
789        # Plain log text
790            fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
791            
792        # A wordlist.Table
793        fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
794        # The recorded sound
795            select Sound 'recordedSound$'
796            Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
797         endif
798 endproc
800 procedure paint_logging_light
801     select Table Config
802     .row = Search column... Label !Logging
803         if .row < 1
804                 exit Button Table Config does not have a row with label !Logging
805         endif
806         # Get button values
807     .leftX = Get value... '.row' LeftX
808     .rightX = Get value... '.row' RightX
809     .lowY = Get value... '.row' LowY
810     .highY = Get value... '.row' HighY
811     .buttonColor$ = Get value... '.row' Color
812     .centerX = (.leftX + .rightX)/2
813     .centerY = (.lowY + .highY)/2
814     .radius = (.highY - .lowY )/(4*2)
815     .wipeRadius = 1.1*.radius
816     if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
817         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
818         demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
819         demoShow()
820     else
821         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
822         demoShow()
823     endif
824 endproc
826 # Uninstall word lists
827 procedure removeWordlist .deletedWordlistName$
828     .targetDir$ = ""
829     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
830         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
831     endif
832         if .targetDir$ <> ""
833         Create Strings as file list... DeleteList '.targetDir$'/*
834         .numdeleteFiles = Get number of strings
835         for .i to .numdeleteFiles
836                 .file$ = Get string... '.i'
837                 deleteFile("'.targetDir$'/'.file$'")
838         endfor
839         filedelete '.targetDir$'
840                 if windows
841                         system rmdir "'.targetDir$'" /s /q
842                 endif
843                 # Remove deleted word list
844                 select Strings DeleteList
845                 plus Table 'wordlist$'
846                 Remove
847                 wordlist$ = ""
848         endif
849 endproc
851 # Install word lists
852 procedure sgc2wordlist .sourceDir$
853         if startsWith(.sourceDir$, "preferencesDirectory$")
854                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
855         endif
857         .targetDirectory$ = "'sgc2wordlists$'"
858         if  fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
859                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
860                 .numFiles = Get number of strings
861                 for .i to .numFiles
862                         select Strings PackageList
863                         .file$ = Get string... '.i'
864                         call readWordlist '.sourceDir$' '.file$'
865                 endfor
867                 select Strings PackageList
868                 Remove
869         endif
870 endproc
872 # Debuggin remarks!!!
873 # fileReadable(<directory>) does not work in Windows
874 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
875 # And yet only the 7z decompression has been implemented
876 windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z"" x"
877 procedure readWordlist .sourceDir$ .file$
878         # No use doing anything if the source does not exist
879         if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
880                 # What will be the target wordlist directory?
881                 .targetDirectory$ = "'sgc2wordlists$'"
882                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
883                 if .dirname$ = ""
884                         .dirname$ = .file$
885                 endif
886                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
887                 # Wordlist directory does not exist, neither locally nor in the preferences
888                 if not (fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
889                         .wasWordList = 0
890                         # Move source to destination
891                         if index(.file$, ".sgc") or index(.file$, ".zip")
892                                 # Create wordlist directory
893                                 createDirectory(.wordlistDirectory$)
894                                 .wasWordList = 1
895                                 if macintosh or unix
896                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
897                                         system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
898                                 elsif windows
899                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
900                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
901                                         system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /q
902                                         system chdir "'.winWordListDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
903                                 endif
904                                 deleteFile("'.wordlistDirectory$'/'.file$'")
905                         elsif index_regex(.file$, "\.(Table|txt)")
906                                 # Create wordlist directory
907                                 createDirectory(.wordlistDirectory$)
908                                 .wasWordList = 1
909                                 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt)$", "\1", 0)
910                                 if macintosh or unix
911                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
912                                 elsif windows
913                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
914                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
915                                         system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'\wordlist.'.extension$'" /q
916                                 endif
917                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
918                                 # Copy wordlist directory
919                                 .wasWordList = 1
920                                 if macintosh or unix
921                                         system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
922                                 elsif windows
923                                         createDirectory(.wordlistDirectory$)
924                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
925                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
926                                         system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
927                                 endif
928                         endif
929                         
930                         # Set current word list to read list
931                         if .wasWordList
932                                 wordlistName$ = .dirname$
933                         else
934                                 .table$ = "Config"
935                                 .label$ = "!NotAWordlist"
937                                 # Get help text
938                                 call findLabel '.table$' '.label$'
939                                 .row = findLabel.row
940                                 select Table '.table$'
941                                 .helpText$ = Get value... '.row' Helptext
942                                 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
943                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
944                                 # Wait for confirmation
945                                 demoWaitForInput()
946                         endif
947                 endif
948         endif
949 endproc