forked from naxingyu/kaldi-gadgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliphn_to_textgrid.pl
76 lines (64 loc) · 1.97 KB
/
aliphn_to_textgrid.pl
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
#!/usr/bin/perl
# Copyright 2016 LeSpeech (Author: Xingyu Na)
if ($#ARGV != 2) {
die "Usage: aliphn_to_textgrid.pl ali.1.phn data/lang/phones.txt ali.1\n" .
" ali.1.phn : generated by ali-to-phn with --write-lengths=true\n" .
" phones.txt: phone list generated when preparing lang\n" .
" ali.1 : output dir for TextGrid files\n";
}
$ali_fn = $ARGV[0];
$phn_fn = $ARGV[1];
$out_dir = $ARGV[2];
open PHN, $phn_fn || die "Cannot open $phn_fn!\n";
my %phn = ();
foreach (<PHN>) {
chomp;
@items = split /\s+/, $_;
$phn{$items[1]} = $items[0];
}
close PHN;
open ALI, $ali_fn || die "Cannot open $ali_fn!\n";
mkdir $out_dir;
foreach (<ALI>) {
chomp;
@items = split /;/, $_;
@subitems = split /\s+/, $items[0];
$utt = $subitems[0];
my @labs = ();
@label = [$phn{$subitems[1]}, $subitems[2] * 0.01];
push @labs, @label;
my $len = $subitems[2] * 0.01;
for ($i = 1; $i <= $#items; $i++) {
@subitems = split /\s+/, $items[$i];
push @labs, [$phn{$subitems[1]}, $subitems[2] * 0.01];
$len = $len + $subitems[2] * 0.01;
}
open TXT, ">$out_dir/$utt.TextGrid" || die "Cannot write to $out_dir/$utt.TextGrid!\n";
print TXT "File type = \"ooTextFile\"\n";
print TXT "Object class = \"TextGrid\"\n\n";
print TXT "xmin = 0\n";
print TXT "xmax = $len\n";
print TXT "tiers? <exists>\n";
print TXT "size = 1\n";
print TXT "item []:\n";
print TXT " item [1]:\n";
print TXT " class = \"IntervalTier\"\n";
print TXT " name = \"phn\"\n";
print TXT " xmin = 0\n";
print TXT " xmax = $len\n";
$n = @labs;
print TXT " intervals: size = $n\n";
$xmin = 0;
$xmax = 0;
for ($i = 0; $i <= $#labs; $i++) {
$id = $i + 1;
$xmin = $xmax;
$xmax = $xmax + $labs[$i][1];
print TXT " intervals[$id]:\n";
print TXT " xmin = $xmin\n";
print TXT " xmax = $xmax\n";
print TXT " text = \"$labs[$i][0]\"\n";
}
close TXT;
}
close ALI;