4 # Praat script handling configuration page
6 # SpeakGoodChinese: Config.praat loads the code needed for the
7 # settings and the Settings page of SpeakGoodChinese.
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
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.
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.
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
29 ###############################################################
31 # Button Drawing Routines
33 ###############################################################
35 procedure DrawCredits .color$ .x .y .size
38 call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
39 .currentFontSize = adjustFontSizeOnHeight.newFontSize
40 demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
42 demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 i
45 call set_font_size 'defaultFontSize'
48 procedure DrawDeleteList .color$ .x .y .size
56 .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
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
67 call set_font_size '.currentFontSize'
68 demo Paint rectangle... White '.leftX' '.rightX' '.botY' '.topY'
70 demo Text... '.x' Centre '.botY' Bottom '.displayWordList$'
73 call set_font_size 'defaultFontSize'
76 procedure DrawManual .color$ .x .y .size
79 call adjustFontSizeOnHeight 'defaultFont$' 24 '.lineheight'
80 .currentFontSize = adjustFontSizeOnHeight.newFontSize
81 demo Paint circle... {0.2,0.2,0.8} '.x' '.y' '.size'
83 demo Text special... '.x' centre '.y' half Times '.currentFontSize' 0 M
86 call set_font_size 'defaultFontSize'
89 procedure DrawLogging .color$ .x .y .size
93 .color$ = "{0.5,0.5,1}"
97 demo Paint circle... '.color$' '.x' '.y' '.size'
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'
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
118 # Handle logging buttons with forced button draw
119 if fileReadable("'preferencesLogDir$'/logPerformance.txt")
120 call Draw_button 'config$' +!Logging 'config.logPerformance'
124 ###############################################################
126 # Button Processing Routines
128 ###############################################################
130 procedure processConfigShuffleLists .clickX .clickY .pressed$
132 .label$ = "ShuffleLists"
133 config.shuffleLists = not config.shuffleLists
134 .displayButton = 2*config.shuffleLists
135 call Draw_button '.table$' '.label$' '.displayButton'
138 procedure processConfigUseSoundExample .clickX .clickY .pressed$
140 .label$ = "UseSoundExample"
141 config.useSoundExample = not config.useSoundExample
142 .displayButton = 2*config.useSoundExample
143 call Draw_button '.table$' '.label$' '.displayButton'
146 procedure processConfigSynthesis .tts$ .clickX .clickY .pressed$
148 .label$ = "Synthesis_'.tts$'"
149 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
150 if config.synthesis$ = ""
151 config.synthesis$ = .tts$
154 config.synthesis$ = ""
158 config.synthesis$ = "_DISABLED_"
161 call Draw_button '.table$' '.label$' '.displayButton'
164 procedure processConfigStrict .clickX .clickY .pressed$
167 config.strict = not config.strict
173 # Change TTS for Strict!
175 call Draw_button '.table$' '.label$' '.displayButton'
178 procedure processConfigDisplayPinyin .clickX .clickY .pressed$
180 .label$ = "DisplayPinyin"
181 config.displayPinyin = not config.displayPinyin
182 .displayButton = 2*config.displayPinyin
183 call Draw_button '.table$' '.label$' '.displayButton'
186 procedure processConfigDisplayChar .clickX .clickY .pressed$
188 .label$ = "DisplayChar"
189 config.displayChar = not config.displayChar
190 .displayButton = 2*config.displayChar
191 call Draw_button 'table$' '.label$' '.displayButton'
194 procedure processConfigDisplayTrans .clickX .clickY .pressed$
196 .label$ = "DisplayTrans"
197 config.displayTrans = not config.displayTrans
198 .displayButton = 2*config.displayTrans
199 call Draw_button 'table$' '.label$' '.displayButton'
202 procedure processConfigDisplayNumbers .clickX .clickY .pressed$
204 .label$ = "DisplayNumbers"
205 config.displayNumbers = not config.displayNumbers
206 .displayButton = 2*config.displayNumbers
207 call Draw_button 'table$' '.label$' '.displayButton'
210 procedure processConfigLanguage .language$ .clickX .clickY .pressed$
212 .label$ = "Language_'.language$'"
213 call processLanguageCodes '.table$' '.label$'
216 procedure processConfigShowBackground .clickX .clickY .pressed$
218 .label$ = "ShowBackground"
219 config.showBackground = not config.showBackground
220 .displayButton = 2*config.showBackground
221 call Draw_button 'table$' '.label$' '.displayButton'
224 procedure processConfigInput .input$ .clickX .clickY .pressed$
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
232 procedure processConfigRegister .register .clickX .clickY .pressed$
234 .label$ = "Register_'.register'"
235 call setRegisterFromLabel '.table$' '.label$'
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$'
247 procedure processConfigDeleteWordlist .clickX .clickY .pressed$
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$'
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
261 exit Cannot find delete directive: '.table$' !'.label$'
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
269 # Wait for confirmation
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
277 call Draw_button '.table$' '.label$' 0
278 call Draw_config_page
282 wordlistTag$ = "Wordlist name"
283 procedure processConfigInstallWordlist .clickX .clickY .pressed$
285 .label$ = "InstallWordlist"
286 call Draw_button '.table$' '.label$' 1
288 # Do not use the automatic sgc list option, ask for a wordlist NAME
290 call findLabel '.table$' '.label$'
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, 1)
303 .wordlist_Name$ = '.wordlistButton$'$
305 call install_wordlists_by_name '.wordlist_Name$'
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$'
315 call load_word_list "'localWordlistDir$'" 0
316 call Draw_button '.table$' '.label$' 0
317 call Draw_config_page
320 procedure processConfigOpenWordlist .clickX .clickY .pressed$
322 .label$ = "OpenWordlist"
323 call Draw_button '.table$' '.label$' 1
326 call findLabel '.table$' '.label$'
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
340 # Select the words to practise. This is quite a complex piece of code
341 procedure processConfigSelectWords .clickX .clickY .pressed$
343 .label$ = "SelectWords"
344 call Draw_button '.table$' '.label$' 1
347 call findLabel '.table$' '.label$'
349 select Table '.table$'
350 .helpText$ = Get value... '.row' Helptext
351 call convert_praat_to_latin1 '.helpText$'
352 .helpText$ = convert_praat_to_latin1.text$
355 select Table 'wordlist$'
356 .tmpOriginalWordlist = Copy: "Original_'wordlist$'"
360 select Table 'wordlist$'
361 te.numberOfWords = Get number of rows
362 .currentWord = te.currentWord
363 if .currentWord <= 0 or .currentWord > te.numberOfWords or config.shuffleLists
368 call get_feedback_text 'config.language$' Part
369 call convert_praat_to_latin1 'get_feedback_text.text$'
370 .partText$ = convert_praat_to_latin1.text$
371 call get_feedback_text 'config.language$' Tones
372 call convert_praat_to_latin1 'get_feedback_text.text$'
373 .toneText$ = convert_praat_to_latin1.text$
374 call get_feedback_text 'config.language$' Cancel
375 call convert_praat_to_latin1 'get_feedback_text.text$'
376 .cancelText$ = convert_praat_to_latin1.text$
377 call get_feedback_text 'config.language$' Clear
378 call convert_praat_to_latin1 'get_feedback_text.text$'
379 .clearText$ = convert_praat_to_latin1.text$
380 call get_feedback_text 'config.language$' All
381 call convert_praat_to_latin1 'get_feedback_text.text$'
382 .allText$ = convert_praat_to_latin1.text$
383 call get_feedback_text 'config.language$' Previous
384 call convert_praat_to_latin1 'get_feedback_text.text$'
385 .prevWord$ = convert_praat_to_latin1.text$
386 call get_feedback_text 'config.language$' Next
387 call convert_praat_to_latin1 'get_feedback_text.text$'
388 .nextWord$ = convert_praat_to_latin1.text$
389 call get_feedback_text 'config.language$' Continue
390 call convert_praat_to_latin1 'get_feedback_text.text$'
391 .continueText$ = convert_praat_to_latin1.text$
392 call get_feedback_text 'config.language$' Show
393 call convert_praat_to_latin1 'get_feedback_text.text$'
394 .showText$ = convert_praat_to_latin1.text$
396 .pinyinText$ = "Pinyin"
397 .characterText$ = "Character"
400 select Table 'wordlist$'
401 .lessonCol = Get column index: "Lesson"
404 # All shown keeps track whether all words are shown
405 # If so, selecting a lesson is preceded by a Clear All
407 # Sort words for consistent selection interface
408 if config.shuffleLists
409 # Allow subdivision in lessons
411 Sort rows... Lesson Pinyin
417 for .i to te.numberOfWords
418 .currentLesson$ = Get value: .i, "Lesson"
419 if .currentLesson$ <> "" and .currentLesson$ <> "-" and index_regex(.lessonList$, "\t'.currentLesson$'\t") <= 0
420 .lessonList$ = .lessonList$ + .currentLesson$ + tab$
422 .lessonName$['.numLessons'] = .currentLesson$
424 .shown$ = Get value: .i, "Show"
432 .numWordsPerScreen = 15
433 while clicked <> 6 and clicked <> 1
438 select Table 'wordlist$'
439 .lessonCol = Get column index: "Lesson"
440 # Sort words for consistent selection interface
441 if config.shuffleLists
442 # Allow subdivision in lessons
444 Sort rows... Lesson Pinyin
449 .max = .numWordsPerScreen - 1
450 if .currentWord + .max > te.numberOfWords
451 .max = te.numberOfWords - .currentWord
453 for .i from 0 to .numWordsPerScreen - 1
455 .pinyin$[.i] = Get value: .currentWord+.i, "Pinyin"
456 .character$[.i] = Get value: .currentWord+.i, "Character"
459 .lessonNum$[.i] = Get value: .currentWord+.i, "Lesson"
460 .lessonNum$[.i] = " : " + .lessonNum$[.i]
462 .showText$[.i] = .pinyin$['.i']
463 if .character$['.i'] <> "-"
464 .showText$[.i] = .pinyin$['.i']+" ("+.character$['.i']+.lessonNum$['.i']+")"
465 elsif .lessonNum$['.i'] <> ""
466 .showText$[.i] = .pinyin$['.i']+" ( -"+.lessonNum$['.i']+")"
468 .showValueText$[.i] = Get value: .currentWord+.i, "Show"
469 if .showValueText$[.i] = "-"
474 .showVariable$[.i] = replace_regex$(.pinyin$[.i], ".+", "\l&", 0)
475 .showVariable$[.i] = replace_regex$(.showVariable$[.i], " ", "_", 0)
476 .tmp$ = .showVariable$['.i']
477 '.tmp$' = .showValue[.i]
484 # The user text input window (beginPause .... endPause)
485 beginPause(.helpText$)
486 boolean (.showText$[0], .showValue[0])
487 boolean (.showText$[1], .showValue[1])
488 boolean (.showText$[2], .showValue[2])
489 boolean (.showText$[3], .showValue[3])
490 boolean (.showText$[4], .showValue[4])
491 boolean (.showText$[5], .showValue[5])
492 boolean (.showText$[6], .showValue[6])
493 boolean (.showText$[7], .showValue[7])
494 boolean (.showText$[8], .showValue[8])
495 boolean (.showText$[9], .showValue[9])
496 boolean (.showText$[10], .showValue[10])
497 boolean (.showText$[11], .showValue[11])
498 boolean (.showText$[12], .showValue[12])
499 boolean (.showText$[13], .showValue[13])
500 boolean (.showText$[14], .showValue[14])
502 optionMenu: .partText$, 1
505 for .j to .numLessons
506 option: .lessonName$['.j']
509 optionMenu: .toneText$, 1
516 clicked = endPause ("'.cancelText$'", "'.clearText$'", "'.allText$'", "'.prevWord$'", "'.nextWord$'", "'.continueText$'", 6, 1)
519 for .i to te.numberOfWords
520 Set string value: .i, "Show", "-"
524 for .i to te.numberOfWords
525 Set string value: .i, "Show", "+"
529 # Get selected Part BEFORE we do anything else
531 .tmp$ = replace_regex$(.partText$, "^(.)", "\l\1", 0)
532 .lessonSelected = '.tmp$' - 1
534 if .lessonSelected > 0
537 select Table 'wordlist$'
538 for .i to te.numberOfWords
539 # Keep track of whether all are shown
540 .shown$ = Get value: .i, "Show"
545 for .i to te.numberOfWords
547 .currentLesson$ = Get value: .i, "Lesson"
548 if .currentLesson$ = .lessonName$['.lessonSelected']
549 Set string value: .i, "Show", "+"
551 Set string value: .i, "Show", "-"
554 for .i to te.numberOfWords
555 # Keep track of whether all are shown
556 .shown$ = Get value: .i, "Show"
557 if .firstShown <=0 and .shown$ <> "-"
562 for .i from 0 to .max
563 .tmp$ = .showVariable$['.i']
564 .showValue['.i'] = '.tmp$'
565 .showWord$['.i'] = "-"
566 if .showValue['.i'] <> 0
567 .showWord$['.i'] = "+"
569 Set string value: .currentWord+.i, "Show", .showWord$['.i']
572 .tmp$ = replace_regex$(.toneText$, "^(.)", "\l\1", 0)
573 .toneSelected = '.tmp$' - 2
574 if .toneSelected >= 0
575 select Table 'wordlist$'
576 for .i to te.numberOfWords
577 .currentPinyin$ = Get value: .i, "Pinyin"
578 .tmp$ = Get value: .i, "Show"
579 if .tmp$ <> "-" and index(.currentPinyin$, "'.toneSelected'") > 0
580 Set string value: .i, "Show", "+"
582 Set string value: .i, "Show", "-"
590 .currentWord = (.firstShown div .numWordsPerScreen) * .numWordsPerScreen + 1
591 elsif .toneSelected < 0
592 .currentWord -= .numWordsPerScreen
595 .currentWord = (te.numberOfWords div .numWordsPerScreen) * .numWordsPerScreen + 1
599 .currentWord = (.firstShown div .numWordsPerScreen) * .numWordsPerScreen + 1
600 elsif .toneSelected < 0
601 .currentWord += .numWordsPerScreen
603 if .currentWord > te.numberOfWords
608 # Reset and go to the first selected word (can shuffle list)
617 select Table 'wordlist$'
619 select .tmpOriginalWordlist
622 select .tmpOriginalWordlist
624 call Draw_button '.table$' '.label$' 0
627 procedure processConfigPerfSummary .clickX .clickY .pressed$
629 .label$ = "PerfSummary"
631 call testLoadTable SummaryToneEvaluation
632 if testLoadTable.table > 0
633 call Draw_button '.table$' '.label$' 1
634 call loadTable SummaryToneEvaluation
635 call write_tabbed_table SummaryToneEvaluation Evaluation_'config.language$'
637 select Table SummaryToneEvaluation
640 call Draw_button '.table$' '.label$' 0
641 call Draw_config_page
645 procedure processConfigListPerf .clickX .clickY .pressed$
648 call Draw_button '.table$' '.label$' 1
650 call write_tabbed_table 'initialiseSGC2.toneevaluation_table$' Evaluation_'config.language$'
653 call Draw_button '.table$' '.label$' 0
654 call Draw_config_page
657 procedure processConfigSavePerf .clickX .clickY .pressed$
660 call Draw_button '.table$' '.label$' 1
663 call findLabel '.table$' '.label$'
665 select Table '.table$'
666 .openDialogue$ = Get value... '.row' Helptext
667 call convert_praat_to_latin1 '.openDialogue$'
668 .openDialogue$ = convert_praat_to_latin1.text$
670 .wordlist_Name$ = chooseWriteFile$ (.openDialogue$, "Performance.Table")
671 if .wordlist_Name$ <> ""
672 select Table 'initialiseSGC2.toneevaluation_table$'
673 Write to table file... '.wordlist_Name$'
676 call Draw_button '.table$' '.label$' 0
677 call Draw_config_page
680 procedure processConfigManual .clickX .clickY .pressed$
683 call Draw_button '.table$' '.label$' 1
684 if fileReadable("ManPages/SpeakGoodChinese_2.man")
685 Read from file... ManPages/SpeakGoodChinese_2.man
687 Go to manual page... SpeakGoodChinese 2
689 # Wait until the manual is put to the background
691 call Draw_button '.table$' '.label$' 0
693 call Draw_config_page
696 ###############################################################
698 # Obligatory button Processing Routines
700 # These MUST be defined
702 ###############################################################
704 procedure processConfigReturn .clickX .clickY .pressed$
707 call Draw_button '.table$' '.label$' 1
708 call write_preferences ""
711 procedure processConfigRefresh .clickX .clickY .pressed$
714 call Draw_config_page
717 procedure processConfigCredits .clickX .clickY .pressed$
720 call Draw_button '.table$' '.label$' 1
721 call write_text_table Credits_'config.language$'
723 call Draw_button '.table$' '.label$' 0
725 call Draw_config_page
728 procedure processConfigHelp .clickX .clickY .pressed$
731 call help_loop '.table$' Draw_config_page
734 ###############################################################
736 # Miscelaneous supporting code
738 ###############################################################
739 procedure install_wordlists_by_name .wordlist_Name$
740 if .wordlist_Name$ <> ""
741 if index(.wordlist_Name$, "wordlist.")
742 .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\]wordlist\.([^/\\]+)$", "", 0)
743 elsif index_regex(.wordlist_Name$, "(?i\.(wav|flac|iaf[fc]|mp3))")
744 .wordlist_Name$ = replace_regex$(.wordlist_Name$, "[/\\][^/\\]+$", "", 0)
746 if index_regex(.wordlist_Name$, "[/\\]")
747 .sourceDir$ = left$(.wordlist_Name$, rindex_regex(.wordlist_Name$, "[/\\]") -1)
748 .file$ = right$(.wordlist_Name$, length(.wordlist_Name$) - rindex_regex(.wordlist_Name$, "[/\\]"))
749 call readWordlist "'.sourceDir$'" '.file$'
752 if index(.wordlist_Name$, ".")
755 .extension1$ = ".sgc"
756 .extension2$ = ".Table"
757 .extension3$ = ".txt"
758 .extension4$ = ".tsv"
760 for .e from .start to 5
761 .currentExtension$ = .extension'.e'$
762 call readWordlist "'homeDirectory$'" '.wordlist_Name$''.currentExtension$'
763 call readWordlist "'homeDirectory$'/Downloads" '.wordlist_Name$''.currentExtension$'
764 call readWordlist "'homeDirectory$'/Documents" '.wordlist_Name$''.currentExtension$'
765 call readWordlist "'homeDirectory$'/My Documents/Downloads" '.wordlist_Name$''.currentExtension$'
766 call readWordlist "'homeDirectory$'/My Documents" '.wordlist_Name$''.currentExtension$'
767 call readWordlist "'homeDirectory$'/Desktop" '.wordlist_Name$''.currentExtension$'
768 call readWordlist "'preferencesAppDir$'" '.wordlist_Name$''.currentExtension$'
774 # Word lists: It is a VERY good idea to make sure that word-lists
776 procedure load_word_list .localdir$ .relnumber
779 # Remove old word list
781 select Table 'wordlist$'
783 call wipeArea 'wipeWordlistArea$'
787 # Create Table that will recieve the wordlists and directories
788 Create Table with column names... AllWordLists 0 Name Directory
789 .te.currentWordlistRow = 0
791 # First the global word lists
792 if fileReadable(globalwordlists$) or fileReadable("'globalwordlists$'/directory.txt")
793 Create Strings as directory list... WordList 'globalwordlists$'
794 .numLists = Get number of strings
796 select Strings WordList
797 .currentName$ = Get string... '.i'
798 if .currentName$ <> "directory.txt"
799 select Table AllWordLists
800 .listExist = Search column: "Name", .currentName$
803 .te.currentWordlistRow = Get number of rows
804 Set string value... '.te.currentWordlistRow' Name '.currentName$'
805 .currentDirectory$ = globalwordlists$+"/"+.currentName$
806 Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
810 select Strings WordList
814 # Now the preferences word lists
815 if fileReadable(sgc2wordlists$) or fileReadable("'sgc2wordlists$'/directory.txt")
816 Create Strings as directory list... WordList 'sgc2wordlists$'
817 .numLists = Get number of strings
819 select Strings WordList
820 .currentName$ = Get string... '.i'
821 if .currentName$ <> "directory.txt"
822 select Table AllWordLists
823 .listExist = Search column: "Name", .currentName$
826 .te.currentWordlistRow = Get number of rows
827 Set string value... '.te.currentWordlistRow' Name '.currentName$'
828 .currentDirectory$ = sgc2wordlists$+"/"+.currentName$
829 Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
833 select Strings WordList
837 # End with the word lists in the distribution
838 call CreateCreateWordlists
839 select Table CreateWordlists
840 .numLists = Get number of rows
842 select Table CreateWordlists
843 .currentName$ = Get value... '.i' Name
844 if .currentName$ <> "directory.txt"
845 select Table AllWordLists
846 .listExist = Search column: "Name", .currentName$
848 .procedureName$ = replace_regex$(.currentName$, "[^\w\-\.]", "_", 0)
849 select Table AllWordLists
851 .te.currentWordlistRow = Get number of rows
852 Set string value... '.te.currentWordlistRow' Name '.currentName$'
853 .currentDirectory$ = "*call Create'.procedureName$'"
854 Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
858 select Table CreateWordlists
861 # Finally, the local word lists
862 if fileReadable(.localdir$) or fileReadable("'.localdir$'/directory.txt")
863 Create Strings as directory list... WordList '.localdir$'
864 .numLists = Get number of strings
866 select Strings WordList
867 .currentName$ = Get string... '.i'
868 if .currentName$ <> "directory.txt"
869 select Table AllWordLists
870 .listExist = Search column: "Name", .currentName$
873 .te.currentWordlistRow = Get number of rows
874 Set string value... '.te.currentWordlistRow' Name '.currentName$'
875 .currentDirectory$ = .localdir$+"/"+.currentName$
876 Set string value... '.te.currentWordlistRow' Directory '.currentDirectory$'
880 select Strings WordList
884 # Get the position of the current word list
885 select Table AllWordLists
887 .numLists = Get number of rows
889 if wordlistName$ <> ""
890 select Table AllWordLists
891 .currentNumber = Search column... Name 'wordlistName$'
894 wordlistNum = .currentNumber + .relnumber
895 if wordlistNum > .numLists
897 elsif wordlistNum < 1 and .numLists > 0
898 wordlistNum = .numLists
900 select Table AllWordLists
901 wordlistName$ = Get value... 'wordlistNum' Name
902 .dirWordlistName$ = Get value... 'wordlistNum' Directory
903 .dirString$ = replace_regex$(.dirWordlistName$, "[ ]", "&", 0)
905 # Read in full tables
906 if fileReadable("'.dirString$'/wordlist.Table")
907 call readTable '.dirString$'/wordlist.Table
908 if readTable.tableID > 0
909 Rename... 'wordlistName$'
910 # Praat wil change the name if it feels like it
911 wordlist$ = selected$("Table")
912 # Add a Sound column if it is not present
913 .soundIndex = Get column index: "Sound"
915 Append column: "Sound"
921 # Handle (legacy) simple word lists
922 elsif fileReadable("'.dirString$'/wordlist.txt")
923 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Sound Translation
924 wordlist$ = selected$("Table")
925 Read Strings from raw text file... '.dirString$'/wordlist.txt
926 Rename... RawWordList
927 .numWordStrings = Get number of strings
928 for .i to .numWordStrings
929 select Strings RawWordList
933 .currentLine$ = Get string... '.i'
934 # Remove leading whitespace
935 .currentLine$ = replace_regex$(.currentLine$, "^[ \t]+", "", 0)
936 .separatorIndex = index_regex(.currentLine$, "[ \t;\-]")
937 if .separatorIndex <= 0
938 .separatorIndex = length(.currentLine$) + 1
940 .currentPinyin$ = left$(.currentLine$, .separatorIndex-1)
941 .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex)
942 # There is more on the line, but we do not know what
943 if length(.currentLine$) > 0
944 while length(.currentLine$) > 0
945 .separatorIndex = index_regex(.currentLine$, "[\t;]")
946 if .separatorIndex <= 0
947 .separatorIndex = length(.currentLine$)+1
949 .currentItem$ = left$(.currentLine$, .separatorIndex-1)
950 .currentLine$ = right$(.currentLine$, length(.currentLine$) - .separatorIndex )
951 if index_regex(.currentItem$, ".(spx|flac|wav|mp3)")
953 .currentFile$ = .currentItem$
954 elsif index_regex(.currentItem$, "[a-zA-Z0-9]") > 0
956 .currentTrans$ = .currentItem$
957 elsif index_regex(.currentItem$, "[^ \t\r\l]") > 0 && index_regex(.currentItem$, "[a-zA-Z0-9\-]") <= 0
959 .currentChar$ = .currentItem$
963 if .currentFile$ = "-"
964 if fileReadable("'.dirString$'/'.currentPinyin$'.spx")
965 .currentFile$ = .currentPinyin$+".spx"
966 elsif fileReadable("'.dirString$'/'.currentPinyin$'.flac")
967 .currentFile$ = .currentPinyin$+".flac"
968 elsif fileReadable("'.dirString$'/'.currentPinyin$'.wav")
969 .currentFile$ = .currentPinyin$+".wav"
970 elsif fileReadable("'.dirString$'/'.currentPinyin$'.mp3")
971 .currentFile$ = .currentPinyin$+".mp3"
974 select Table 'wordlist$'
976 Set string value... '.i' Pinyin '.currentPinyin$'
977 Set string value... '.i' Sound '.currentFile$'
978 Set string value... '.i' Character '.currentChar$'
979 Set string value... '.i' Translation '.currentTrans$'
981 select Strings RawWordList
984 select Table 'wordlist$'
985 elsif fileReadable(.dirString$) or fileReadable("'.dirString$'/directory.txt")
986 Create Table with column names... "'wordlistName$'" 0 Pinyin Sound
987 wordlist$ = selected$("Table")
988 Create Strings as file list... RawWordList '.dirString$'/*
989 .numWordStrings = Get number of strings
991 for .j to .numWordStrings
992 select Strings RawWordList
993 .currentLine$ = Get string... '.j'
994 .currentFile$ = extractWord$(.currentLine$, "")
995 if index_regex(.currentFile$, "\.(spx|flac|wav|mp3)")
996 .currentPinyin$ = left$(.currentFile$, index(.currentFile$, ".")-1)
997 select Table 'wordlist$'
1000 Set string value... '.i' Pinyin '.currentPinyin$'
1001 Set string value... '.i' Sound '.currentFile$'
1004 select Strings RawWordList
1007 select Table 'wordlist$'
1008 elsif startsWith(.dirString$, "*call ")
1009 .callProcedure$ = right$(.dirString$, length(.dirString$)-1)
1011 wordlist$ = selected$("Table")
1018 # Can this wordlist be deleted?
1019 if fileReadable("'sgc2wordlists$'/'wordlistName$'") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'wordlistName$'/wordlist.Table")
1020 config.deleteWordlist = 0
1022 config.deleteWordlist = -1
1025 # Check first column name and add Character, Translation and Show columns if missing
1027 select Table 'wordlist$'
1028 # HAck to correct odd behavior of tables with unicode characters
1029 .firstColumn$ = Get column label: 1
1030 if index_regex(.firstColumn$, "^[^!-~]") > 0
1031 .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1032 Set column label (index): 1, .firstColumn$
1035 te.numberOfWords = Get number of rows
1036 .characterColumn = Get column index... Character
1037 if not .characterColumn
1038 Append column... Character
1039 for .i to te.numberOfWords
1040 Set string value... '.i' Character -
1043 .translationColumn = Get column index... Translation
1044 if not .translationColumn
1045 Append column... Translation
1046 for .i to te.numberOfWords
1047 Set string value... '.i' Translation -
1050 .showColumn = Get column index... Show
1052 Append column... Show
1053 for .i to te.numberOfWords
1054 Set string value... '.i' Show +
1059 # Remove all rows without Pinyin
1060 .numRows = Get number of rows
1062 .rowNum = .numRows - i + 1
1063 .pinyinValue$ = Get value... '.rowNum' Pinyin
1064 if not index_regex(.pinyinValue$, "[a-zA-Z0-9]")
1065 Remove row... '.rowNum'
1068 te.numberOfWords = Get number of rows
1070 # Shuffle words if requested
1071 if config.shuffleLists
1077 select Table AllWordLists
1080 # There were no Word Lists
1083 wordlistName$ = wordlistName$+" No Word Lists available"
1085 Create Table with column names... "'wordlistName$'" 0 Pinyin Character Translation Sound
1086 wordlist$ = selected$("Table")
1090 Set string value... '.i' Pinyin ni3hao3
1091 Set string value... '.i' Character 你好
1092 Set string value... '.i' Translation hello
1093 Set string value... '.i' Sound -
1096 Set string value... '.i' Pinyin xie4xie0
1097 Set string value... '.i' Character 谢谢
1098 Set string value... '.i' Translation thanks
1099 Set string value... '.i' Sound -
1102 Set string value... '.i' Pinyin zai4jian4
1103 Set string value... '.i' Character 再见
1104 Set string value... '.i' Translation goodbye
1105 Set string value... '.i' Sound -
1106 te.numberOfWords = Get number of rows
1109 call set_window_title 'buttons$' 'wordlistName$'
1114 select Table 'wordlist$'
1116 if te.currentWord < 0 or te.currentWord > te.numberOfWords
1117 if config.shuffleLists
1122 while .showCurrent$ = "-" and te.currentWord <= te.numberOfWords
1124 if te.currentWord <= te.numberOfWords
1125 .showCurrent$ = Get value... 'te.currentWord' Show
1131 procedure previous_word
1133 select Table 'wordlist$'
1135 if te.currentWord <= 0
1136 if config.shuffleLists
1139 te.currentWord = te.numberOfWords + 1
1141 while .showCurrent$ = "-" and te.currentWord > 0
1143 if te.currentWord > 0
1144 .showCurrent$ = Get value... 'te.currentWord' Show
1150 procedure display_word_list_name
1154 .displayWordList$ = replace_regex$(wordlistName$, "[_]", " ", 0)
1155 call wipeArea 'wipeWordlistArea$'
1156 call adjustFontSizeOnHeight 'defaultFont$' 'defaultFontSize' 5
1157 .currentFontSize = adjustFontSizeOnHeight.newFontSize
1160 demo Text special... '.xtext' Centre '.ytext' Bottom 'defaultFont$' '.currentFontSize' 0 '.displayWordList$'
1163 call set_font_size 'defaultFontSize'
1166 procedure write_word_list
1167 # Write current Pinyin text
1168 call display_text Black
1170 # Write the current word list name
1171 call display_word_list_name
1174 procedure start_logging
1175 if fileReadable("'preferencesLogDir$'/logPerformance.txt")
1176 .logDirectory$ < 'preferencesLogDir$'/logPerformance.txt
1177 .logDirectory$ = extractWord$(.logDirectory$, "")
1178 if .logDirectory$ = "" or not (fileReadable(.logDirectory$) or fileReadable("'.logDirectory$'/directory.txt")
1179 .logDirectory$ = "'preferencesLogDir$'"
1181 currentLogDirectory$ = "'.logDirectory$'/log'logtimeStamp$'"
1182 createDirectory(currentLogDirectory$)
1183 if not fileReadable("'currentLogDirectory$'/wordlist.Table")
1184 .headerLine$ = "Pinyin'tab$'Character'tab$'Sound'newline$'"
1185 .headerLine$ > 'currentLogDirectory$'/wordlist.Table
1188 config.logPerformance = 1
1192 procedure log_command .logtext$
1194 fileappend "'currentLogDirectory$'/logFile.txt" '.logtext$''newline$'
1198 procedure log_performance .recordedSound$ .register .proficiency .pinyin$ .choice$
1200 .currentDate$ = date$()
1201 .timeStamp$ = replace_regex$(.currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
1202 .choiceText$ = replace_regex$(.choice$, "[^a-zA-Z0-9\-_]", "-", 0)
1204 if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
1205 .outfilename$ = .pinyin$+"_"+.choice$+"_'.register'_"+.timeStamp$+".wav"
1206 .logtext$ = "# #+.pinyin$+tab$+.choice$+tab$+"'.register'Hz"+tab$+.currentDate$+tab$+.outfilename$+newline$
1208 fileappend 'currentLogDirectory$'/logFile.txt '.logtext$'
1211 fileappend 'currentLogDirectory$'/wordlist.Table '.pinyin$''tab$''.choice$''tab$''.outfilename$''newline$'
1212 # The recorded sound
1213 select Sound 'recordedSound$'
1214 Write to WAV file... 'currentLogDirectory$'/'.outfilename$'
1218 procedure paint_logging_light
1220 .row = Search column... Label !Logging
1222 exit Button Table Config does not have a row with label !Logging
1225 .leftX = Get value... '.row' LeftX
1226 .rightX = Get value... '.row' RightX
1227 .lowY = Get value... '.row' LowY
1228 .highY = Get value... '.row' HighY
1229 .buttonColor$ = Get value... '.row' Color
1230 .centerX = (.leftX + .rightX)/2
1231 .centerY = (.lowY + .highY)/2
1232 .radius = (.highY - .lowY )/(4*2)
1233 .wipeRadius = 1.1*.radius
1234 if config.logPerformance and fileReadable("'preferencesLogDir$'/logPerformance.txt")
1235 demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
1236 demo Paint circle... '.buttonColor$' '.centerX' '.centerY' '.radius'
1239 demo Paint circle... White '.centerX' '.centerY' '.wipeRadius'
1244 # Uninstall word lists
1245 procedure removeWordlist .deletedWordlistName$
1247 if fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.txt") or fileReadable("'sgc2wordlists$'/'.deletedWordlistName$'/wordlist.Table")
1248 .targetDir$ = "'sgc2wordlists$'/'.deletedWordlistName$'"
1250 if .targetDir$ <> ""
1251 Create Strings as file list... DeleteList '.targetDir$'
1252 .numdeleteFiles = Get number of strings
1253 for .i to .numdeleteFiles
1254 .file$ = Get string... '.i'
1255 deleteFile("'.targetDir$'/'.file$'")
1257 filedelete '.targetDir$'
1258 # Check whether the directory was actually deleted. Make sure this is a valid directory name!
1259 # That is, it does not contain funny characters, nor funny names
1260 if index_regex(.deletedWordlistName$, "[^a-zA-Z0-9_ .\-]") <= 0 and index(.deletedWordlistName$, "..") <= 0 and index_regex(.deletedWordlistName$, "[a-zA-Z]") > 0
1262 nocheck system rmdir "'.targetDir$'" /s /q
1263 elsif fileReadable(.targetDir$)
1264 system bash -rc -- 'rm -r -- "'.targetDir$'"'
1267 # Remove deleted word list
1268 select Strings DeleteList
1269 plus Table 'wordlist$'
1275 # Install word lists
1276 procedure sgc2wordlist .sourceDir$
1277 if startsWith(.sourceDir$, "preferencesDirectory$")
1278 .sourceDir$ = replace$(.sourceDir$, "preferencesDirectory$", preferencesDirectory$)
1281 .targetDirectory$ = "'sgc2wordlists$'"
1282 if fileReadable(.sourceDir$) or fileReadable("'.sourceDir$'/directory.txt")
1283 Create Strings as file list... PackageList '.sourceDir$'/*.sgc
1284 .numFiles = Get number of strings
1286 select Strings PackageList
1287 .file$ = Get string... '.i'
1288 call readWordlist '.sourceDir$' '.file$'
1291 select Strings PackageList
1296 # Debuggin remarks!!!
1297 # fileReadable(<directory>) does not work in Windows
1298 # The file paths / -> \\ must be performed on the filenames in Windows before the system_nocheck is given
1299 # And yet only the 7z decompression has been implemented
1300 windowsUnzipCommand$ = ""
1301 if fileReadable("C:\Program Files\7-Zip\7z.exe")
1302 windowsUnzipCommand$ = """C:\Program Files\7-Zip\7z.exe"" e"
1303 elsif fileReadable("C:\Program Files\WinZip\winzip32.exe")
1304 # !!! Find a way to include the output folder !!!
1305 windowsUnzipCommand$ = "C:\Program Files\WinZip\winzip32.exe"" -min -e "
1307 procedure readWordlist .sourceDir$ .file$
1308 # No use doing anything if the source does not exist
1309 if fileReadable("'.sourceDir$'/'.file$'") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/wordlist.Table")
1310 # What will be the target wordlist directory?
1311 .targetDirectory$ = "'sgc2wordlists$'"
1312 .dirname$ = left$(.file$, rindex(.file$, ".")-1)
1316 .wordlistDirectory$ = .targetDirectory$+"/"+.dirname$
1317 # Wordlist directory does not exist, neither locally nor in the preferences
1319 .tmpDirs = nocheck Create Strings as directory list... TMPWORDLISTS '.targetDirectory$'/
1320 if .tmpDirs != undefined and .tmpDirs > 0
1321 .numDirs = Get number of strings
1324 .currentString$ = Get string... '.d'
1325 if .currentString$ = .dirname$
1331 if not (.wordListExists or fileReadable(.dirname$) or fileReadable("'.dirname$'/directory.txt") or fileReadable(.wordlistDirectory$) or fileReadable("'.wordlistDirectory$'/directory.txt"))
1333 # Move source to destination
1334 if index(.file$, ".sgc") or index(.file$, ".zip")
1335 if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and not (windows and windowsUnzipCommand$ = "")
1336 # Create wordlist directory
1337 createDirectory(.wordlistDirectory$)
1339 if macintosh or unix
1340 system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/"'
1341 system cd ''.wordlistDirectory$'';bash -rc -- 'unzip "'.file$'"'
1342 elsif windows and windowsUnzipCommand$ <> ""
1343 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1344 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1345 system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /Y
1346 system chdir /d "'.winWordListDirectory$'" && 'windowsUnzipCommand$' "'.file$'"
1349 deleteFile("'.wordlistDirectory$'/'.file$'")
1350 # Remove if not valid!
1351 if fileReadable("'.wordlistDirectory$'/wordlist.Table") or fileReadable("'.wordlistDirectory$'/wordlist.txt") or fileReadable("'.wordlistDirectory$'/LICENSE.txt")
1352 if fileReadable("'.wordlistDirectory$'/wordlist.Table")
1353 call readTable '.sourceDir$'/'.file$'
1354 if readTable.tableID > 0
1355 select readTable.tableID
1356 # Hack around odd behavior of column index
1358 .firstColumn$ = Get column label: 1
1359 .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1360 if .firstColumn$ = "Pinyin"
1363 .pinyinCol = Get column index... Pinyin
1366 # No Pinyin in table
1375 # None of wordlist.Table, wordlist.txt, nor LICENSE.txt
1378 ### REALLY DANGEROUS STUFF, SHOULD BE HANDLED BETTER
1380 # Remove newly created directory
1381 if index_regex(.wordlistDirectory$, "[^a-zA-Z0-9_\.\- /~\:]") <= 0 and index_regex(.dirname$, "[^a-zA-Z0-9_\.\- ]") <= 0 and index(.dirname$, "..") <= 0
1382 if macintosh or unix
1383 system bash -rc -- 'rm -r "'.wordlistDirectory$'/"'
1385 system rmdir /Q /S "'.winWordListDirectory$'/"
1389 elsif index_regex(.file$, "\.(Table|txt|tsv)")
1390 # Check whether this is a valid table
1391 call readTable '.sourceDir$'/'.file$'
1392 if readTable.tableID > 0
1393 select readTable.tableID
1394 # Hack around odd behavior of column index
1396 .firstColumn$ = Get column label: 1
1397 .firstColumn$ = replace_regex$(.firstColumn$, "^[^!-~]", "", 0)
1398 if .firstColumn$ = "Pinyin"
1401 .pinyinCol = Get column index... Pinyin
1406 # Create wordlist directory
1407 createDirectory(.wordlistDirectory$)
1409 .extension$ = replace_regex$(.file$, "^.+\.(Table|txt|tsv)$", "\1", 0)
1410 if .extension$ = "tsv"
1411 .extension$ = "Table"
1413 if macintosh or unix
1414 system bash -rc -- 'cp "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'/wordlist.'.extension$'"'
1416 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1417 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1418 system copy "'.winSourceDirectory$'" "'.winWordListDirectory$'\wordlist.'.extension$'" /q
1422 elsif fileReadable("'.sourceDir$'/'.file$'/wordlist.Table") or fileReadable("'.sourceDir$'/'.file$'/wordlist.txt") or fileReadable("'.sourceDir$'/'.file$'/LICENSE.txt")
1423 # Copy wordlist directory
1424 if index_regex(.file$, "[^a-zA-Z0-9_\.\- ]") <= 0
1426 if macintosh or unix
1427 system bash -rc -- 'cp -r "'.sourceDir$'/'.file$'" "'.wordlistDirectory$'"'
1429 createDirectory(.wordlistDirectory$)
1430 .winWordListDirectory$ = replace_regex$(.wordlistDirectory$, "/", "\\", 0)
1431 .winSourceDirectory$ = replace_regex$("'.sourceDir$'\'.file$'", "/", "\\", 0)
1432 system xcopy "'.winSourceDirectory$'" "'.winWordListDirectory$'" /s /e /q
1437 # Set current word list to read list
1439 wordlistName$ = .dirname$
1442 .label$ = "!NotAWordlist"
1445 call findLabel '.table$' '.label$'
1446 .row = findLabel.row
1447 select Table '.table$'
1448 .helpText$ = Get value... '.row' Helptext
1449 .filetext$ = replace_regex$("'.sourceDir$'/'.file$'", "_", "\\_ ", 0)
1450 call write_text_popup 'defaultFont$' 14 '.helpText$' "'.filetext$'"
1451 # Wait for confirmation