-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadLabel.m
56 lines (54 loc) · 1.37 KB
/
readLabel.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
function [rhyme, startTime, endTime] = readLabel(labelFile, tier)
% read label (TextGrid) files
% input:
% - labelFile: the directory of label files
% - tier: the number of the label tier
% output:
% - rhyme: the label of the rhyme
% - startTime: the start time of the rhyme portion
% - endTime: the end time
% read TextGrid file
tgfile = fopen(labelFile, "r");
n = 0;
while ~feof(tgfile)
n = n + 1;
tgText{n} = fgetl(tgfile);
end
fclose(tgfile);
for l = 1:n
if startsWith(tgText{l}, " item")
ntier = extractBetween(tgText{l}, "[", "]");
if str2double(ntier) == tier
stier = l;
elseif str2double(ntier) == tier + 1
etier = l;
else
etier = n;
end
end
end
vpat = "[aei]{1,2}q?";
rn = 0;
for nl = stier:etier
if startsWith(tgText{nl}, " text = ")
label = extractBetween(tgText{nl}, '"', '"');
label = convertCharsToStrings(label);
containRhyme = ~isempty(regexp(label, vpat, 'once'));
if containRhyme
rn = nl;
else
nrn = nl;
end
end
end
if rn
label = extractBetween(tgText{rn}, '"', '"');
rhyme = convertCharsToStrings(label);
startTime = str2double(erase(tgText{rn-2}, "xmin = "));
endTime = str2double(erase(tgText{rn-1}, "xmax = "));
else
rhyme = "NA";
startTime = 0;
endTime = 0;
end
end