aligned Patch file with new Praat version
[sgc2.git] / tables2scripts.praat
blob5195bcf475f00e766f3880d0ed58a25098cae079
2 # tables2scripts
3
4 #     Praat script converting .Table files into praat scripts
5 #     
6 #     Copyright (C) 2010  R.J.J.H. van Son and the Netherlands Cancer Institute
7
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.
12
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.
17
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
21
23 # form Source directory and target file
24 #       sentence Source_directory       Data
25 #       sentence Target_file    CreateTables.praat
26 # endform
28 Text writing preferences... UTF-8
30 source_directory$ = "Data"
31 target_file$ = "CreateTables.praat"
32 call tables2scripts "'source_directory$'" 'target_file$'
34 # Definitions
35 procedure tables2scripts .sourceDir$ .targetFile$
36         # Iterate over all tables in the directory
37         if fileReadable(.sourceDir$)
38                 deleteFile(.targetFile$)
39                 # Start new file
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$'
58                 
59                 # 
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
67                 recursion = 0
68                 call createListOfTables ListOfTables '.sourceDir$'
69         
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
76                         # Get Table
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")
86                         endif
88                         select Table '.newTableNameList$'
89                         Append row
90                         .currentTableNum = Get number of rows
91                         Set string value... '.currentTableNum' Name '.tableName$'
93                         # Convert table
94                         call table2procedure '.tableName$' '.targetFile$'
95                         select Table '.tableName$'
96                         Remove
97                 endfor
99                 # Convert table
100                 call table2procedure '.newTableNameList$' '.targetFile$'
102                 select Table '.newTableNameList$'
103                 plus Table ListOfTables
104                 Remove
105         else
106                 exit Directory not found: '.sourceDir$'
107         endif
108 endproc
110 # Convert a single table to a Praat script
111 procedure table2procedure .tableName$ .targetFile$
112         select Table '.tableName$'
113         
114         .space$ = " "
115         # Collect information
116         .numberOfColumns = Get number of columns
117         .numberOfRows = Get number of rows
118         
119         # Set name of procedure as variable
120         fileappend '.targetFile$' 'newline$'procCreate'.tableName$'$ = "'.tableName$'"'newline$'
121         
122         # Start output
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$'
130         endfor
131         fileappend '.targetFile$' 'newline$'
132         fileappend '.targetFile$' 'tab$'# Fill table values'newline$'
134         # Fill the table
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$'
141                 endfor
142         endfor
144         fileappend '.targetFile$' endproc'newline$'
145 endproc
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.
151 recursion = 0
152 procedure createListOfTables .listName$ .topDirectory$
153         recursion += 1
154         .listName'recursion'$ = .listName$
155         .topDirectory'recursion'$ = .topDirectory$
156         # Files
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'
161                 select Strings Files
162                 .table'recursion'$ = Get string... '.i'
163                 
164                 .currentListName$ = .listName'recursion'$
165                 select Table '.currentListName$'
166                 Append row
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$'
172         endfor
173         select Strings Files
174         Remove
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$'
186         endfor
187         select Strings Directories
188         Remove
189         recursion -= 1
190 endproc