Ran tables2scripts.praat
[sgc2.git] / tables2scripts.praat
bloba73fde9e4192b0b5d1f578741bc088984cea64bc
2 # tables2scripts
3
4 #     Praat script converting .Table files into praat scripts
5 #     
6 #     Copyright (C) 2010  R.J.J.H. van Son
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 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$'
36 # Definitions
37 procedure tables2scripts .sourceDir$ .targetFile$
38         # Iterate over all tables in the directory
39         if fileReadable(.sourceDir$)
40                 deleteFile(.targetFile$)
41                 # Start new file
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 2'newline$'
58                 fileappend '.targetFile$' # or later.'newline$'
59                 fileappend '.targetFile$' # 'newline$'
60                 
61                 # 
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
69                 recursion = 0
70                 call createListOfTables ListOfTables '.sourceDir$'
71         
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
78                         # Get Table
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")
88                         endif
90                         select Table '.newTableNameList$'
91                         Append row
92                         .currentTableNum = Get number of rows
93                         Set string value... '.currentTableNum' Name '.tableName$'
95                         # Convert table
96                         call table2procedure '.tableName$' '.targetFile$'
97                         select Table '.tableName$'
98                         Remove
99                 endfor
101                 # Convert table
102                 call table2procedure '.newTableNameList$' '.targetFile$'
104                 select Table '.newTableNameList$'
105                 plus Table ListOfTables
106                 Remove
107         else
108                 exit Directory not found: '.sourceDir$'
109         endif
110 endproc
112 # Convert a single table to a Praat script
113 procedure table2procedure .tableName$ .targetFile$
114         select Table '.tableName$'
115         
116         .space$ = " "
117         # Collect information
118         .numberOfColumns = Get number of columns
119         .numberOfRows = Get number of rows
120                 
121         # Start output
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$'
129         endfor
130         fileappend '.targetFile$' 'newline$'
131         fileappend '.targetFile$' 'tab$'# Fill table values'newline$'
133         # Fill the table
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$'
140                 endfor
141         endfor
143         fileappend '.targetFile$' endproc'newline$'
144 endproc
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.
150 recursion = 0
151 procedure createListOfTables .listName$ .topDirectory$
152         recursion += 1
153         .listName'recursion'$ = .listName$
154         .topDirectory'recursion'$ = .topDirectory$
155         # Files
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'
160                 select Strings Files
161                 .table'recursion'$ = Get string... '.i'
162                 
163                 .currentListName$ = .listName'recursion'$
164                 select Table '.currentListName$'
165                 Append row
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$'
171         endfor
172         select Strings Files
173         Remove
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$'
185         endfor
186         select Strings Directories
187         Remove
188         recursion -= 1
189 endproc