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$'
35 procedure tables2scripts .sourceDir$ .targetFile$
36 # Iterate over all tables in the directory
37 if fileReadable(.sourceDir$)
38 deleteFile(.targetFile$)
40 fileappend '.targetFile$' # 'target_file$''newline$'
41 fileappend '.targetFile$' # 'newline$'
42 fileappend '.targetFile$' # Automatically created code!'newline$'
43 fileappend '.targetFile$' # Any changes will be lost when this file is regenerated.'newline$'
44 fileappend '.targetFile$' # Adapt the original Tables instead of this code.'newline$'
45 fileappend '.targetFile$' # To regenerate the original Tables, run a Praat script'newline$'
46 fileappend '.targetFile$' # with the following code:'newline$'
47 fileappend '.targetFile$' # 'newline$'
48 fileappend '.targetFile$' #: include tables2scripts.praat'newline$'
49 fileappend '.targetFile$' #: call Create<TableName>'newline$'
50 fileappend '.targetFile$' #: Write to table file... <TableName>.Table'newline$'
51 fileappend '.targetFile$' # 'newline$'
52 fileappend '.targetFile$' # Where <TableName> is the name of the table.'newline$'
53 fileappend '.targetFile$' # Move the Table file to the desired location (eg, Data/)'newline$'
54 fileappend '.targetFile$' # 'newline$'
55 fileappend '.targetFile$' # Tables and this code are licensed under the GNU GPL version 2'newline$'
56 fileappend '.targetFile$' # or later.'newline$'
57 fileappend '.targetFile$' # 'newline$'
60 .nameStart = rindex(.targetFile$, "/")+1
61 .nameEnd = rindex(.targetFile$, ".") -.nameStart
62 .newTableNameList$ = mid$(.targetFile$, .nameStart, .nameEnd)
63 Create Table with column names... '.newTableNameList$' 0 Name
65 # Create a list of Tables with Paths
66 Create Table with column names... ListOfTables 0 Name Directory
68 call createListOfTables ListOfTables '.sourceDir$'
70 select Table ListOfTables
71 .numOfTables = Get number of rows
72 for .i to .numOfTables
73 select Table ListOfTables
74 .table$ = Get value... '.i' Name
75 .tableDir$ = Get value... '.i' Directory
77 Read from file... '.tableDir$'/'.table$'
78 .tableName$ = selected$("Table")
79 if .tableName$ = "wordlist" or .tableName$ = "table"
80 .nameStart = rindex(.tableDir$, "/")+1
81 .nameEnd = length(.tableDir$)+1 -.nameStart
82 .newTableName$ = mid$(.tableDir$, .nameStart, .nameEnd)
83 select Table '.tableName$'
84 Rename... '.newTableName$'
85 .tableName$ = selected$("Table")
88 select Table '.newTableNameList$'
90 .currentTableNum = Get number of rows
91 Set string value... '.currentTableNum' Name '.tableName$'
94 call table2procedure '.tableName$' '.targetFile$'
95 select Table '.tableName$'
100 call table2procedure '.newTableNameList$' '.targetFile$'
102 select Table '.newTableNameList$'
103 plus Table ListOfTables
106 exit Directory not found: '.sourceDir$'
110 # Convert a single table to a Praat script
111 procedure table2procedure .tableName$ .targetFile$
112 select Table '.tableName$'
115 # Collect information
116 .numberOfColumns = Get number of columns
117 .numberOfRows = Get number of rows
119 # Set name of procedure as variable
120 fileappend '.targetFile$' 'newline$'procCreate'.tableName$'$ = "'.tableName$'"'newline$'
123 fileappend '.targetFile$' procedure Create'.tableName$''newline$'
124 # Create table with columns
125 fileappend '.targetFile$' 'tab$'Create Table with column names... '.tableName$' '.numberOfRows''newline$'
126 fileappend '.targetFile$' 'tab$'...
127 for .col to .numberOfColumns
128 .colName$ = Get column label... '.col'
129 fileappend '.targetFile$' '.space$''.colName$'
131 fileappend '.targetFile$' 'newline$'
132 fileappend '.targetFile$' 'tab$'# Fill table values'newline$'
135 for .row to .numberOfRows
136 fileappend '.targetFile$' 'tab$'# Row '.row''newline$'
137 for .col to .numberOfColumns
138 .label$ = Get column label... '.col'
139 .value$ = Get value... '.row' '.label$'
140 fileappend '.targetFile$' 'tab$'Set string value... '.row' '.label$' '.value$''newline$'
144 fileappend '.targetFile$' endproc'newline$'
147 # .listName$ is name of table to recieve all file names
148 # Labels are Name and Directory
149 # Who says you cannot do recursion in Praat?
150 # This is eerily fragile code.
152 procedure createListOfTables .listName$ .topDirectory$
154 .listName'recursion'$ = .listName$
155 .topDirectory'recursion'$ = .topDirectory$
157 .currentTopDirectory$ = .topDirectory'recursion'$
158 Create Strings as file list... Files '.currentTopDirectory$'/*.Table
159 .numOfFiles'recursion' = Get number of strings
160 for .i to .numOfFiles'recursion'
162 .table'recursion'$ = Get string... '.i'
164 .currentListName$ = .listName'recursion'$
165 select Table '.currentListName$'
167 .numRows = Get number of rows
168 .currentTable$ = .table'recursion'$
169 .currentTopDirectory$ = .topDirectory'recursion'$
170 Set string value... '.numRows' Name '.currentTable$'
171 Set string value... '.numRows' Directory '.currentTopDirectory$'
175 # Recurse into directories
176 .currentTopDirectory$ = .topDirectory'recursion'$
177 Create Strings as directory list... Directories '.currentTopDirectory$'
178 .numOfDirectories'recursion' = Get number of strings
179 for .i'recursion' to .numOfDirectories'recursion'
180 select Strings Directories
181 .currentI = .i'recursion'
182 .directory'recursion'$ = Get string... '.currentI'
183 .currentTopDirectory$ = .topDirectory'recursion'$
184 .currentDirectory$ = .directory'recursion'$
185 call createListOfTables '.listName$' '.currentTopDirectory$'/'.currentDirectory$'
187 select Strings Directories