Corrections in handling missing neutral tones
[sgc2.git] / Config.praat
blob56e530375e20dace8340f248892e9ed6d9d65dc7
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 DrawManual .color$ .x .y .size
77         .size *= 0.71
78         .lineheight = 2*.size
79         call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
80         .currentFontSize = adjustFontSizeOnHeight.newFontSize
81     demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
82         demo Colour... White
83         demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 M
84         demoShow()
85         demo Colour... Black
86         call set_font_size 'defaultFontSize'    
87 endproc
89 procedure DrawLogging .color$ .x .y .size
90     .size /= 2
91         .y += .size
92     if .color$ = "Blue"
93         .color$ = "{0.5,0.5,1}"
94      else
95         .color$ = "Blue"
96      endif
97     demo Paint circle... '.color$' '.x' '.y' '.size'
98 endproc
100 ###############################################################
102 # Obligatory button Drawing Routines
104 # These MUST be defined
106 ###############################################################
108 procedure DrawReturn .color$ .x .y .size
109     call DrawConfig '.color$' '.x' '.y' '.size'
110 endproc
112 # Set the correct button states after redrawing the window
113 procedure setConfigMainPage
114         call testLoadTable SummaryToneEvaluation
115         if testLoadTable.table <= 0
116                 call Draw_button 'config$' PerfSummary 1
117         endif
118     # Handle logging buttons with forced button draw
119     if fileReadable("'preferencesLogDir$'/logPerformance.txt")
120         call Draw_button 'config$' +!Logging 'config.logPerformance'
121     endif
122 endproc
124 ###############################################################
126 # Button Processing Routines
128 ###############################################################
130 procedure processConfigShuffleLists .clickX .clickY .pressed$
131         .table$ = "Config"
132         .label$ = "ShuffleLists"
133         config.shuffleLists = not config.shuffleLists
134         .displayButton = 2*config.shuffleLists
135     call Draw_button '.table$' '.label$' '.displayButton'
136 endproc
138 procedure processConfigUseSoundExample .clickX .clickY .pressed$
139         .table$ = "Config"
140         .label$ = "UseSoundExample"
141         config.useSoundExample = not config.useSoundExample
142         .displayButton = 2*config.useSoundExample
143     call Draw_button '.table$' '.label$' '.displayButton'
144 endproc
145         
146 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
147         .table$ = "Config"
148         .label$ = "Synthesis_'.tts$'"
149         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
150                 if config.synthesis$ = ""
151                         config.synthesis$ = .tts$
152                         .displayButton = 2
153                 else
154                         config.synthesis$ = ""
155                         .displayButton = 0
156                 endif
157         else
158                 config.synthesis$ = "_DISABLED_"
159                 .displayButton = -1
160         endif
161     call Draw_button '.table$' '.label$' '.displayButton'
162 endproc
163         
164 procedure processConfigStrict .clickX .clickY .pressed$
165         .table$ = "Config"
166         .label$ = "Strict"
167         config.strict = not config.strict
168         if config.strict > 0
169                 .displayButton = 2
170         else
171                 .displayButton = 0
172         endif
173         # Change TTS for Strict!
174         call set_up_TTS
175     call Draw_button '.table$' '.label$' '.displayButton'
176 endproc
178 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
179         .table$ = "Config"
180         .label$ = "DisplayPinyin"
181         config.displayPinyin = not config.displayPinyin
182         .displayButton = 2*config.displayPinyin
183     call Draw_button '.table$' '.label$' '.displayButton'
184 endproc
185                 
186 procedure processConfigDisplayChar .clickX .clickY .pressed$
187         .table$ = "Config"
188         .label$ = "DisplayChar"
189         config.displayChar = not config.displayChar
190         .displayButton = 2*config.displayChar
191     call Draw_button 'table$' '.label$' '.displayButton'
192 endproc
194 procedure processConfigDisplayTrans .clickX .clickY .pressed$
195         .table$ = "Config"
196         .label$ = "DisplayTrans"
197         config.displayTrans = not config.displayTrans
198         .displayButton = 2*config.displayTrans
199     call Draw_button 'table$' '.label$' '.displayButton'
200 endproc
202 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
203         .table$ = "Config"
204         .label$ = "DisplayNumbers"
205         config.displayNumbers = not config.displayNumbers
206         .displayButton = 2*config.displayNumbers
207     call Draw_button 'table$' '.label$' '.displayButton'
208 endproc
210 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
211         .table$ = "Config"
212         .label$ = "Language_'.language$'"
213         call processLanguageCodes '.table$' '.label$'
214 endproc
215         
216 procedure processConfigShowBackground .clickX .clickY .pressed$
217         .table$ = "Config"
218         .label$ = "ShowBackground"
219         config.showBackground = not config.showBackground
220         .displayButton = 2*config.showBackground
221     call Draw_button 'table$' '.label$' '.displayButton'
222 endproc
224 procedure processConfigInput .input$ .clickX .clickY .pressed$
225         .table$ = "Config"
226         .label$ = "Input_'.input$'"
227     call Draw_button '.table$' Input_'config.input$' 0
228         config.input$ = .input$
229     call Draw_button '.table$' Input_'config.input$' 2
230 endproc
232 procedure processConfigRegister .register .clickX .clickY .pressed$
233         .table$ = "Config"
234         .label$ = "Register_'.register'"
235         call setRegisterFromLabel '.table$' '.label$'
236 endproc
237         
238 procedure setRegisterFromLabel .table$ .label$
239     call Draw_button '.table$' Register_'config.register' 0
240     call Draw_button '.table$' '.label$' 2
241     # Someone might have to use more than 3 chars for the config.register code
242     .numChars = length(.label$) - length("Register_")
243         .registerText$ = right$(.label$, .numChars)
244         config.register = '.registerText$'
245 endproc
247 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
248         .table$ = "Config"
249         .label$ = "DeleteWordlist"
251     # Do not process undeletable word lists, only those stored in the 
252     # preferencesDirectory$ can be deleted
253     if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or     fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
254                 call findLabel '.table$' !'.label$'
255                 if findLabel.row > 0
256                 select Table '.table$'
257                 .alertText$ = Get value... 'findLabel.row' Text
258                 .confirmKey$ = Get value... 'findLabel.row' Key
259                 .popupText$ = Get value... 'findLabel.row' Helptext
260         else
261             exit Cannot find delete directive: '.table$' !'.label$'
262         endif
263         call write_text_popup 'defaultFont$' 14 '.popupText$'
264         call Draw_button '.table$' '.label$' 2
265         alertText$ = .alertText$
266         call Draw_button '.table$' '.label$' 3
267         alertText$ = ""
268         
269                 # Wait for confirmation
270                 demoWaitForInput()
271         if demoInput(.confirmKey$)
272                 .deleteWordListDir$ = wordlistName$
273                 call load_word_list "'localWordlistDir$'" 1
274                 call removeWordlist '.deleteWordListDir$'
275                         call load_word_list "'localWordlistDir$'" 0
276         endif
277                 call Draw_button '.table$' '.label$' 0
278                 call Draw_config_page
279     endif
280 endproc
282 wordlistTag$ = "Wordlist name"        
283 procedure processConfigInstallWordlist .clickX .clickY .pressed$
284         .table$ = "Config"
285         .label$ = "InstallWordlist"
286     call Draw_button '.table$' '.label$' 1
287         if windows
288                 # Do not use the automatic sgc list option, ask for a wordlist NAME
289                 # Get help text
290                 call findLabel '.table$' '.label$'
291                 .row = findLabel.row
292                 select Table '.table$'
293                 .openDialogue$ = Get value... '.row' Helptext
294                 call convert_praat_to_latin1 '.openDialogue$'
295                 .openDialogue$ = convert_praat_to_latin1.text$
296                 .wordlistButton$ = replace$(wordlistTag$, " ", "_", 0)
297                 .wordlistButton$ = replace_regex$(.wordlistButton$, "^.", "\l&", 0)
298                 beginPause(.openDialogue$)
299                         sentence (wordlistTag$, "")
300                 clicked = endPause ("Cancel", "Open", 2)
301                 .wordlist_Name$ = ""
302                 if clicked = 2
303                         .wordlist_Name$ = '.wordlistButton$'$
304                 endif
305                 call install_wordlists_by_name '.wordlist_Name$'
306         else
307         call sgc2wordlist 'homeDirectory$'
308         call sgc2wordlist 'homeDirectory$'/Downloads
309         call sgc2wordlist 'homeDirectory$'/Documents
310         call sgc2wordlist 'homeDirectory$'/My Documents
311         call sgc2wordlist 'homeDirectory$'/My Documents/Downloads
312         call sgc2wordlist 'homeDirectory$'/Desktop
313         call sgc2wordlist 'preferencesAppDir$'
314         endif
315         call load_word_list "'localWordlistDir$'" 0
316     call Draw_button '.table$' '.label$' 0
317         call Draw_config_page
318 endproc
320 procedure processConfigOpenWordlist .clickX .clickY .pressed$
321         .table$ = "Config"
322         .label$ = "OpenWordlist"
323     call Draw_button '.table$' '.label$' 1
325         # Get help text
326         call findLabel '.table$' '.label$'
327         .row = findLabel.row
328         select Table '.table$'
329         .openDialogue$ = Get value... '.row' Helptext
330         call convert_praat_to_latin1 '.openDialogue$'
331         .openDialogue$ = convert_praat_to_latin1.text$
333         .wordlist_Name$ = chooseReadFile$ (.openDialogue$)
334         call install_wordlists_by_name '.wordlist_Name$'
335         call load_word_list "'localWordlistDir$'" 0
336     call Draw_button '.table$' '.label$' 0
337         call Draw_config_page
338 endproc
340 procedure processConfigPerfSummary .clickX .clickY .pressed$
341         .table$ = "Config"
342         .label$ = "PerfSummary"
343         
344         call testLoadTable SummaryToneEvaluation
345         if testLoadTable.table > 0
346                 call Draw_button '.table$' '.label$' 1
347                 call loadTable SummaryToneEvaluation
348                 call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
349                 demoWaitForInput()
350                 select Table SummaryToneEvaluation
351                 Remove
353                 call Draw_button '.table$' '.label$' 0
354                 call Draw_config_page
355         endif
356 endproc
358 procedure processConfigListPerf .clickX .clickY .pressed$
359         .table$ = "Config"
360         .label$ = "ListPerf"
361     call Draw_button '.table$' '.label$' 1
362         
363         call write_tabbed_table 'initialiseSGC2.toneevaluation_table$' Evaluation_'config.language$'
364         demoWaitForInput()
365         
366     call Draw_button '.table$' '.label$' 0
367         call Draw_config_page
368 endproc
370 procedure processConfigSavePerf .clickX .clickY .pressed$
371         .table$ = "Config"
372         .label$ = "SavePerf"
373     call Draw_button '.table$' '.label$' 1
374         
375         # Get help text
376         call findLabel '.table$' '.label$'
377         .row = findLabel.row
378         select Table '.table$'
379         .openDialogue$ = Get value... '.row' Helptext
380         call convert_praat_to_latin1 '.openDialogue$'
381         .openDialogue$ = convert_praat_to_latin1.text$
383         .wordlist_Name$ = chooseWriteFile$ (.openDialogue$, "Performance.Table")
384         if .wordlist_Name$ <> ""
385                 select Table 'initialiseSGC2.toneevaluation_table$'
386                 Write to table file... '.wordlist_Name$'
387         endif
388         
389     call Draw_button '.table$' '.label$' 0
390         call Draw_config_page
391 endproc
393 procedure processConfigManual .clickX .clickY .pressed$
394         .table$ = "Config"
395         .label$ = "Manual"
396         call Draw_button '.table$' '.label$' 1
397         if fileReadable("ManPages/SpeakGoodChinese_2.man")
398                 Read from file... ManPages/SpeakGoodChinese_2.man
399         else
400                 Go to manual page... SpeakGoodChinese 2
401         endif
402         # Wait until the manual is put to the background
403         demoWaitForInput()
404     call Draw_button '.table$' '.label$' 0
405     demo Erase all
406     call Draw_config_page
407 endproc
409 ###############################################################
411 # Obligatory button Processing Routines
413 # These MUST be defined
415 ###############################################################
417 procedure processConfigReturn .clickX .clickY .pressed$
418         .table$ = "Config"
419         .label$ = "Return"
420         call Draw_button '.table$' '.label$' 1
421         call write_preferences ""
422 endproc
424 procedure processConfigRefresh .clickX .clickY .pressed$
425         .table$ = "Config"
426         .label$ = "Refresh"
427         call Draw_config_page
428 endproc
430 procedure processConfigCredits .clickX .clickY .pressed$
431         .table$ = "Config"
432         .label$ = "Credits"
433         call Draw_button '.table$' '.label$' 1
434         call write_text_table Credits_'config.language$'
435         demoWaitForInput()
436     call Draw_button '.table$' '.label$' 0
437     demo Erase all
438     call Draw_config_page
439 endproc
441 procedure processConfigHelp .clickX .clickY .pressed$
442         .table$ = "Config"
443         .label$ = "Help"
444         call help_loop  '.table$' Draw_config_page
445 endproc
447 ###############################################################
449 # Miscelaneous supporting code
451 ###############################################################
452 procedure install_wordlists_by_name .wordlist_Name$
453         if .wordlist_Name$ <> ""
454                 if index(.wordlist_Name$, "wordlist.")
455                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\]wordlist\.([^/\\]+)$", "", 0)
456                 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
457                         .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
458                 endif
459                 if index_regex(.wordlist_Name$, "[/\\]")
460                         .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
461                         .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
462                         call readWordlist "'.sourceDir$'" '.file$'
463                 else
464                         .start = 1
465                         if index(.wordlist_Name$, ".")
466                                 .start = 4
467                         endif
468                         .extension1$ = ".sgc"
469                         .extension2$ = ".Table"
470                         .extension3$ = ".txt"
471                         .extension4$ = ".tsv"
472                         .extension5$ = ""
473                         for .e from .start to 5
474                                 .currentExtension$ = .extension'.e'$
475                         call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
476                         call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
477                         call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
478                         call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
479                         call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
480                         call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
481                         call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
482                         endfor
483                 endif
484         endif
485 endproc
487 # Word lists: It is a VERY good idea to make sure that word-lists
488 # have unique names.
489 procedure load_word_list .localdir$ .relnumber
490         call clean_up_sound
491     
492     # Remove old word list
493     if wordlist$ <> ""
494                 select Table 'wordlist$'
495                 Remove
496                 call wipeArea 'wipeWordlistArea$'
497                 wordlist$ = ""
498     endif
499     
500         # Create Table that will recieve the wordlists and directories
501         Create Table with column names... AllWordLists 0 Name Directory
502         .te.currentWordlistRow = 0
503         
504         # Start with the word lists in the distribution, unless the local directory exists!
505         call CreateCreateWordlists
506         select Table CreateWordlists
507     .numLists = Get number of rows
508         for .i to .numLists
509                 select Table CreateWordlists
510        .currentName$ = Get value... '.i' Name
511            if not (fileReadable("'.localdir$'/'.currentName$'") or fileReadable("'.localdir$'/'.currentName$'/wordlist.Table") or fileReadable("'.localdir$'/'.currentName$'/wordlist.txt"))
512                    .procedureName$ = replace_regex$(.currentName$, "[^\w\-\.]", "_", 0)
513                         select Table AllWordLists
514                         Append row
515                         .te.currentWordlistRow = Get number of rows
516                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
517                     .currentDirectory$ = "*call Create'.procedureName$'"
518                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
519                 endif
520         endfor
521         select Table CreateWordlists
522         Remove
524         # First the global word lists
525         if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
526         Create Strings as directory list... WordList 'globalwordlists$'
527         .numLists = Get number of strings
528         for .i to .numLists
529             select Strings WordList
530             .currentName$ = Get string... '.i'
531                         if .currentName$ <> "directory.txt"
532                                 select Table AllWordLists
533                                 .listExist = Search column: "Name", .currentName$
534                                 if not .listExist
535                                         Append row
536                                         .te.currentWordlistRow = Get number of rows
537                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
538                                 .currentDirectory$ = globalwordlists$+"/"+.currentName$
539                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
540                                 endif
541                         endif
542         endfor
543         select Strings WordList
544         Remove
545         endif
546         
547         # Now the preferences word lists
548         if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
549         Create Strings as directory list... WordList 'sgc2wordlists$'
550         .numLists = Get number of strings
551         for .i to .numLists
552             select Strings WordList
553             .currentName$ = Get string... '.i'
554                         if .currentName$ <> "directory.txt"
555                                 select Table AllWordLists
556                                 .listExist = Search column: "Name", .currentName$
557                                 if not .listExist
558                                         Append row
559                                         .te.currentWordlistRow = Get number of rows
560                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
561                                 .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
562                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
563                                 endif
564                         endif
565         endfor
566         select Strings WordList
567         Remove
568         endif
569         
570         # Finally, the local word lists
571         if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
572         Create Strings as directory list... WordList '.localdir$'
573         .numLists = Get number of strings
574         for .i to .numLists
575             select Strings WordList
576             .currentName$ = Get string... '.i'
577                         if .currentName$ <> "directory.txt"
578                                 select Table AllWordLists
579                                 .listExist = Search column: "Name", .currentName$
580                                 if not .listExist
581                                         Append row
582                                         .te.currentWordlistRow = Get number of rows
583                                         Set string value... '.te.currentWordlistRow' Name '.currentName$'
584                                 .currentDirectory$ = .localdir$+"/"+.currentName$
585                                         Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
586                                 endif
587                         endif
588         endfor
589         select Strings WordList
590         Remove
591     endif
593         # Get the position of the current word list
594         select Table AllWordLists
595         .currentNumber = 1
596     .numLists = Get number of rows
598         if wordlistName$ <> ""
599                 select Table AllWordLists
600                 .currentNumber = Search column... Name 'wordlistName$'
601         endif
603     wordlistNum = .currentNumber + .relnumber
604     if wordlistNum > .numLists
605         wordlistNum = 1
606         elsif wordlistNum < 1 and .numLists > 0
607                 wordlistNum = .numLists
608     endif
609     select Table AllWordLists
610     wordlistName$ = Get value... 'wordlistNum' Name
611         .dirWordlistName$ = Get value... 'wordlistNum' Directory
612     .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
613         
614         # Read in full tables
615         if fileReadable("'.dirString$'/wordlist.Table")
616                 call readTable '.dirString$'/wordlist.Table
617         if readTable.tableID > 0
618                         Rename... 'wordlistName$'
619                         # Praat wil change the name if it feels like it
620                         wordlist$ = selected$("Table")
621                         # Add a Sound column if it is not present
622                         .soundIndex = Get column index: "Sound"
623                         if .soundIndex <= 0
624                                 Append column: "Sound"
625                         endif
626                 else
627                         .numLists = 0
628                         goto EMERGENCYEXIT
629                 endif
630         # Handle (legacy) simple word lists
631         elsif fileReadable("'.dirString$'/wordlist.txt")
632                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Sound Translation
633         wordlist$ = selected$("Table")
634                 Read Strings from raw text file... '.dirString$'/wordlist.txt
635                 Rename... RawWordList
636                 .numWordStrings = Get number of strings
637                 for .i to .numWordStrings
638                         select Strings RawWordList
639                         .currentFile$ = "-"
640                         .currentChar$ = "-"
641                         .currentTrans$ = "-"
642                         .currentLine$ = Get string... '.i'
643                         # Remove leading whitespace
644                         .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
645                         .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
646                         if .separatorIndex <= 0
647                                 .separatorIndex = length(.currentLine$) + 1
648                         endif
649                         .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
650                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
651                         # There is more on the line, but we do not know what
652                         if length(.currentLine$) > 0
653                                 while length(.currentLine$) > 0
654                                         .separatorIndex = index_regex(.currentLine$, "[\t;]")
655                                         if .separatorIndex <= 0
656                                                 .separatorIndex = length(.currentLine$)+1
657                                         endif
658                                         .currentItem$ = left$(.currentLine$, .separatorIndex-1)
659                                         .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
660                                         if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
661                                                 # Audio
662                                                 .currentFile$ = .currentItem$
663                                         elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
664                                                 # Translation
665                                                 .currentTrans$ = .currentItem$
666                                         elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
667                                                 # Characters
668                                                 .currentChar$ = .currentItem$
669                                         endif
670                                 endwhile
671                         endif
672                         if .currentFile$ = "-"
673                                 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
674                                         .currentFile$ = .currentPinyin$+".spx"
675                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
676                                         .currentFile$ = .currentPinyin$+".flac"
677                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
678                                         .currentFile$ = .currentPinyin$+".wav"
679                                 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
680                                         .currentFile$ = .currentPinyin$+".mp3"
681                                 endif
682                         endif
683                         select Table 'wordlist$'
684                         Append row
685                         Set string value... '.i' Pinyin '.currentPinyin$'
686                         Set string value... '.i' Sound '.currentFile$'
687                         Set string value... '.i' Character '.currentChar$'
688                         Set string value... '.i' Translation '.currentTrans$'
689                 endfor
690                 select Strings RawWordList
691                 Remove
692                 
693                 select Table 'wordlist$'
694         elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
695                 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
696         wordlist$ = selected$("Table")
697                 Create Strings as file list... RawWordList '.dirString$'/*
698                 .numWordStrings = Get number of strings
699                 .i = 0
700                 for .j to .numWordStrings
701                         select Strings RawWordList
702                         .currentLine$ = Get string... '.j'
703                         .currentFile$ = extractWord$(.currentLine$, "")
704                         if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
705                                 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
706                                 select Table 'wordlist$'
707                                 Append row
708                                 .i += 1
709                                 Set string value... '.i' Pinyin '.currentPinyin$'
710                                 Set string value... '.i' Sound '.currentFile$'
711                         endif
712                 endfor
713                 select Strings RawWordList
714                 Remove
715                 
716                 select Table 'wordlist$'
717         elsif startsWith(.dirString$, "*call ")
718                 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
719                 '.callProcedure$'
720                 wordlist$ = selected$("Table")
721         else
722                 # Nothing, get out
723                 .numLists = 0
724                 goto EMERGENCYEXIT              
725         endif
727         # Can this wordlist be deleted?
728         if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
729                 config.deleteWordlist = 0
730         else
731                 config.deleteWordlist = -1              
732         endif
734         # Add a Character and Translation column if missing
735         if wordlist$ <> ""
736                 select Table 'wordlist$'
737         te.numberOfWords = Get number of rows
738                 .characterColumn = Get column index... Character
739                 if not .characterColumn
740                         Append column... Character
741                         for .i to te.numberOfWords
742                                 Set string value... '.i' Character -
743                         endfor
744                 endif
745                 .translationColumn = Get column index... Translation
746                 if not .translationColumn
747                         Append column... Translation
748                         for .i to te.numberOfWords
749                                 Set string value... '.i' Translation -
750                         endfor
751                 endif
752         endif
753         
754         # Remove all rows without Pinyin
755         .numRows = Get number of rows
756         for i to .numRows
757                 .rowNum = .numRows - i + 1
758                 .pinyinValue$ = Get value... '.rowNum' Pinyin
759                 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
760                         Remove row... '.rowNum'
761                 endif
762         endfor
763         te.numberOfWords = Get number of rows
764         
765         # Shuffle words if requested
766     if config.shuffleLists
767         Randomize rows
768     endif
770         # Clean up
771         label EMERGENCYEXIT
772     select Table AllWordLists
773     Remove
774         
775     # There were no Word Lists
776     label NOWORDLISTS
777     if .numLists <= 0
778         wordlistName$ = wordlistName$+" No Word Lists available"
779         wordlistNum = 1
780                 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
781         wordlist$ = selected$("Table")
782         .i = 0
783                 Append row
784                 .i += 1
785                 Set string value... '.i' Pinyin ni3hao3
786                 Set string value... '.i' Character 你好
787                 Set string value... '.i' Translation hello
788                 Set string value... '.i' Sound -
789                 Append row
790                 .i += 1
791                 Set string value... '.i' Pinyin xie4xie0
792                 Set string value... '.i' Character 谢谢
793                 Set string value... '.i' Translation thanks
794                 Set string value... '.i' Sound -
795                 Append row
796                 .i += 1
797                 Set string value... '.i' Pinyin zai4jian4
798                 Set string value... '.i' Character 再见
799                 Set string value... '.i' Translation goodbye
800                 Set string value... '.i' Sound -
801         te.numberOfWords = Get number of rows
802     endif
804         call set_window_title 'buttons$' 'wordlistName$'
805 endproc
807 procedure display_word_list_name
808     .xtext = 50
809     .ytext = 12
810    call reset_viewport
811     .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
812     call wipeArea 'wipeWordlistArea$'
813         call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
814         .currentFontSize = adjustFontSizeOnHeight.newFontSize
816     demo Blue
817         demo Text special... '.xtext' Centre '.ytext' Bottom Helvetica '.currentFontSize' 0 '.displayWordList$'
818     demo Black
819         demoShow()
820         call set_font_size 'defaultFontSize'
821 endproc
823 procedure write_word_list
824         # Write current Pinyin text
825         call display_text Black
826         
827         # Write the current word list name
828         call display_word_list_name
829 endproc
831 procedure start_logging
832         if fileReadable("'preferencesLogDir$'/logPerformance.txt")
833                 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
834                 .logDirectory$ = extractWord$(.logDirectory$, "")
835                 if .logDirectory$ = "" or not (fileReadable(.logDirectory$) or  fileReadable("'.logDirectory$'/directory.txt")
836                         .logDirectory$ = "'preferencesLogDir$'"
837                 endif
838                 currentLogDirectory$ = "'.logDirectory$'/log'logtimeStamp$'"
839                 createDirectory(currentLogDirectory$)
840                 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
841                         .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
842                         .headerLine$ > 'currentLogDirectory$'/wordlist.Table
843                 endif
844                         # Flip switch
845                         config.logPerformance = 1
846         endif
847 endproc
849 procedure log_command .logtext$
850         if logging
851                 fileappend "'currentLogDirectory$'/logFile.txt" '.logtext$''newline$'
852         endif
853 endproc
855 procedure log_performance .recordedSound$ .register .proficiency .pinyin$ .choice$
856         # Log files
857         .currentDate$ = date$()
858         .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
859         .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
861         if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
862            .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
863            .logtext$ = "# #+.pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
864        # Plain log text
865            fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
866            
867        # A wordlist.Table
868        fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
869        # The recorded sound
870            select Sound 'recordedSound$'
871            Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
872         endif
873 endproc
875 procedure paint_logging_light
876     select Table Config
877     .row = Search column... Label !Logging
878         if .row < 1
879                 exit Button Table Config does not have a row with label !Logging
880         endif
881         # Get button values
882     .leftX = Get value... '.row' LeftX
883     .rightX = Get value... '.row' RightX
884     .lowY = Get value... '.row' LowY
885     .highY = Get value... '.row' HighY
886     .buttonColor$ = Get value... '.row' Color
887     .centerX = (.leftX + .rightX)/2
888     .centerY = (.lowY + .highY)/2
889     .radius = (.highY - .lowY )/(4*2)
890     .wipeRadius = 1.1*.radius
891     if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
892         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
893         demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
894         demoShow()
895     else
896         demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
897         demoShow()
898     endif
899 endproc
901 # Uninstall word lists
902 procedure removeWordlist .deletedWordlistName$
903     .targetDir$ = ""
904     if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
905         .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
906     endif
907         if .targetDir$ <> ""
908         Create Strings as file list... DeleteList '.targetDir$'
909         .numdeleteFiles = Get number of strings
910         for .i to .numdeleteFiles
911                 .file$ = Get string... '.i'
912                 deleteFile("'.targetDir$'/'.file$'")
913         endfor
914         filedelete '.targetDir$'
915         # Check whether the directory was actually deleted. Make sure this is a valid directory name!
916         # That is, it does not contain funny characters, nor funny names
917         if index_regex(.deletedWordlistName$, "[^a-zA-Z0-9_ .\-]") <= 0 and index(.deletedWordlistName$, "..") <= 0 and index_regex(.deletedWordlistName$, "[a-zA-Z]") > 0
918                         if windows
919                                 nocheck system rmdir "'.targetDir$'" /s /q
920                         elsif fileReadable(.targetDir$)
921                                 system bash -rc -- 'rm -r -- "'.targetDir$'"'
922                         endif
923                 endif
924                 # Remove deleted word list
925                 select Strings DeleteList
926                 plus Table 'wordlist$'
927                 Remove
928                 wordlist$ = ""
929         endif
930 endproc
932 # Install word lists
933 procedure sgc2wordlist .sourceDir$
934         if startsWith(.sourceDir$, "preferencesDirectory$")
935                 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
936         endif
938         .targetDirectory$ = "'sgc2wordlists$'"
939         if  fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
940                 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
941                 .numFiles = Get number of strings
942                 for .i to .numFiles
943                         select Strings PackageList
944                         .file$ = Get string... '.i'
945                         call readWordlist '.sourceDir$' '.file$'
946                 endfor
948                 select Strings PackageList
949                 Remove
950         endif
951 endproc
953 # Debuggin remarks!!!
954 # fileReadable(<directory>) does not work in Windows
955 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
956 # And yet only the 7z decompression has been implemented
957 windowsUnzipCommand$ = ""
958 if fileReadable("C:\Program Files\7-Zip\7z.exe")
959         windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z.exe"" e"
960 endif
961 procedure readWordlist .sourceDir$ .file$
962         # No use doing anything if the source does not exist
963         if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
964                 # What will be the target wordlist directory?
965                 .targetDirectory$ = "'sgc2wordlists$'"
966                 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
967                 if .dirname$ = ""
968                         .dirname$ = .file$
969                 endif
970                 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
971                 # Wordlist directory does not exist, neither locally nor in the preferences
972                 .wordListExists = 0
973                 .tmpDirs = nocheck Create Strings as directory list... TMPWORDLISTS '.targetDirectory$'/
974                 if .tmpDirs != undefined and .tmpDirs > 0
975                         .numDirs = Get number of strings
976                         for .d to .numDirs
977                                 select .tmpDirs
978                                 .currentString$ = Get string... '.d'
979                                 if .currentString$ = .dirname$
980                                         .wordListExists = 1
981                                 endif
982                         endfor
983                         Remove
984                 endif
985                 if not (.wordListExists or fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
986                         .wasWordList = 0
987                         # Move source to destination
988                         if index(.file$, ".sgc") or index(.file$, ".zip")
989                                 if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and not (windows and windowsUnzipCommand$ = "")
990                                         # Create wordlist directory
991                                         createDirectory(.wordlistDirectory$)
992                                         .wasWordList = 1
993                                         if macintosh or unix
994                                                 system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
995                                                 system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
996                                         elsif windows and windowsUnzipCommand$ <> ""
997                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
998                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
999                                                 system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /Y
1000                                                 system chdir /d "'.winWordListDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
1001                                         endif
1002                                 endif
1003                                 deleteFile("'.wordlistDirectory$'/'.file$'")
1004                                 # Remove if not valid!
1005                                 if fileReadable("'.wordlistDirectory$'/wordlist.Table") or fileReadable("'.wordlistDirectory$'/wordlist.txt") or fileReadable("'.wordlistDirectory$'/LICENSE.txt")
1006                                         if fileReadable("'.wordlistDirectory$'/wordlist.Table")
1007                                                 call readTable '.sourceDir$'/'.file$'
1008                                                 if readTable.tableID > 0
1009                                                         select readTable.tableID
1010                                                         .pinyinCol = Get column index... Pinyin
1011                                                         Remove
1012                                                         # No Pinyin in table
1013                                                         if .pinyinCol <= 0
1014                                                                 .wasWordList = 0
1015                                                         endif
1016                                                 else
1017                                                         .wasWordList = 0
1018                                                 endif
1019                                         endif
1020                                 else
1021                                         # None of wordlist.Table, wordlist.txt, nor LICENSE.txt
1022                                         .wasWordList = 0
1023                                 endif
1024                                 ### REALLY DANGEROUS STUFF, SHOULD BE HANDLED BETTER
1025                                 if .wasWordList = 0
1026                                         # Remove newly created directory
1027                                         if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and index(.dirname$, "..") <= 0
1028                                                 if macintosh or unix
1029                                                         system bash -rc -- 'rm -r "'.wordlistDirectory$'/"'
1030                                                 elsif windows
1031                                                         system rmdir /Q /S "'.winWordListDirectory$'/"
1032                                                 endif
1033                                         endif
1034                                 endif
1035                         elsif index_regex(.file$, "\.(Table|txt|tsv)")
1036                                 # Check whether this is a valid table
1037                                 call readTable '.sourceDir$'/'.file$'
1038                                 if readTable.tableID > 0
1039                                         select readTable.tableID
1040                                         .pinyinCol = Get column index... Pinyin
1041                                         Remove
1042                                         
1043                                         if .pinyinCol > 0
1044                                                 # Create wordlist directory
1045                                                 createDirectory(.wordlistDirectory$)
1046                                                 .wasWordList = 1
1047                                                 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt|tsv)$", "\1", 0)
1048                                                 if .extension$ = "tsv"
1049                                                         .extension$ = "Table"
1050                                                 endif
1051                                                 if macintosh or unix
1052                                                         system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
1053                                                 elsif windows
1054                                                         .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1055                                                         .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1056                                                         system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'\wordlist.'.extension$'" /q
1057                                                 endif
1058                                         endif
1059                                 endif
1060                         elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
1061                                 # Copy wordlist directory
1062                                 if index_regex(.file$, "[^a-zA-Z0-9_\.\- ]") <= 0
1063                                         .wasWordList = 1
1064                                         if macintosh or unix
1065                                                 system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
1066                                         elsif windows
1067                                                 createDirectory(.wordlistDirectory$)
1068                                                 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1069                                                 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1070                                                 system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
1071                                         endif
1072                                 endif
1073                         endif
1074                         
1075                         # Set current word list to read list
1076                         if .wasWordList
1077                                 wordlistName$ = .dirname$
1078                         else
1079                                 .table$ = "Config"
1080                                 .label$ = "!NotAWordlist"
1082                                 # Get help text
1083                                 call findLabel '.table$' '.label$'
1084                                 .row = findLabel.row
1085                                 select Table '.table$'
1086                                 .helpText$ = Get value... '.row' Helptext
1087                                 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
1088                         call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
1089                                 # Wait for confirmation
1090                                 demoWaitForInput()
1091                         endif
1092                 endif
1093         endif
1094 endproc