forked from mktiede/GetContours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadPraatTier.m
225 lines (213 loc) · 6.14 KB
/
ReadPraatTier.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function [segs,labs,tier,tierNames,dur] = ReadPraatTier(fName,tier)
%READPRAATTIER - read tier data from PRAAT TextGrid file
%
% usage: [segs,labs,tier,tierNames,dur] = ReadPraatTier(fName, tier)
%
% use this procedure to load the specified point or interval TIER data
% from PRAAT TextGrid format FNAME (both standard and short forms supported)
%
% default ".TextGrid" extension is optional
%
% if no return arguements are specified returns a list of available tier names
%
% TIER may be a tier name or numeric index; if none specified the first tier found is used
%
% returns SEGS offsets [nSegs x offs] and segment labels LABS [nSegs]
% for specified tier (for interval tiers offs == head,tail)
% filter labelled intervals using
% k = find(~cellfun(@isempty,labs)); ht = segs(k,:); labs = labs(k);
%
% optionally returns name of tier used (TIER), a list of all tiers (TIERNAMES),
% and DURation (secs)
%
% see also WRITEPRAATTEXTGRID
% mkt 03/09
% mkt 07/14 updated
% mkt 01/18 negative offset support
% parse args
if nargin < 1,
eval('help ReadPraatTier');
return;
end;
if nargin<2, tier = []; end
if isempty(tier), tier = 1; end
[p,f,e] = fileparts(fName);
if isempty(e), fName = fullfile(p,[f,'.TextGrid']); end;
% vacuum file
try,
fid = fopen(fName,'rt');
lines = {};
while 1,
line = fgetl(fid);
if ~ischar(line), break; end;
lines{end+1,1} = line;
end;
fclose(fid);
catch,
error('error attempting to load from %s', fName);
end;
% some rudimentary format checking
if length(lines)<15 || isempty(findstr(lines{1},'ooTextFile')) || isempty(findstr(lines{2},'"TextGrid"')) || isempty(findstr(lines{6},'exists')),
error('%s has unrecognized file format', fName);
end;
% long or short?
try,
data = str2num(char(lines([4 5 7])));
isShort = 1;
if isempty(data),
data(1) = str2num(lines{4}(8:end));
data(2) = str2num(lines{5}(8:end));
data(3) = str2num(lines{7}(8:end));
isShort = 0;
end;
catch,
error('%s has unrecognized file format', fName);
end;
head = data(1);
tail = data(2);
dur = tail;
nTiers = data(3);
% get tier names and types
pointTiers = []; intTiers = [];
if isShort,
k = strmatch('"TextTier"',lines,'exact');
if isempty(k),
pointTierNames = {};
else,
pointTiers = k + 1;
pointTierNames = lines(pointTiers);
for k = 1 : length(pointTiers), pointTierNames{k} = pointTierNames{k}(2:end-1); end;
end;
k = strmatch('"IntervalTier"',lines,'exact');
if isempty(k),
intTierNames = {};
else,
intTiers = k + 1;
intTierNames = lines(intTiers);
for k = 1 : length(intTiers), intTierNames{k} = intTierNames{k}(2:end-1); end;
end;
else,
k = find(~cellfun(@isempty,regexp(lines,'class = "TextTier"')));
if isempty(k),
pointTierNames = {};
else,
pointTiers = k + 1;
q = regexp(lines(k+1),'name = "(.+)"','tokens');
for k = 1 : length(q), pointTierNames(k) = q{k}{1}; end;
end;
k = find(~cellfun(@isempty,regexp(lines,'class = "IntervalTier"')));
if isempty(k),
intTierNames = {};
else,
intTiers = k + 1;
q = regexp(lines(k+1),'name = "(.+)"','tokens');
for k = 1 : length(q),
if isempty(q{k}), intTierNames(k) = {''}; else, intTierNames(k) = q{k}{1}; end;
end;
end;
end;
% list names if no output args
tierNames = [pointTierNames , intTierNames];
if nTiers ~= length(tierNames),
error('expecting %d tiers, found %d', nTiers, length(tierNames));
end;
if nargout < 1,
fprintf('Tiers in %s\n', fName);
if ~isempty(pointTierNames),
fprintf(' Point: "%s"', pointTierNames{1});
if length(pointTierNames)>1, fprintf(', "%s"', pointTierNames{2:end}); end
fprintf('\n');
end;
if ~isempty(intTierNames),
fprintf(' Interval: "%s"', intTierNames{1});
if length(intTierNames)>1, fprintf(', "%s"', intTierNames{2:end}); end
fprintf('\n');
end;
return;
end;
% format data
if isnumeric(tier),
tiers = [];
if ~isempty(pointTiers), tiers = [tiers ; pointTiers]; end;
if ~isempty(intTiers), tiers = [tiers ; intTiers]; end;
[tiers,k] = sort(tiers);
tierNames = tierNames(k);
tierStart = tiers(tier);
tier = tierNames(tier);
isPoint = ~isempty(strmatch(tier, pointTierNames));
else,
k = strmatch(tier, tierNames, 'exact');
if length(k) > 1,
k = k(1);
fprintf('more than one instance of %s found in %s; using first\n', tierNames{k}, fName);
elseif isempty(k),
k = strmatch(tier, tierNames);
if isempty(k),
error('%s not found in %s', tier, fName);
else,
fprintf('exact match to "%s" not found in %s; using "%s"\n', tier, fName, tierNames{1});
tier = tierNames{1};
end;
end;
k = strmatch(tier, pointTierNames, 'exact');
if isempty(k),
k = strmatch(tier, intTierNames, 'exact');
tiers = intTiers;
isPoint = 0;
else,
tiers = pointTiers;
isPoint = 1;
end;
tierStart = tiers(k);
end;
% verify tier length against file length
if isShort,
h = str2num(lines{tierStart+1});
t = str2num(lines{tierStart+2});
else,
h = str2num(lines{tierStart+1}(regexp(lines{tierStart+1},' [-]*\d'):end)); % support negative values
t = str2num(lines{tierStart+2}(regexp(lines{tierStart+2},' [-]*\d'):end));
end;
if head ~= h || tail ~= t,
error('mismatch between tier (%.1f:%.1f) and file lengths (%.1f:%.1f)', h,t,head,tail);
end;
% short format
urLines = lines;
if isShort,
nSegs = str2num(lines{tierStart+3});
M = (3 - isPoint);
h = tierStart + 4;
t = nSegs*M + h - 1;
n = (t - h + 1) / M;
lines = reshape(lines(h:t),M,n)';
labs = lines(:,M);
for k = 1 : length(labs),
labs{k} = labs{k}(2:end-1);
end;
lines = reshape(lines(:,1:(M-1))',(M-1)*n,1);
segs = reshape(str2num(char(lines)),(M-1),n)';
% long format
else,
nSegs = str2num(lines{tierStart+3}(regexp(lines{tierStart+3},'\d'):end));
M = (4 - isPoint);
h = tierStart + 4;
t = nSegs*M + h - 1;
n = (t - h + 1) / M;
lines = reshape(lines(h:t),M,n)';
labs = lines(:,M);
for k = 1 : length(labs),
kk = findstr(labs{k},'"');
if length(kk) ~= 2,
n = strmatch(labs{k},urLines);
error('tier name without closing " character in line %d of %s (unable to continue)', n, fName);
end;
labs{k} = labs{k}(kk(1)+1:kk(2)-1);
end;
lines = reshape(lines(:,2:(M-1))',(M-2)*n,1);
idx = cell2mat(regexp(lines,' [-]*\d')); % support negative
segs = zeros(length(lines),1);
for k = 1 : length(lines),
segs(k) = str2num(lines{k}(idx(k):end));
end;
segs = reshape(segs,(M-2),n)';
end;