4 # Praat script converting .Table files into praat scripts
6 # Copyright (C) 2010 R.J.J.H. van Son and the Netherlands Cancer Institute
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 # form Source directory and target file
24 # sentence Source_directory Data
25 # sentence Target_file CreateTables.praat
28 Text writing preferences... UTF-8
30 source_directory$ = "Data"
31 target_file$ = "CreateTables.praat"
32 call tables2scripts "'source_directory$'" 'target_file$'
34 source_directory$ = "wordlists"
35 target_file$ = "CreateWordlists.praat"
36 call tables2scripts "'source_directory$'" 'target_file$'
39 procedure tables2scripts .sourceDir$ .targetFile$
40 # Iterate over all tables in the directory
41 if fileReadable(.sourceDir$)
42 deleteFile(.targetFile$)
44 fileappend '.targetFile$' # 'target_file$''newline$'
45 fileappend '.targetFile$' # 'newline$'
46 fileappend '.targetFile$' # Automatically created code!'newline$'
47 fileappend '.targetFile$' # Any changes will be lost when this file is regenerated.'newline$'
48 fileappend '.targetFile$' # Adapt the original Tables instead of this code.'newline$'
49 fileappend '.targetFile$' # To regenerate the original Tables, run a Praat script'newline$'
50 fileappend '.targetFile$' # with the following code:'newline$'
51 fileappend '.targetFile$' # 'newline$'
52 fileappend '.targetFile$' #: include tables2scripts.praat'newline$'
53 fileappend '.targetFile$' #: call Create<TableName>'newline$'
54 fileappend '.targetFile$' #: Write to table file... <TableName>.Table'newline$'
55 fileappend '.targetFile$' # 'newline$'
56 fileappend '.targetFile$' # Where <TableName> is the name of the table.'newline$'
57 fileappend '.targetFile$' # Move the Table file to the desired location (eg, Data/)'newline$'
58 fileappend '.targetFile$' # 'newline$'
59 fileappend '.targetFile$' # Tables and this code are licensed under the GNU GPL version 2'newline$'
60 fileappend '.targetFile$' # or later.'newline$'
61 fileappend '.targetFile$' # 'newline$'
64 .nameStart = rindex(.targetFile$, "/")+1
65 .nameEnd = rindex(.targetFile$, ".") -.nameStart
66 .newTableNameList$ = mid$(.targetFile$, .nameStart, .nameEnd)
67 Create Table with column names... '.newTableNameList$' 0 Name
69 # Create a list of Tables with Paths
70 Create Table with column names... ListOfTables 0 Name Directory
72 call createListOfTables ListOfTables '.sourceDir$'
74 select Table ListOfTables
75 .numOfTables = Get number of rows
76 for .i to .numOfTables
77 select Table ListOfTables
78 .table$ = Get value... '.i' Name
79 .tableDir$ = Get value... '.i' Directory
81 Read from file... '.tableDir$'/'.table$'
82 .tableName$ = selected$("Table")
83 if .tableName$ = "wordlist" or .tableName$ = "table"
84 .nameStart = rindex(.tableDir$, "/")+1
85 .nameEnd = length(.tableDir$)+1 -.nameStart
86 .newTableName$ = mid$(.tableDir$, .nameStart, .nameEnd)
87 select Table '.tableName$'
88 Rename... '.newTableName$'
89 .tableName$ = selected$("Table")
92 select Table '.newTableNameList$'
94 .currentTableNum = Get number of rows
95 Set string value... '.currentTableNum' Name '.tableName$'
98 call table2procedure '.tableName$' '.targetFile$'
99 select Table '.tableName$'
104 call table2procedure '.newTableNameList$' '.targetFile$'
106 select Table '.newTableNameList$'
107 plus Table ListOfTables
110 exit Directory not found: '.sourceDir$'
114 # Convert a single table to a Praat script
115 procedure table2procedure .tableName$ .targetFile$
116 select Table '.tableName$'
119 # Collect information
120 .numberOfColumns = Get number of columns
121 .numberOfRows = Get number of rows
123 # Set name of procedure as variable
124 .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
125 fileappend '.targetFile$' 'newline$'procCreate'.tableVariableName$'$ = "'.tableName$'"'newline$'
128 fileappend '.targetFile$' procedure Create'.tableVariableName$''newline$'
129 # Create table with columns
130 fileappend '.targetFile$' 'tab$'Create Table with column names... '.tableName$' '.numberOfRows''newline$'
131 fileappend '.targetFile$' 'tab$'...
132 for .col to .numberOfColumns
133 .colName$ = Get column label... '.col'
134 fileappend '.targetFile$' '.space$''.colName$'
136 fileappend '.targetFile$' 'newline$'
137 fileappend '.targetFile$' 'tab$'# Fill table values'newline$'
140 for .row to .numberOfRows
141 fileappend '.targetFile$' 'tab$'# Row '.row''newline$'
142 for .col to .numberOfColumns
143 .label$ = Get column label... '.col'
144 .value$ = Get value... '.row' '.label$'
145 fileappend '.targetFile$' 'tab$'Set string value... '.row' '.label$' '.value$''newline$'
149 fileappend '.targetFile$' endproc'newline$'
152 # .listName$ is name of table to recieve all file names
153 # Labels are Name and Directory
154 # Who says you cannot do recursion in Praat?
155 # This is eerily fragile code.
157 procedure createListOfTables .listName$ .topDirectory$
159 .listName'recursion'$ = .listName$
160 .topDirectory'recursion'$ = .topDirectory$
162 .currentTopDirectory$ = .topDirectory'recursion'$
163 Create Strings as file list... Files '.currentTopDirectory$'/*.Table
164 .numOfFiles'recursion' = Get number of strings
165 for .i to .numOfFiles'recursion'
167 .table'recursion'$ = Get string... '.i'
169 .currentListName$ = .listName'recursion'$
170 select Table '.currentListName$'
172 .numRows = Get number of rows
173 .currentTable$ = .table'recursion'$
174 .currentTopDirectory$ = .topDirectory'recursion'$
175 Set string value... '.numRows' Name '.currentTable$'
176 Set string value... '.numRows' Directory '.currentTopDirectory$'
180 # Recurse into directories
181 .currentTopDirectory$ = .topDirectory'recursion'$
182 Create Strings as directory list... Directories '.currentTopDirectory$'
183 .numOfDirectories'recursion' = Get number of strings
184 for .i'recursion' to .numOfDirectories'recursion'
185 select Strings Directories
186 .currentI = .i'recursion'
187 .directory'recursion'$ = Get string... '.currentI'
188 .currentTopDirectory$ = .topDirectory'recursion'$
189 .currentDirectory$ = .directory'recursion'$
190 call createListOfTables '.listName$' '.currentTopDirectory$'/'.currentDirectory$'
192 select Strings Directories