4 # Praat script converting .Table files into praat scripts
6 # Copyright (C) 2010 R.J.J.H. van Son
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 source_directory$ = "Data"
29 target_file$ = "CreateTables.praat"
30 call tables2scripts "'source_directory$'" 'target_file$'
32 source_directory$ = "wordlists"
33 target_file$ = "CreateWordlists.praat"
34 call tables2scripts "'source_directory$'" 'target_file$'
37 procedure tables2scripts .sourceDir$ .targetFile$
38 # Iterate over all tables in the directory
39 if fileReadable(.sourceDir$)
40 deleteFile(.targetFile$)
42 fileappend '.targetFile$' # 'target_file$''newline$'
43 fileappend '.targetFile$' # 'newline$'
44 fileappend '.targetFile$' # Automatically created code!'newline$'
45 fileappend '.targetFile$' # Any changes will be lost when this file is regenerated.'newline$'
46 fileappend '.targetFile$' # Adapt the original Tables instead of this code.'newline$'
47 fileappend '.targetFile$' # To regenerate the original Tables, run a Praat script'newline$'
48 fileappend '.targetFile$' # with the following code:'newline$'
49 fileappend '.targetFile$' # 'newline$'
50 fileappend '.targetFile$' #: include tables2scripts.praat'newline$'
51 fileappend '.targetFile$' #: call Create<TableName>'newline$'
52 fileappend '.targetFile$' #: Write to table file... <TableName>.Table'newline$'
53 fileappend '.targetFile$' # 'newline$'
54 fileappend '.targetFile$' # Where <TableName> is the name of the table.'newline$'
55 fileappend '.targetFile$' # Move the Table file to the desired location (eg, Data/)'newline$'
56 fileappend '.targetFile$' # 'newline$'
57 fileappend '.targetFile$' # Tables and this code are licensed under the GNU GPL version 3'newline$'
58 fileappend '.targetFile$' # or later.'newline$'
59 fileappend '.targetFile$' # 'newline$'
62 .nameStart = rindex(.targetFile$, "/")+1
63 .nameEnd = rindex(.targetFile$, ".") -.nameStart
64 .newTableNameList$ = mid$(.targetFile$, .nameStart, .nameEnd)
65 Create Table with column names... '.newTableNameList$' 0 Name
67 # Create a list of Tables with Paths
68 Create Table with column names... ListOfTables 0 Name Directory
70 call createListOfTables ListOfTables '.sourceDir$'
72 select Table ListOfTables
73 .numOfTables = Get number of rows
74 for .i to .numOfTables
75 select Table ListOfTables
76 .table$ = Get value... '.i' Name
77 .tableDir$ = Get value... '.i' Directory
79 Read from file... '.tableDir$'/'.table$'
80 .tableName$ = selected$("Table")
81 if .tableName$ = "wordlist" or .tableName$ = "table"
82 .nameStart = rindex(.tableDir$, "/")+1
83 .nameEnd = length(.tableDir$)+1 -.nameStart
84 .newTableName$ = mid$(.tableDir$, .nameStart, .nameEnd)
85 select Table '.tableName$'
86 Rename... '.newTableName$'
87 .tableName$ = selected$("Table")
90 select Table '.newTableNameList$'
92 .currentTableNum = Get number of rows
93 Set string value... '.currentTableNum' Name '.tableName$'
96 call table2procedure '.tableName$' '.targetFile$'
97 select Table '.tableName$'
102 call table2procedure '.newTableNameList$' '.targetFile$'
104 select Table '.newTableNameList$'
105 plus Table ListOfTables
108 exit Directory not found: '.sourceDir$'
112 # Convert a single table to a Praat script
113 procedure table2procedure .tableName$ .targetFile$
114 select Table '.tableName$'
117 # Collect information
118 .numberOfColumns = Get number of columns
119 .numberOfRows = Get number of rows
122 fileappend '.targetFile$' procedure Create'.tableName$''newline$'
123 # Create table with columns
124 fileappend '.targetFile$' 'tab$'Create Table with column names... '.tableName$' '.numberOfRows''newline$'
125 fileappend '.targetFile$' 'tab$'...
126 for .col to .numberOfColumns
127 .colName$ = Get column label... '.col'
128 fileappend '.targetFile$' '.space$''.colName$'
130 fileappend '.targetFile$' 'newline$'
131 fileappend '.targetFile$' 'tab$'# Fill table values'newline$'
134 for .row to .numberOfRows
135 fileappend '.targetFile$' 'tab$'# Row '.row''newline$'
136 for .col to .numberOfColumns
137 .label$ = Get column label... '.col'
138 .value$ = Get value... '.row' '.label$'
139 fileappend '.targetFile$' 'tab$'Set string value... '.row' '.label$' '.value$''newline$'
143 fileappend '.targetFile$' endproc'newline$'
146 # .listName$ is name of table to recieve all file names
147 # Labels are Name and Directory
148 # Who says you cannot do recursion in Praat?
149 # This is earily fragile code.
151 procedure createListOfTables .listName$ .topDirectory$
153 .listName'recursion'$ = .listName$
154 .topDirectory'recursion'$ = .topDirectory$
156 .currentTopDirectory$ = .topDirectory'recursion'$
157 Create Strings as file list... Files '.currentTopDirectory$'/*.Table
158 .numOfFiles'recursion' = Get number of strings
159 for .i to .numOfFiles'recursion'
161 .table'recursion'$ = Get string... '.i'
163 .currentListName$ = .listName'recursion'$
164 select Table '.currentListName$'
166 .numRows = Get number of rows
167 .currentTable$ = .table'recursion'$
168 .currentTopDirectory$ = .topDirectory'recursion'$
169 Set string value... '.numRows' Name '.currentTable$'
170 Set string value... '.numRows' Directory '.currentTopDirectory$'
174 # Recurse into directories
175 .currentTopDirectory$ = .topDirectory'recursion'$
176 Create Strings as directory list... Directories '.currentTopDirectory$'
177 .numOfDirectories'recursion' = Get number of strings
178 for .i'recursion' to .numOfDirectories'recursion'
179 select Strings Directories
180 .currentI = .i'recursion'
181 .directory'recursion'$ = Get string... '.currentI'
182 .currentTopDirectory$ = .topDirectory'recursion'$
183 .currentDirectory$ = .directory'recursion'$
184 call createListOfTables '.listName$' '.currentTopDirectory$'/'.currentDirectory$'
186 select Strings Directories